如何解决使用woocommerce_product_set_stock挂钩获取库存变化的原因
我正在使用woocommerce,并创建了一个自定义表格来监视产品上的库存运动。我正在使用woocommerce_product_set_stock挂钩来实现此目的,并且它正在成功运行。这显示了库存量增加或减少的量以及用户,日期等的量。这是功能:
add_action( 'woocommerce_product_set_stock',array( $this,'save_stock' ) );
public function save_stock( $product ) {
//get the previous stock level from the custom table
global $wpdb;
$old_qty = $wpdb->get_var("SELECT qty FROM wp_stock_log WHERE product_id = '".$product->get_id()."' ORDER BY id DESC LIMIT 1 ");
//get current stock level and calculate the change.
$qty = $product->get_stock_quantity();
$change_in_stock = $qty-$old_qty;
//get user full name
$current_user = wp_get_current_user();
$first_name = $current_user->user_firstname;
$last_name = $current_user->user_lastname;
$full_name = $first_name . ' ' . $last_name;
$data = array();
//insert new row into the custom table
$data['date_created'] = date( 'Y-m-d H:i:s',time() );
$data['qty'] = $product->get_stock_quantity();
$data['qty_change'] = $change_in_stock;
$data['product_id'] = $product->get_id();
$data['user'] = $full_name;
$data['reason'] = SOME REASON ;
$wpdb->insert( $wpdb->prefix.'stock_log',$data );
}
wp_stock_log表如下:
|--------|--------------|------------|-----|----------|-----------|-------------|
| ID | date_created | product_id | qty |qty_change| user | reason |
|--------|--------------|------------|-----|----------|-----------|-------------|
| 1 | 14-08-2020 | 123 | 12 | -2 | Customer1 | Sale |
|--------|--------------|------------|-----|----------|-----------|-------------|
| 2 | 15-08-2020 | 123 | 10 | -1 | Customer2 | Sale |
|--------|--------------|------------|-----|----------|-----------|-------------|
| 3 | 15-08-2020 | 123 | 9 | 4 | Manager | Stock Added |
|--------|--------------|------------|-----|----------|-----------|-------------|
| 4 | 17-08-2020 | 123 | 13 | -1 | Customer2 | Sale |
|--------|--------------|------------|-----|----------|-----------|-------------|
我想显示库存变化的原因,例如,如果客户下了订单,则显示“销售”,如果商店经理将新库存添加到库存,则显示“添加的库存”。我怎么能做到这一点。是否可以使用现有属性来告诉我它是否与订单有关?
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。