【发布时间】:2018-09-15 17:58:56
【问题描述】:
我有一个使用 Woocommerce“可变产品”的设置,其中唯一的变化是“尺寸”属性:15 克、100 克、250 克。我想要做的是使用该变化量传递给 Woo wc-stock-functions,这样当购买产品变化“15 克”时,总库存下降 15,而不是 1。
在 Woo 内部,有文件 wc-stock-functions (http://hookr.io/plugins/woocommerce/3.0.6/files/includes-wc-stock-functions/) - 这甚至提供了一个过滤器 woocommerce_order_item_quantity。我想用它来将库存数量乘以克数,并以这种方式减少库存。
我正在尝试这个:
// define the woocommerce_order_item_quantity callback
function filter_woocommerce_order_item_quantity( $item_get_quantity, $order,
$item ) {
$original_quantity = $item_get_quantity;
$item_quantity_grams = $item->get_attribute('pa_size');
// attribute value is "15 grams" - so remove all but the numerals
$item_quantity_grams = preg_replace('/[^0-9.]+/', '', $item_quantity_grams);
// multiply for new quantity
$item_get_quantity = ($item_quantity_grams * $original_quantity);
return $item_get_quantity;
};
// add the filter
add_filter( 'woocommerce_order_item_quantity',
'filter_woocommerce_order_item_quantity', 10, 3 );
但我现在收到内部服务器错误作为响应。
有人知道我在上面的代码中做错了什么吗?感谢您的帮助。
【问题讨论】:
标签: php wordpress woocommerce orders stock