簡體   English   中英

在 Woocommerce 中為特定產品類別添加費用

[英]Add a fee for a specific product category in Woocommerce

我已經在這里找到了這段代碼,它適用於我的 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' );

您使用的代碼有點過時,您應該使用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 的費用。

我找到了如下解決方案。 請注意,我一直在添加費用,因為我從之前的代碼片段開始。 您可以根據需要應用現有折扣或計算不同的總計。 在這種情況下,我根據購物車的 >= 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');

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM