第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

僅允許對 WooCommerce 中特定運(yùn)輸區(qū)域之外的特定產(chǎn)品進(jìn)行本地提貨

僅允許對 WooCommerce 中特定運(yùn)輸區(qū)域之外的特定產(chǎn)品進(jìn)行本地提貨

PHP
牛魔王的故事 2023-10-01 16:00:46
我的商店里有多種產(chǎn)品,但其中一種產(chǎn)品(易燃)只能通過特定的運(yùn)輸公司運(yùn)輸;不幸的是,該公司無法覆蓋全國。因此,如果客戶購買了易燃產(chǎn)品,并且該產(chǎn)品不在唯一可以運(yùn)送該產(chǎn)品的公司的覆蓋范圍內(nèi),那么除了本地取貨之外,他不應(yīng)該看到任何其他運(yùn)送選項(xiàng)。到目前為止,我有這段代碼(由不同的 StackOverFlow 答案提供):function filter_woocommerce_package_rates( $rates, $package ) {    // Shipping zone    //echo 'entrando';    $shipping_zone = wc_get_shipping_zone( $package );    $product_ids = array( 2267 ); // HERE set the product IDs in the array    $method_id = 'weight_based_shipping:38'; // HERE set the shipping method ID that I want to hide    $found = false;        // Get zone ID    $zone_id = $shipping_zone->get_id();        //echo $shipping_zone;    //echo $zone_id;        // NOT equal    if ( $zone_id != 8 ) {        // Unset a single rate/method for a specific product        foreach( $package['contents'] as $cart_item ) {        if ( in_array( $cart_item['product_id'], $product_ids ) ){            $found = true;            break;        }        }         if ( $found )            unset( $rates[$method_id] );    }    return $rates;}add_filter( 'woocommerce_package_rates', 'filter_woocommerce_package_rates', 10, 2 );但我不知道為什么不起作用。甚至“回聲”也不起作用。
查看完整描述

1 回答

?
嗶嗶one

TA貢獻(xiàn)1854條經(jīng)驗(yàn) 獲得超8個(gè)贊

更新- 請嘗試以下操作(代碼已注釋):


// SETTINGS BELOW: Custom function that handle your settings

function custom_shipping_settings() {

    return array(

        'product_ids' => array(2267), // Define the products that need to be in a separated shipping package (local pickup)

        'allowed_zones_ids' => array(8), // Define the allowed zones IDs

    );

}



// Splitting cart items into 2 shipping packages

add_filter( 'woocommerce_cart_shipping_packages', 'split_shipping_packages' );

function split_shipping_packages( $packages ) {

    extract(custom_shipping_settings()); // Load and extract settings


    $customer       = WC()->customer;

    $destination    = array(

        'country'   => $customer->get_shipping_country(),

        'state'     => $customer->get_shipping_state(),

        'postcode'  => $customer->get_shipping_postcode(),

        'city'      => $customer->get_shipping_city(),

        'address'   => $customer->get_shipping_address(),

        'address_2' => $customer->get_shipping_address_2()

    );

    $package_dest   = array( 'destination' => $destination );

    $zone           = wc_get_shipping_zone( $package_dest );


    if ( ! in_array( $zone->get_id(), $allowed_zones_ids ) ) {


        // Reset packages and initialize variables

        $packages = $splitted_cart_items = array();


        // Loop through cart items

        foreach ( WC()->cart->get_cart() as $item_key => $item ) {

            if ( is_a($item['data'], 'WC_Product') && $item['data']->needs_shipping() ) {

                if ( ! array_intersect( array($item['product_id'], $item['variation_id']), $product_ids ) ) {

                    $splitted_cart_items[0][$item_key] = $item; // Regular items

                } else {

                    $splitted_cart_items[1][$item_key] = $item; // Special separated items

                }

            }

        }


        if ( count($splitted_cart_items) < 2 )

            return $packages;


        // Loop through spitted cart items

        foreach ( $splitted_cart_items as $key => $items ) {

            // Set each cart items group in a shipping package

            $packages[$key] = array(

                'contents'        => $items,

                'contents_cost'   => array_sum( wp_list_pluck( $items, 'line_total' ) ),

                'applied_coupons' => WC()->cart->get_applied_coupons(),

                'user'            => array( 'ID' => get_current_user_id() ),

                'destination'     => $destination,

            );

        }

    }

    return $packages;

}


// Force local pickup for specific splitted shipping package (for the defined products)

add_filter( 'woocommerce_package_rates', 'filter_wc_package_rates', 10, 2 );

function filter_wc_package_rates( $rates, $package ) {

    extract(custom_shipping_settings()); // Load and extract settings


    $zone  = wc_get_shipping_zone( $package ); // Get current shipping zone

    $found = false;


    // For all others shipping zones than allowed zones

    if ( ! in_array( $zone->get_id(), $allowed_zones_ids ) ) {

        // Loop through cart items for current shipping package

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

            // Look for defined specific product Ids

            if ( array_intersect( array($item['product_id'], $item['variation_id']), $product_ids ) ) {

                $found = true; // Flag as found

                break; // Stop the loop

            }

        }


        // If any defined product is in cart

        if ( $found ) {

            // Loop through shipping rates

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

                // Hide all shipping methods keeping "Local pickup"

                if ( 'local_pickup' !== $rate->method_id ) {

                    unset( $rates[$rate_key] );

                }

            }

        }

    }

    return $rates;

}

代碼位于活動(dòng)子主題(或活動(dòng)主題)的functions.php 文件中。經(jīng)過測試并有效。

刷新運(yùn)輸緩存:

  1. 此代碼已保存在您的functions.php 文件中。

  2. 在運(yùn)輸區(qū)域設(shè)置中,禁用/保存任何運(yùn)輸方式,然后啟用返回/保存。

    你已經(jīng)完成了,你可以測試它。


查看完整回答
反對 回復(fù) 2023-10-01
  • 1 回答
  • 0 關(guān)注
  • 105 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)