如何在woocommerce上为特殊产品申请运费折扣?

特殊产品的运费有任何折扣吗?


所有美国运费为 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);


LEATH
浏览 181回答 1
1回答

慕哥6287543

假设您将修改woocommerce_package_rates过滤器,购物车中的所有商品都在key$package内的变量中contents,因此您可以简单地遍历它并提取所有 id 以检查您想要的产品是否在其中。目的地信息也在键$package里面的变量中destination,它包含完整的送货地址,包括国家;这是$package变量的json 输出示例{&nbsp; &nbsp; "contents": {&nbsp; &nbsp; &nbsp; &nbsp; "044a23cadb567653eb51d4eb40acaa88": {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "key": "044a23cadb567653eb51d4eb40acaa88",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "product_id": 2754,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "variation_id": 0,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "variation": [],&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "quantity": 2,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "data_hash": "b5c1d5ca8bae6d4896cf1807cdf763f0",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "line_tax_data": {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "subtotal": [],&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "total": []&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; },&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "line_subtotal": 35.9,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "line_subtotal_tax": 0,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "line_total": 35.9,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "line_tax": 0,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "data": {}&nbsp; &nbsp; &nbsp; &nbsp; },&nbsp; &nbsp; &nbsp; &nbsp; "0fc170ecbb8ff1afb2c6de48ea5343e7": {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "key": "0fc170ecbb8ff1afb2c6de48ea5343e7",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "product_id": 2800,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "variation_id": 0,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "variation": [],&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "quantity": 1,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "data_hash": "b5c1d5ca8bae6d4896cf1807cdf763f0",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "line_tax_data": {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "subtotal": [],&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "total": []&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; },&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "line_subtotal": 107.95,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "line_subtotal_tax": 0,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "line_total": 107.95,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "line_tax": 0,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "data": {}&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; },&nbsp; &nbsp; "contents_cost": 143.85,&nbsp; &nbsp; "applied_coupons": [],&nbsp; &nbsp; "user": {&nbsp; &nbsp; &nbsp; &nbsp; "ID": 1&nbsp; &nbsp; },&nbsp; &nbsp; "destination": {&nbsp; &nbsp; &nbsp; &nbsp; "country": "US",&nbsp; &nbsp; &nbsp; &nbsp; "state": "",&nbsp; &nbsp; &nbsp; &nbsp; "postcode": "",&nbsp; &nbsp; &nbsp; &nbsp; "city": "Pine Bluff",&nbsp; &nbsp; &nbsp; &nbsp; "address": "",&nbsp; &nbsp; &nbsp; &nbsp; "address_1": "",&nbsp; &nbsp; &nbsp; &nbsp; "address_2": ""&nbsp; &nbsp; },&nbsp; &nbsp; "cart_subtotal": 143.85,&nbsp; &nbsp; "rates": {&nbsp; &nbsp; &nbsp; &nbsp; "flat_rate:3": {}&nbsp; &nbsp; }}所以有了这些信息,你可以做这样的事情来修改运输成本和运输标签add_filter('woocommerce_package_rates', '_shipping_cost_product_ID', 100, 2);function _shipping_cost_product_ID( $rates, $package ){&nbsp; &nbsp; // duh&nbsp; &nbsp; if ( is_admin() && ! defined( 'DOING_AJAX' ) )&nbsp; &nbsp; &nbsp; &nbsp; return $rates;&nbsp; &nbsp; // The ID of the product you want to modify shipping cost if found in cart&nbsp; &nbsp; $discounted_product_id = 2800;&nbsp; &nbsp; // Get all the product IDs added in cart and put them in array&nbsp; &nbsp; $ids_in_cart = [];&nbsp; &nbsp; foreach ( $package['contents'] as $key => $item ) {&nbsp; &nbsp; &nbsp; &nbsp; $ids_in_cart[] = $item['product_id'];&nbsp; &nbsp; }&nbsp; &nbsp; // loop through each shipping rates&nbsp; &nbsp; foreach ( $rates as $rate_key => $rate ){&nbsp; &nbsp; &nbsp; &nbsp; $has_taxes = false;&nbsp; &nbsp; &nbsp; &nbsp; // Only modify shipping if all conditions below are meet&nbsp; &nbsp; &nbsp; &nbsp; if(&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'flat_rate' === $rate->method_id // if shipping method is "Flat RATE"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; && ( isset($package['destination']['country'] ) && $package['destination']['country'] == 'US' )&nbsp; // if country is US&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; && in_array($discounted_product_id, $ids_in_cart )//if $discounted_product_id is in cart&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $rates[$rate_key]->label = 'Cool Shipping'; // Modify Shipping Name&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $rates[$rate_key]->cost = 3.00; // Modify Shiping Cost&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //Taxes rate cost (if enabled)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $taxes = [];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; foreach ($rates[$rate_key]->taxes as $key => $tax){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if( $rates[$rate_key]->taxes[$key] > 0 ){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $initial_tax_cost = $new_tax_cost = $rates[$rate_key]->taxes[$key];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $tax_rate&nbsp; &nbsp; = $initial_tax_cost / $initial_cost;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $taxes[$key] = $new_cost * $tax_rate;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $has_taxes&nbsp; &nbsp;= true;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if( $has_taxes ) $rates[$rate_key]->taxes = $taxes;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; return $rates;}
打开App,查看更多内容
随时随地看视频慕课网APP