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

问题描述 投票:1回答:2

我已经在这里找到了这个代码,它适用于我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
2个回答
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的费用。


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');

© www.soinside.com 2019 - 2024. All rights reserved.