如何在WooCommerce中为类似产品设置和更新动态库存

时间:2019-09-16 07:54:05

标签: php wordpress woocommerce

问题:我有3个产品类似类型或相同(同一个产品,但名称不同)。如果这些产品应具有相同的库存。我不确定是否可以?

例如:

Product One Stock= 10
Product Two Stock= 10
Product Three Stock= 10

目标要为这些产品设置/更新相同库存

结果1:产品1数量为5,以便新更新库存

Product One Stock = 10-5 =5
Product Two Stock  = 10-5=5
Product Three Stock = 10-5=5

结果2:产品1的数量为5,产品2的数量为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),并以逗号分隔 和“普通数量”的选项字段。

我的代码我正在尝试

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);
            }
        }
    }
}

1 个答案:

答案 0 :(得分:-2)

我假设$vpm_similar_product_qty是用逗号分隔的产品ID。我还假设您仅使用update_option和options数组来更新此值。

此处显示的逻辑:

  1. 循环浏览订单项
  2. 对于每个订单商品,检查它是否在“双胞胎”列表中
  3. 累计订购数量
  4. 从同步数量设置值中减少总量
  5. 循环“扭曲”并相应地更新其数量

这是提供的解决方案:

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。这将产生负库存值。但是由于此问题不在此范围内,因此我将不予解决。