问题描述
我正在使用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 (将#修改为@)