【发布时间】:2020-07-23 12:20:05
【问题描述】:
我在主题的 functions.php 文件中使用以下代码将 WooCommerce 产品的数量增加 0.5 而不是 1。
add_filter('woocommerce_quantity_input_min', 'min_decimal');
function min_decimal($val) {
return 0.5;
}
// Add step value to the quantity field (default = 1)
add_filter('woocommerce_quantity_input_step', 'nsk_allow_decimal');
function nsk_allow_decimal($val) {
return 0.5;
}
// Removes the WooCommerce filter, that is validating the quantity to be an int
remove_filter('woocommerce_stock_amount', 'intval');
// Add a filter, that validates the quantity to be a float
add_filter('woocommerce_stock_amount', 'floatval');
// Add unit price fix when showing the unit price on processed orders
add_filter('woocommerce_order_amount_item_total', 'unit_price_fix', 10, 5);
function unit_price_fix($price, $order, $item, $inc_tax = false, $round = true) {
$qty = (!empty($item['qty']) && $item['qty'] != 0) ? $item['qty'] : 1;
if($inc_tax) {
$price = ($item['line_total'] + $item['line_tax']) / $qty;
} else {
$price = $item['line_total'] / $qty;
}
$price = $round ? round( $price, 2 ) : $price;
return $price;
}
但我希望代码只影响一个特定的产品类别,而不是一个。
所以,我在这段代码中抓取了上面的代码:
if( is_product_category( 'apple-phones' ) ) {
//Above code here
}
但它根本不起作用。但如果我只添加第一个代码,那么它适用于所有产品。
谁能帮我解决我做错了什么?
我希望上述代码仅适用于我的一个产品类别。
感谢您的回答。
【问题讨论】:
标签: php wordpress woocommerce categories