Reputation: 3174
Problem : I have 3 product similar type or they are same(Same product with different name). If those product should have same Stock. I am not sure it possible or not?
For example:
Product One Stock= 10
Product Two Stock= 10
Product Three Stock= 10
Goal: Want to set/update Same Stock for those product if any of them in order
Result 1: Product One qty 5 in order so new updated Stock
Product One Stock = 10-5 =5
Product Two Stock = 10-5=5
Product Three Stock = 10-5=5
Result 2: Product One qty 5 and Product Two qty 2 in order So total qty of similar product =5+2=7 so new updated stock
Product One Stock = 10-7 =3
Product Two Stock = 10-7=3
Product Three Stock = 10-7=3
To Achieve this I set all similar product in a option like 12,45,133 (product ID) separated by comma ANd a option field for Common Quantity.
My Code what I am trying
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);
}
}
}
}
Upvotes: 4
Views: 902
Reputation: 2027
I assume $vpm_similar_product_qty
is a comma separated product IDs. I also assume that you update this value simply by update_option
with the options array.
The logic presented here:
This is an offered solution:
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);
}
There is one issue which you didn't cover though:
The shared stock is 10. Two "twin" products are in cart, one with qty of 8 and one with 5. This will produce a negative stock value. But since this issue is out of this scope, I will not address it.
Upvotes: -2