WooCommerce의 특정 카테고리에서 각 2 개 품목을 고정 요금으로 배송하는 데 추가 비용이 추가됩니다.
Jan 16 2021
우 커머스 답안 코드 에서 3 개 품목별 고정 요금 배송비 추가를 기준으로 2 개 품목 (각 3 개 품목이 아닌) 배송 방식에 별도의 배송비를 추가하도록 일부 변경하였습니다. 특정 카테고리 (여기서는 "T-Shirts"카테고리).
내 코드 시도는 다음과 같습니다.
// 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;
}
}
선택한 배송 방법 '정액'의 경우 '티셔츠'카테고리의 항목이 장바구니에있을 때 코드가 제대로 작동합니다. 하지만 "티셔츠"카테고리에 속하지 않는 항목이 있으면 요금이 사라지고 금액없이 제목이 표시됩니다.
누군가가 '범주가 X 인 경우'라는 조건을 어디에 두어야 해당 코드가 작동하는지 말해 줄 수 있습니까?
답변
1 LoicTheAztec Jan 16 2021 at 14:15
코드에 몇 가지 실수가 있습니다 ( return $rates;
마지막 닫는 대괄호 바로 앞에 있어야 함). 대신 다음 재검토 된 코드를 시도하십시오.
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;
}
코드는 활성 자식 테마 (또는 활성 테마)의 functions.php 파일에 들어갑니다. 작동합니다.
배송 캐시 데이터를 새로 고치려면 카트를 비우는 것을 잊지 마십시오.