亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何在woocommerce上為特殊產品申請運費折扣?

如何在woocommerce上為特殊產品申請運費折扣?

PHP
LEATH 2021-11-13 16:00:38
特殊產品的運費有任何折扣嗎?所有美國運費為 10.00 美元,但如果產品在購物車中,運費將為 3.00 美元我已經使用優惠券代碼嘗試過。如何查看賬單/發貨國家/地區?但它對總成本設置了折扣,但我需要對運費進行折扣,我也使用不同的運輸類別。我正在嘗試什么。function shipping_costs_discounted( $rates, $package ){ if ( is_admin() && ! defined( 'DOING_AJAX' ) )    return $rates;foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {        $product_id_in_cart = array( 1391193 );// this is product ID        if( in_array( $cart_item['product_id'], $product_id_in_cart ) ) {               //Looking for the shipping classes            $shipping_class = 'special-offer-discount'; // <=== Shipping class slug            $discount_rate  = 1.46;            $is_found       = false;            // Loop through cart items and checking for the specific defined shipping class            foreach( $package['contents'] as $cart_item ) {                if( $cart_item['data']->get_shipping_class() == $shipping_class )                    $is_found = true;            }            // Set shipping costs shipping class is found            if( $is_found ){                foreach ( $rates as $rate_key => $rate ){                    $has_taxes = false;                    // Targeting "flat rate"                    if( 'flat_rate' === $rate->method_id  ){                        $rates[$rate_key]->cost = $rate->cost - $discount_rate;                        // Taxes rate cost (if enabled)                        foreach ($rates[$rate_key]->taxes as $key => $tax){                            if( $tax > 0 ){                                $has_taxes = true;                                $taxes[$key] = $tax - $discount_rate;                            }                        }                        if( $has_taxes )                            $rates[$rate_key]->taxes = $taxes;                    }                }            }            return $rates;        }    }}add_filter('woocommerce_package_rates', 'shipping_costs_discounted', 10, 2);
查看完整描述

1 回答

?
慕哥6287543

TA貢獻1831條經驗 獲得超10個贊

假設您將修改woocommerce_package_rates過濾器,購物車中的所有商品都在key$package內的變量中contents,因此您可以簡單地遍歷它并提取所有 id 以檢查您想要的產品是否在其中。


目的地信息也在鍵$package里面的變量中destination,它包含完整的送貨地址,包括國家;


這是$package變量的json 輸出示例


{

    "contents": {

        "044a23cadb567653eb51d4eb40acaa88": {

            "key": "044a23cadb567653eb51d4eb40acaa88",

            "product_id": 2754,

            "variation_id": 0,

            "variation": [],

            "quantity": 2,

            "data_hash": "b5c1d5ca8bae6d4896cf1807cdf763f0",

            "line_tax_data": {

                "subtotal": [],

                "total": []

            },

            "line_subtotal": 35.9,

            "line_subtotal_tax": 0,

            "line_total": 35.9,

            "line_tax": 0,

            "data": {}

        },

        "0fc170ecbb8ff1afb2c6de48ea5343e7": {

            "key": "0fc170ecbb8ff1afb2c6de48ea5343e7",

            "product_id": 2800,

            "variation_id": 0,

            "variation": [],

            "quantity": 1,

            "data_hash": "b5c1d5ca8bae6d4896cf1807cdf763f0",

            "line_tax_data": {

                "subtotal": [],

                "total": []

            },

            "line_subtotal": 107.95,

            "line_subtotal_tax": 0,

            "line_total": 107.95,

            "line_tax": 0,

            "data": {}

        }

    },

    "contents_cost": 143.85,

    "applied_coupons": [],

    "user": {

        "ID": 1

    },

    "destination": {

        "country": "US",

        "state": "",

        "postcode": "",

        "city": "Pine Bluff",

        "address": "",

        "address_1": "",

        "address_2": ""

    },

    "cart_subtotal": 143.85,

    "rates": {

        "flat_rate:3": {}

    }

}

所以有了這些信息,你可以做這樣的事情來修改運輸成本和運輸標簽


add_filter('woocommerce_package_rates', '_shipping_cost_product_ID', 100, 2);

function _shipping_cost_product_ID( $rates, $package ){


    // duh

    if ( is_admin() && ! defined( 'DOING_AJAX' ) )

        return $rates;


    // The ID of the product you want to modify shipping cost if found in cart

    $discounted_product_id = 2800;


    // Get all the product IDs added in cart and put them in array

    $ids_in_cart = [];

    foreach ( $package['contents'] as $key => $item ) {

        $ids_in_cart[] = $item['product_id'];

    }


    // loop through each shipping rates

    foreach ( $rates as $rate_key => $rate ){


        $has_taxes = false;


        // Only modify shipping if all conditions below are meet

        if( 

            'flat_rate' === $rate->method_id // if shipping method is "Flat RATE"

            && ( isset($package['destination']['country'] ) && $package['destination']['country'] == 'US' )  // if country is US

            && in_array($discounted_product_id, $ids_in_cart )//if $discounted_product_id is in cart

            ) {


            $rates[$rate_key]->label = 'Cool Shipping'; // Modify Shipping Name

            $rates[$rate_key]->cost = 3.00; // Modify Shiping Cost


            //Taxes rate cost (if enabled)

            $taxes = [];

            foreach ($rates[$rate_key]->taxes as $key => $tax){

                if( $rates[$rate_key]->taxes[$key] > 0 ){

                    $initial_tax_cost = $new_tax_cost = $rates[$rate_key]->taxes[$key];

                    $tax_rate    = $initial_tax_cost / $initial_cost;

                    $taxes[$key] = $new_cost * $tax_rate;

                    $has_taxes   = true; 

                }

            }

            if( $has_taxes ) $rates[$rate_key]->taxes = $taxes;

        }

    }

    return $rates;

}


查看完整回答
反對 回復 2021-11-13
  • 1 回答
  • 0 關注
  • 207 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號