在 WooCommerce 中根据产品类别和产品长度申请费用

Apply fee based on product category and product length in WooCommerce

我正在尝试根据产品类别和产品长度收取费用。

如果产品属于某个类别并且长度小于 30m 我想收取每个产品的切割价格费用(切割地板卷的成本)。

我有一个功能可以将降价添加到某个类别,但我似乎无法弄清楚如何使其成为有条件的,所以它只在商品长度小于 30 米时才添加费用

请看下面我试过的两个版本:

函数 1: 下面的代码在没有长度条件的情况下工作,因此会向购物车中具有特定类别的每个项目添加费用

// Add cut price to category flotex
function woo_add_cart_fee() {

$category_ID = '83'; // Flotex Category is 83
global $woocommerce;
$cpfee = 0.00; // initialize special fee

//Getting Cart Contents. 
$cart = $woocommerce->cart->get_cart();

//Calculating Quantity in cart
foreach($cart as $cart_val => $cid){
   $qty += $cid['quantity']; 
}

foreach ($woocommerce->cart->cart_contents as $key => $values ) {
    // Get the terms, i.e. category list using the ID of the product
    $terms = get_the_terms( $values['product_id'], 'product_cat' );
        // Because a product can have multiple categories, we need to iterate through the list of the products category for a match
        foreach ($terms as $term) {
            // 83 is the ID of the category for which we want to remove the payment gateway
            if($term->term_id == $category_ID){
                $cutprice = 30;
            }
            $cpfee = $qty * $cutprice;
        }
        $woocommerce->cart->add_fee('Cut Price', $cpfee, $taxable = true, $tax_class = 'standard');
    }
}
add_action( 'woocommerce_cart_calculate_fees', 'woo_add_cart_fee' );

?>

函数 2: 下面的代码试图添加条件长度规则,但它不起作用。即使长度为 30

,它总是 returns 为每件商品收取 0 或 30 的费用
// Add cut price to category flotex
function woo_add_cart_fee() {

    $category_ID = '83'; // Flotex Category is 83
    global $woocommerce;
    $cpfee = 0.00; // initialize special fee
    $qty = 0;
    $cutpricesmall = 0;

    //Getting Cart Contents. 
    $cart = $woocommerce->cart->get_cart();

    //Calculating Quantity in cart
    foreach($cart as $cart_val => $cid){
       $qty += $cid['quantity']; 
    }

    foreach ($woocommerce->cart->cart_contents as $key => $values ) {

        $product = $values['data'];
        $length = $product->get_length();

        // Get the terms, i.e. category list using the ID of the product
        $terms = get_the_terms( $values['product_id'], 'product_cat' );
        
        if ( $length < 29 ) {
            // Because a product can have multiple categories, we need to iterate through the list of the products category for a match
            foreach ($terms as $term) {
                
                // 83 is the ID of the category for which we want to remove the payment gateway
                if($term->term_id == $category_ID){
                    
                        $cutprice = 30;
                }
            }

        } elseif ( $length > 29 ){
            foreach ($terms as $term) {
            
                // 83 is the ID of the category for which we want to remove the payment gateway
                if($term->term_id == $category_ID){
                    
                    $cutpricesmall = 0;
                }
            }
        }
        $cpfee = $qty * ($cutprice + $cutpricesmall);
        $woocommerce->cart->add_fee('Cut Price', $cpfee, $taxable = true, $tax_class = 'standard');
    
    }
    
}
add_action( 'woocommerce_cart_calculate_fees', 'woo_add_cart_fee' );

如有任何建议,我们将不胜感激。

您的代码有一些错误或可以优化:

  • 不需要使用全局$woocommerce;
  • get_the_terms() 替换为 has_term()
  • 在您的第二次代码尝试中,您使用了 4 个 foreach 循环,而 1 个循环就足够了
  • 您可以在循环中应用 if/else 条件,因此您不必将所有内容都检查两次

所以你得到:

function action_woocommerce_cart_calculate_fees( $cart ) {
    if ( is_admin() && ! defined( 'DOING_AJAX' ) )
        return;
    
    // Specific categories: the term name/term_id/slug. Several could be added, separated by a comma
    $categories = array( 83, 'categorie-1' );

    // Settings
    $cut_price = 30;
    $length = 30;

    // Initialize
    $cp_fee = 0;
     
    // Gets cart contents
    foreach ( $cart->get_cart_contents() as $cart_item ) {
        // Has certain category     
        if ( has_term( $categories, 'product_cat', $cart_item['product_id'] ) ) {
            // Get length
            $product_length = $cart_item['data']->get_length();

            // NOT empty and less than
            if ( ! empty ( $product_length ) && $product_length < $length ) {
                // Get quantity
                $quantity = $cart_item['quantity'];
            
                // Addition to the total
                $cp_fee += $cut_price * $quantity;
            }
        }
    }

    // Greater than
    if ( $cp_fee > 0 ) {
        // Add fee
        $cart->add_fee( __( 'Cut Price', 'woocommerce' ), $cp_fee, true );
    }
}
add_action( 'woocommerce_cart_calculate_fees', 'action_woocommerce_cart_calculate_fees', 10, 1 );

注意:如果每个产品的产品数量不应该考虑

替换:

// NOT empty and less than
if ( ! empty ( $product_length ) && $product_length < $length ) {
    // Get quantity
    $quantity = $cart_item['quantity'];

    // Addition to the total
    $cp_fee += $cut_price * $quantity;
}

有:

// NOT empty and less than
if ( ! empty ( $product_length ) && $product_length < $length ) {    
    // Addition to the total
    $cp_fee += $cut_price;
}