【问题标题】:Add a fee in WooCommerce for a specific product category, but only when related categories also present在 WooCommerce 中为特定产品类别添加费用,但仅当相关类别也存在时
【发布时间】:2022-01-03 16:58:36
【问题描述】:

我想为类别 (A) 添加费用,但仅当订单中有其他类别(B、C、D 等)的产品时才计算。

但如果只订购 A 类产品,则不适用该税。

在我的代码中,这两种情况都会增加费用。您能指导我找到更好的解决方案吗?

我在我的网站上添加此代码:

add_action( 'woocommerce_cart_calculate_fees','custom_pcat_fee', 20, 1 );
function custom_pcat_fee( $cart ) {
    if ( is_admin() && ! defined( 'DOING_AJAX' ) )
        return;

    // Set HERE your categories (can be term IDs, slugs or names) in a coma separated array
    $categories = array('396');
    $fee_amount = 0;

    // Loop through cart items
    foreach( $cart->get_cart() as $cart_item ){
        if( has_term( $categories, 'product_cat', $cart_item['product_id']) )
            $fee_amount = 20;
    }

    // Adding the fee
    if ( $fee_amount > 0 ){
        // Last argument is related to enable tax (true or false)
        WC()->cart->add_fee( __( "Taxa livrare ROPET", "woocommerce" ), $fee_amount, false );
    }
}

【问题讨论】:

    标签: php wordpress woocommerce hook-woocommerce fee


    【解决方案1】:

    您可以使用wp_get_post_terms() 检索帖子的条款。在浏览购物车内容时,我们会收集术语 ID, 然后我们可以比较看看是否匹配。

    使用的WordPress函数:

    使用的 PHP 函数:

    所以你得到:

    function action_woocommerce_cart_calculate_fees( $cart ) {
        if ( is_admin() && ! defined( 'DOING_AJAX' ) )
            return;
        
        // Only use term IDs
        $category_a = 15;
        $other_categories = array( 16, 17, 18 );
        
        // Fee amount
        $fee_amount = 20;
        
        // Initialize
        $term_ids = array();
        
        // Loop through cart contents
        foreach ( $cart->get_cart_contents() as $cart_item ) {      
            // Get product id
            $product_id = $cart_item['product_id'];
            
            // Retrieves the terms for a post.
            $terms = wp_get_post_terms( $product_id, 'product_cat', array( 'fields' => 'ids' ) );
            
            // Loop through
            foreach ( $terms as $term_id ) {
                // Checks if a value NOT exists in an array
                if ( ! in_array( $term_id, $term_ids ) ) {
                    // Push
                    $term_ids[] = $term_id;
                }   
            }
        }
        
        // Check for a specific category (A)
        if ( in_array( $category_a, $term_ids ) ) {
            // Check if ANY of the term ids exist
            if ( ! empty ( array_intersect( $other_categories, $term_ids ) ) ) {            
                // Last argument is related to enable tax (true or false)
                $cart->add_fee( __( 'Taxa livrare ROPET', 'woocommerce' ), $fee_amount, false );
            }
        }
    }
    add_action( 'woocommerce_cart_calculate_fees', 'action_woocommerce_cart_calculate_fees', 10, 1 );
    

    【讨论】:

    • 感谢您的回复!但我无法得到我想要的。所以我希望当 A 类产品(仅 A 类产品)在购物车中时,不添加费用。但是当在购物篮中添加其他类别的产品时,需要添加费用。谢谢!
    • @DobosDaniel 在您的问题中,您说:“我想为一个类别 (A) 添加费用,但仅当订单中有其他类别的产品时才计算 (B, C、D 等)。” - 这正是我的回答所做的,现在你似乎在描述/问另一个问题。但是,我无意在之后更改您在 Stack Overflow 上的问题,所以我必须再次更改我的答案...
    猜你喜欢
    • 1970-01-01
    • 2018-11-09
    • 2013-08-11
    • 1970-01-01
    • 1970-01-01
    • 2018-06-07
    • 2023-04-08
    • 1970-01-01
    • 2016-07-16
    相关资源
    最近更新 更多