在Woocommerce中为特定产品类别添加费用

时间:2018-06-26 14:16:16

标签: php wordpress woocommerce cart fee

我已经在这里找到了此代码,对我来说它的工作效率约为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' );

2 个答案:

答案 0 :(得分:0)

您使用的代码有些过时,您应该使用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的费用。

答案 1 :(得分:0)

我找到了以下解决方案。请注意,由于我是从上一个代码段开始的,所以我会不断增加费用。您可以根据需要应用现有折扣或计算其他总计。 在这种情况下,我会根据购物车总价值> = 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');