【问题标题】:Add a WooCommerce function for a particular category为特定类别添加 WooCommerce 功能
【发布时间】: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


    【解决方案1】:

    is_product_category 函数仅适用于存档页面。为了实现您的目标,您可以在 if 语句包裹的存档页面中添加过滤器。

    您可以在存档 woocommerce 页面中使用以下代码:

    if(is_product_category('apple-phones')) {
     add_filter('woocommerce_quantity_input_min', 'min_decimal');
     add_filter('woocommerce_quantity_input_step', 'nsk_allow_decimal');
     // 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);
    }
    

    还有functions.php:

    function min_decimal($val) {
        return 0.5;
    }
    // Add step value to the quantity field (default = 1)
    function nsk_allow_decimal($val) {
        return 0.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;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-04-08
      • 2018-06-07
      • 1970-01-01
      • 1970-01-01
      • 2018-03-10
      • 2022-10-25
      • 1970-01-01
      相关资源
      最近更新 更多