问题:我有三个产品相似的类型或他们是相同的(相同的产品,不同的名称)。如果这些产品应该有相同的库存。我不确定这是否可能?
例如:
Product One Stock= 10
Product Two Stock= 10
Product Three Stock= 10目标:希望为这些产品设置/更新相同的库存(如果其中任何一个是有序的)
结果1:产品1 qty 5按顺序更新新库存
Product One Stock = 10-5 =5
Product Two Stock = 10-5=5
Product Three Stock = 10-5=5结果2:产品1 qty 5和产品2 qty 2按顺序排列,因此同类产品=5+2=7的总数量如此更新
Product One Stock = 10-7 =3
Product Two Stock = 10-7=3
Product Three Stock = 10-7=3为了实现这一点,我在一个选项中设置了所有类似的产品,比如12,45,133 (产品ID),用逗号ANd分隔,这是公共数量的一个选项字段。
我的密码,我正在尝试的
add_action( 'woocommerce_thankyou', 'set_dynamic_quantity');
function set_dynamic_quantity_having_product_id_in_order($order_id){
$order = wc_get_order( $order_id );
$items = $order->get_items();
//getting options value
$similar_product_options = get_option( 'similar_product_options_name' ); // Array of All Options
$vpm_similar_product = $similar_product_options['vpm_similar_product']; //get similar product
$vpm_similar_product_qty = $similar_product_options['vpm_similar_product_qty']; //get dynamic qty = 100
$similar_id = explode( ",", $vpm_similar_product );
//auto update qty if product in order
if(in_array( $order->get_items() , $similar_id )){
foreach ( $items as $item => $value ) {
$quantity = $item->get_quantity();
$new_qty = $vpm_similar_product_qty - $quantity;
//set new quantity to all similar product set in
$vpm_similar_product_qty['vpm_similar_product_qty'] = $new_qty;
foreach ( $similar_id as $item_id => $item_data ) {
update_meta_data( '_qty', $new_qty, $item_id );
update_option('vpm_similar_product_qty',$same_type_product);
}
}
}
}发布于 2019-09-18 20:12:34
我假设$vpm_similar_product_qty是一个逗号分隔的产品ID。我还假设您只需使用options数组通过update_option更新此值。
这里提出的逻辑是:
这是一个提供的解决方案:
add_action( 'woocommerce_thankyou', 'set_dynamic_quantity_having_product_id_in_order');
function set_dynamic_quantity_having_product_id_in_order($order_id){
$order = new WC_Order( $order_id );
# This protect from double reduction on the same order
if (get_post_meta($order->get_id(), 'shared_products_quantities_updated', true)) return;
$similar_product_options = get_option( 'similar_product_options_name' ); // Array of All Options
$vpm_similar_product = $similar_product_options['vpm_similar_product']; //get similar product
$shared_stock_products = explode( ",", $vpm_similar_product );
// Local Testing $shared_stock_products = array(48, 45, 41);
# First time, set stock level
if (!get_option('vpm_similar_product_qty')) update_option('vpm_similar_product_qty', 100);
# Get Shared stock level
$vpm_similar_product_qty = get_option('vpm_similar_product_qty');
# Count how many twin products bought
$reduce_quantity = 0;
foreach ($order->get_items() as $key => $item){
if ( in_array($item['product_id'], $shared_stock_products ) ){
$reduce_quantity += $item->get_quantity();
}
}
# Sync stock levels according to the shared stock level setting
$updated_quantity = $vpm_similar_product_qty - $reduce_quantity;
foreach ($shared_stock_products as $product_id){
$product = new WC_Product($product_id);
$product->set_stock_quantity( $updated_quantity );
$product->save();
}
# Prevent duplicate qty deduction
add_post_meta($order->get_id(), 'shared_products_quantities_updated', true);
# Sync back the quantity to the shared quantity
update_option('vpm_similar_product_qty', $updated_quantity);
}--有一个问题你没有报道:
共享股票为10。两种“孪生”产品在购物车中,一种是8的,一种是5的。这将产生一个负的股票价值。但是,由于这个问题不在这个范围之内,我将不谈这个问题。
https://stackoverflow.com/questions/57952526
复制相似问题