Good day,
I have a product with 4 variations (variations made from the ‘size’ attribute): Small, Medium, Large and Full Set (all 3 sizes). People can choose to buy only a single size or buy a set that has all 3 sizes (prices differ for all 4 variations).
I only have stock management (with backorders allowed) selected for the 3 size variations (small, medium, large) and for the Set variation I have only selected the stock status to be ‘in stock’. I need to automatically change the Set variation stock status based on the stock quantities of the size variations.
When I have 1 Small, 1 Medium and 1 Large, I obviously also have a Set in stock. But, for instance, when someone buys the small one, then I do not have a full Set anymore. I would like to automatically update the stock status of the Set variation to ‘on backorder’. Also, the other way around would be, when someone buys a full Set to automatically update the stock quantities of Small, Medium & Large with -1.
SMALL - 1x in stock
MEDIUM - 1x in stock
LARGE - 1x in stock
FULL SET - stock status: 'in stock' (only 'in-stock' when there is at least 1 of each; small, medium, large)
When someone buys a full set:
SMALL - 1x in stock -1 = 0x in stock
MEDIUM - 1x in stock -1 = 0x in stock
LARGE - 1x in stock -1 = 0x in stock
FULL SET - stock status: 'on backorder' (only 'in-stock' when there is at least 1 of each; small, medium, large)
All of the products make use of these variations and they will not change.
Dummy code will probably be something like (this will in no way work, I was just trying to put something together from a lot of internet searches):
To set the Full Set stock status:
function fullset_custom_stockstatus (){
global $product;
// Get the available variations
$available_variations = $product->get_available_variations();
// Get the term slugs
$attribute_slug = $values['attributes']['attribute_pa_headcover-size'];
$wp_term = get_term_by( 'slug', $attribute_slug, 'pa_headcover-size' );
$term_slug = $wp_term->slug; // Headcover Size Slug
// Get the variation quantity
$variation_obj = wc_get_product( $values['variation_id'] );
$stock_qty = $variation_obj->get_stock_quantity(); // Stock qty
//Seperate variation stock data
$var_small = $term_slug['small'].$stock_qty;
$var_medium = $term_slug['medium'].$stock_qty;
$var_large = $term_slug['large'].$stock_qty;
$var_fullset = $term_name['full-set'].$stock_status;
//Check if each variation stock is 1 or more
if ($var_small == 0 || $var_medium == 0 || $var_large == 0){
$var_fullset = 'on-backorder';
}
else{
$var_fullset = 'in-stock';
}
}
add_action( 'woocommerce_order_item_quantity', 'fullset_custom_stockstatus');
To update the variation quantities when a full set was bought:
function update_stockqty_after_fullset_order (){
//Get the order information
$order = wc_get_order( $order_id );
foreach( $order->get_items() as $item ){
$order_product_id = $item->get_product_id();
$order_variation_id = $item->get_variation_id();
//Get the order variation information
$order_variation = wc_get_product($order_variation_id);
$order_variation_attribs = $order_variation->get_variation_attributes();
//Check if order variation is 'full-set'
if ($order_variation_attribs !== 'full-set' ){
//Get the order product variation information
$product = wc_get_product( $order_product_id );
$variations = $product->get_available_variations();
// Get the term slugs
$attribute_slug = $values['attributes']['attribute_pa_headcover-size'];
$wp_term = get_term_by( 'slug', $attribute_slug, 'pa_headcover-size' );
$term_slug = $wp_term->slug; // Headcover Size Slug
// Get the variation quantity
$variation_obj = wc_get_product( $values['variation_id'] );
$stock_qty = $variation_obj->get_stock_quantity(); // Stock qty
//Seperate variation stock data
$var_small = $term_slug['small'].$stock_qty;
$var_medium = $term_slug['medium'].$stock_qty;
$var_large = $term_slug['large'].$stock_qty;
//Update the stock quanities
wc_update_product_stock( $var_small, 'decrease' );
wc_update_product_stock( $var_medium, 'decrease' );
wc_update_product_stock( $var_large, 'decrease' );
// Clear/refresh the variation cache (optionally if needed)
wc_delete_product_transients($variation['variation_id']);
} else {
return;
}
}
}
add_action( 'woocommerce_order_status_processing', 'update_stockqty_after_fullset_order');
Any help would be highly appreciated.
2
Answers
Thanks to @Vdgaetano's code I could have get the code to fit my needs. Thank you again @Vdgaetano - you are awesome! :-)
You can use the
woocommerce_order_status_processing
hook which is activated when the order status is changed to wc-processing.UPDATED
The "full-set" variation is updated only if:
In this way the stock quantity of the "full-set" variation will be equal to the minimum salable quantity based on the stock quantities of the "small", "medium" and "large" variations.
When the stock quantity of the "full-set" variation is less than or equal to zero, it enables back orders.
I have tested the code and it works. The code goes into your theme’s functions.php.