Thêm chi phí bổ sung để vận chuyển tỷ lệ cố định cho mỗi 2 mặt hàng từ danh mục cụ thể trong WooCommerce

Jan 16 2021

Dựa trên Thêm chi phí bổ sung để vận chuyển theo tỷ lệ cố định cho mỗi 3 mặt hàng trong mã câu trả lời của Woocommerce , tôi đã thực hiện một số thay đổi để thêm một khoản chi phí bổ sung vào phương thức vận chuyển cố định là 2 mặt hàng (thay vì mỗi 3 mặt hàng) và chỉ khi có duy nhất các mặt hàng từ một danh mục cụ thể (ở đây là danh mục "Áo phông").

Đây là lần thử mã của tôi:

// add X amount to shipping for every 2 items added to the cart (flat rate only)
add_filter('woocommerce_package_rates', 'shipping_additional_cost_each_three_items', 100, 2);
function shipping_additional_cost_each_three_items( $rates, $package ){
    if ( is_admin() && ! defined( 'DOING_AJAX' ) )
        return $rates; // HERE set your additional shipping cost $additional_cost = 8.40;
    $items_count = WC()->cart->get_cart_contents_count(); // Define/replace here your correct category slug (!) $product_category = 't-shirts';
    $prod_cat = false; // Going through each item in cart to see if there is anyone of your category foreach ( WC()->cart->get_cart() as $cart_item ) {
        $product_id = $cart_item['product_id'];
        if ( has_term( $product_category, 'product_cat', $product_id ) ){
            $prod_cat = true; } } // Loop through the shipping taxes array foreach ( $rates as $rate_key => $rate ){
        $has_taxes = false; // Targetting "flat rate" if( 'flat_rate' === $rate->method_id ){

            // Get the initial cost
            $initial_cost = $new_cost = $rates[$rate_key]->cost;

            // Adding the additional cost if product in T-Shirt category after every 2 items (3, 6, 9 …)
            if ( $prod_cat ) { for($i = 0; $i <= $items_count; $i+=3){ $new_cost += $additional_cost; } } // Set the new cost $rates[$rate_key]->cost = $new_cost;

            // Taxes rate cost (if enabled)
            $taxes = []; // Loop through the shipping taxes array (as they can be many) foreach ($rates[$rate_key]->taxes as $key => $tax){ if( $rates[$rate_key]->taxes[$key] > 0 ){
                    // Get the initial tax cost
                    $initial_tax_cost = $new_tax_cost = $rates[$rate_key]->taxes[$key]; // Get the tax rate conversion $tax_rate    = $initial_tax_cost / $initial_cost;

                    // Set the new tax cost
                    $taxes[$key] = $new_cost * $tax_rate;
                    $has_taxes = true; // Enabling tax } } } if( $has_taxes )
            $rates[$rate_key]->taxes = $taxes; return $rates;
    }
}

Đối với phương thức giao hàng đã chọn "Tỷ lệ cố định", mã chỉ hoạt động tốt khi các mặt hàng từ danh mục "áo thun" có trong giỏ hàng. Nhưng nếu có một mục không thuộc danh mục "áo phông", tỷ lệ sẽ biến mất và hiển thị tiêu đề mà không có số lượng.

Ai đó có thể cho tôi biết nơi tôi phải đặt điều kiện 'nếu danh mục là X', để làm cho mã đó hoạt động không?

Trả lời

1 LoicTheAztec Jan 16 2021 at 14:15

Có một số lỗi trong mã của bạn (như return $rates;vậy cần phải có ngay trước dấu ngoặc đóng cuối cùng). Hãy thử mã được truy cập lại sau đây để thay thế:

add_filter('woocommerce_package_rates', 'shipping_additional_cost_each_three_items', 100, 2);
function shipping_additional_cost_each_three_items( $rates, $package ){ if ( is_admin() && ! defined( 'DOING_AJAX' ) ) return $rates;

    // HERE set your additional shipping cost
    $additional_cost = 8.40; $each_items      = 2; // Number of items (for additional cost)

    // Her set your category(ies) (can be term Ids slugs or names)
    $product_categories = array('t-shirts'); $items_cat_count    = 0; // Initializing

    // Loop through cart items for the current shipping package        
    foreach( $package['contents'] as $cart_item ) {
        if ( ! has_term( $product_categories, 'product_cat', $cart_item['product_id'] ) ){
            $items_cat_count += $cart_item['quantity']; // Count items from defined category
        }
    }
    
    if ( $items_cat_count >= $each_items ) {
       // Loop through the shipping taxes array
        foreach ( $rates as $rate_key => $rate ){ // Targetting "flat rate" if( 'flat_rate' === $rate->method_id ){
                $initial_cost = $new_cost = $rate->cost; // Get the initial cost $has_taxes    = false; // Initializing
                $taxes = array(); // Initializing // Adding to cost the additional cost each 2 items (2, 4, 6 …) for($i = 0; $i <= $items_cat_count; $i += $each_items){
                    $new_cost += $additional_cost;
                }
                $rates[$rate_key]->cost = $new_cost; // Set the new cost // Taxes rate cost (if any) - Loop through taxes array (as they can be many) foreach ($rate->taxes as $key => $tax){
                    if( $tax > 0 ){ // Get the initial tax cost $initial_tax_cost = $new_tax_cost = $tax;
    
                        // Get the tax rate conversion
                        $tax_rate = $initial_tax_cost / $initial_cost; // Set the new tax cost in the array $taxes[$key] = $new_cost * $tax_rate; $has_taxes   = true; // Enabling tax changes
                    }
                }
                // set array of shipping tax cost
                if( $has_taxes ) { $rates[$rate_key]->taxes = $taxes; 
                }
            } 
        }
    }
    return $rates;
}

Mã nằm trong tệp functions.php của chủ đề con đang hoạt động (hoặc chủ đề hoạt động). Nó sẽ hoạt động.

Đừng quên làm trống giỏ hàng của bạn để làm mới dữ liệu vận chuyển đã lưu trong bộ nhớ cache.