programing

Woocommerce의 각 3개 품목을 균일 요금으로 배송하는 데 추가 비용을 추가합니다.

telebox 2023. 6. 21. 22:31
반응형

Woocommerce의 각 3개 품목을 균일 요금으로 배송하는 데 추가 비용을 추가합니다.

저는 우커머스 숍을 운영하고 있으며 균일 요금 배송을 사용하고 있습니다.$15추가할 공식을 작성했습니다.$1.25각 추가 항목에 대해 입력합니다.

13.50 + (1.25 * [수량])

홀핑 "고정 속도 설정 |$1.25추가 각 항목:

$1.25 for Additional Each Item

하지만 이 비용을 추가하고 싶습니다.$1.25매 3개 항목에 대해그러니까 3, 6, 9, 12 등등...

누가 이걸 어떻게 하는지 알려줄 수 있나요?어떤 도움이든 감사합니다.

업데이트됨 (2021)

다음 코드는 각 3개 항목(3, 6, 9 등)의 균일 요금 배송 방법에 추가 비용을 추가합니다.

공식 대신 간단한 초기 비용으로 배송비를 변경해야 합니다.

임시로 캐시를 발송하지 않으려면 일반 발송 설정의 "배송 옵션" 탭에서 "디버깅 모드 사용"을 선택해야 할 수 있습니다.

코드(추가 배송비를 설정할 위치):

add_filter('woocommerce_package_rates', 'shipping_additional_cost_each_three_items', 10, 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 = 1.25;
    $items_count     = 0; // Initializing
    $each_items      = 3; // Number of items (for additional cost)

    // Loop through cart items for the current shipping package        
    foreach( $package['contents'] as $cart_item ) {
        $items_count = += $cart_item['quantity']; // Count cart items for current shipping package
    }

    if ( $items_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;
                $has_taxes    = false; // Initializing
                $taxes        = array(); // Initializing
                
                // Adding to cost the additional cost each 3 items (3, 6, 9 …)
                for($i = 0; $i <= $items_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
                        $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;
}

코드가 작동합니다.활성 하위 테마(또는 활성 테마)의 php 파일입니다.테스트를 거쳐 작동합니다.

발송 설정에서 "디버깅 모드 사용" 옵션을 사용하지 않도록 설정하는 것을 잊지 마십시오.


당신의 두 번째 의견에 따른 답변:

이 블록을 교체합니다.

// Adding to cost the additional cost each 3 items (3, 6, 9 …)
for($i = 0; $i <= $items_count; $i += $each_items){
    $new_cost += $additional_cost;
}

다음 기준으로:

// Adding to cost an additional fixed cost for the 2nd item
if($items_count >= 2){
    $new_cost += 6.21; 
}

// Adding to cost the additional cost each 3 items (3, 6, 9 …)
for($i = 0; $i <= $items_count; $i += $each_items){
    $new_cost += $additional_cost;
}
<?php
$number = 13;
$all_number = 1;
for($i=1;$i<=13;$i++){
    if ($i % 3 == 0){
        $all_number = $all_number + 1;
    }
}
$price_new_data = $old_price*$all_number;
$price = 13.50+($price_new_data*$product_quenty);
?>

언급URL : https://stackoverflow.com/questions/51165313/add-an-additional-cost-to-flat-rate-shipping-each-3-items-in-woocommerce

반응형