【发布时间】:2021-01-21 12:46:39
【问题描述】:
早安,
我的产品有 4 种变体(由“尺寸”属性制成的变体):小号、中号、大号和全套(全部 3 种尺寸)。人们可以选择只购买单一尺码或购买包含全部 3 种尺码的套装(所有 4 种款式的价格不同)。
我只为 3 种尺寸变体(小、中、大)选择了库存管理(允许延期交货),而对于设置变体,我只选择了库存状态为“有货”。我需要根据尺寸变化的库存数量自动更改设置变化库存状态。
当我有 1 个小号、1 个中号和 1 个大号时,我显然还有一套库存。但是,例如,当有人购买小的时,我就没有全套了。我想自动将 Set 变体的库存状态更新为“延期交货”。此外,另一种方法是,当有人购买完整的 Set 时,会自动将 Small、Medium 和 Large 的库存数量更新为 -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)
当有人购买全套时:
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)
所有产品都使用了这些变体,它们不会改变。
虚拟代码可能是这样的(这绝对行不通,我只是想从很多互联网搜索中收集一些东西):
设置全套库存状态:
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');
购买全套时更新变化数量:
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');
任何帮助将不胜感激。
【问题讨论】:
标签: php wordpress woocommerce