【问题标题】:Add a fee for a specific product category in Woocommerce在 Woocommerce 中为特定产品类别添加费用
【发布时间】:2018-06-26 14:16:15
【问题描述】:

我已经在这里找到了这段代码,它对我来说是 80/90%(见下文)。

当购物车中有类别 ID 349 的产品时,此代码将 60 欧元添加到我的购物车中。当我在购物车为空时将该类别的产品添加到我的购物车时,它工作正常。但是,当我的购物车中已经有来自不同类别的产品,然后我添加类别为 349 的产品时,它不会增加 60 欧元的额外费用。这怎么可能?

 function woo_add_cart_fee() {

$category_ID = '349';
global $woocommerce;

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) {
        // 349 is the ID of the category for which we want to remove the payment gateway
        if($term->term_id == $category_ID){
         $excost = 60;
         }
         }
        $woocommerce->cart->add_fee('Extra bezorgkosten kunstgras', $excost, $taxable = false, $tax_class = '');
}
}
add_action( 'woocommerce_cart_calculate_fees', 'woo_add_cart_fee' );

【问题讨论】:

    标签: php wordpress woocommerce cart fee


    【解决方案1】:

    您使用的代码有点过时,您应该使用has_term() Wordpress 条件函数以这种方式定位产品类别:

    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('349');
        $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 = 60;
        }
    
        // Adding the fee
        if ( $fee_amount > 0 ){
            // Last argument is related to enable tax (true or false)
            WC()->cart->add_fee( __( "Extra bezorgkosten kunstgras", "woocommerce" ), $fee_amount, false );
        }
    }
    

    代码进入您的活动子主题(或活动主题)的 function.php 文件中。经过测试并且可以工作。

    如果购物车中有来自 349 产品类别 ID 的商品,将始终添加 60 的费用。

    【讨论】:

    • 是的,我从 2014 年得到的帖子是我猜的。谢谢这对我有用!
    【解决方案2】:

    我找到了如下解决方案。请注意,我一直在添加费用,因为我是从以前的 sn-p 开始的。您可以根据需要应用现有折扣或计算不同的总计。 在这种情况下,我会根据购物车的总价值 >= 300 应用负费用(如折扣),其值仅为 local_pickup 运输方式的 -25%,另外计算税费。

    function discount_to_cat(){
    // thanks to LoicTheAztec's snippet
    $cat_in_cart = false;
    // Loop through all products in the Cart        
    foreach ( WC()->cart->get_cart() as $cart_item ) {
    
        if ( has_term( 'category_to_pick', 'product_cat', $cart_item['product_id'] ) ) {
            $cat_in_cart = true;
            $tot_category_price = $cart_item['data']->get_price();
            $tot_category_qty = $cart_item['quantity'];
            $tot_category =  $tot_category_price * $tot_category_qty;
            break;
        }
    }   global $product;
        $total = WC()->cart->subtotal;
        $discount_label = "";
        if($total >= 300){
            $discount_label=15;     
    }
        $chosen_methods = WC()->session->get( 'chosen_shipping_methods' );
        $chosen_shipping = explode(':',$chosen_methods[0]);
    
        if($chosen_shipping[0]=='local_pickup'){
            $discount_label=25;
        }
        $discount_applied = ($total-$tot_category)*$discount_label/100;
        if($discount_label!=""){
            $discount_applied_net = ($discount_applied/1.1); //1.1 according taxes for shipping
            WC()->cart->add_fee( "Discount applied: ($discount_label%)", -$discount_applied_net, false );
        }   
    }
    

    add_action('woocommerce_cart_calculate_fees','discount_to_cat');

    【讨论】:

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