仅允许对 WooCommerce 中特定运输区域之外的特定产品进行本地提货

我的商店里有多种产品,但其中一种产品(易燃)只能通过特定的运输公司运输;不幸的是,该公司无法覆盖全国。因此,如果客户购买了易燃产品,并且该产品不在唯一可以运送该产品的公司的覆盖范围内,那么除了本地取货之外,他不应该看到任何其他运送选项。


到目前为止,我有这段代码(由不同的 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 );


但我不知道为什么不起作用。甚至“回声”也不起作用。


牛魔王的故事
浏览 76回答 1
1回答

哔哔one

更新- 请尝试以下操作(代码已注释):// SETTINGS BELOW: Custom function that handle your settingsfunction custom_shipping_settings() {&nbsp; &nbsp; return array(&nbsp; &nbsp; &nbsp; &nbsp; 'product_ids' => array(2267), // Define the products that need to be in a separated shipping package (local pickup)&nbsp; &nbsp; &nbsp; &nbsp; 'allowed_zones_ids' => array(8), // Define the allowed zones IDs&nbsp; &nbsp; );}// Splitting cart items into 2 shipping packagesadd_filter( 'woocommerce_cart_shipping_packages', 'split_shipping_packages' );function split_shipping_packages( $packages ) {&nbsp; &nbsp; extract(custom_shipping_settings()); // Load and extract settings&nbsp; &nbsp; $customer&nbsp; &nbsp; &nbsp; &nbsp;= WC()->customer;&nbsp; &nbsp; $destination&nbsp; &nbsp; = array(&nbsp; &nbsp; &nbsp; &nbsp; 'country'&nbsp; &nbsp;=> $customer->get_shipping_country(),&nbsp; &nbsp; &nbsp; &nbsp; 'state'&nbsp; &nbsp; &nbsp;=> $customer->get_shipping_state(),&nbsp; &nbsp; &nbsp; &nbsp; 'postcode'&nbsp; => $customer->get_shipping_postcode(),&nbsp; &nbsp; &nbsp; &nbsp; 'city'&nbsp; &nbsp; &nbsp; => $customer->get_shipping_city(),&nbsp; &nbsp; &nbsp; &nbsp; 'address'&nbsp; &nbsp;=> $customer->get_shipping_address(),&nbsp; &nbsp; &nbsp; &nbsp; 'address_2' => $customer->get_shipping_address_2()&nbsp; &nbsp; );&nbsp; &nbsp; $package_dest&nbsp; &nbsp;= array( 'destination' => $destination );&nbsp; &nbsp; $zone&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;= wc_get_shipping_zone( $package_dest );&nbsp; &nbsp; if ( ! in_array( $zone->get_id(), $allowed_zones_ids ) ) {&nbsp; &nbsp; &nbsp; &nbsp; // Reset packages and initialize variables&nbsp; &nbsp; &nbsp; &nbsp; $packages = $splitted_cart_items = array();&nbsp; &nbsp; &nbsp; &nbsp; // Loop through cart items&nbsp; &nbsp; &nbsp; &nbsp; foreach ( WC()->cart->get_cart() as $item_key => $item ) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if ( is_a($item['data'], 'WC_Product') && $item['data']->needs_shipping() ) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if ( ! array_intersect( array($item['product_id'], $item['variation_id']), $product_ids ) ) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $splitted_cart_items[0][$item_key] = $item; // Regular items&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $splitted_cart_items[1][$item_key] = $item; // Special separated items&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; if ( count($splitted_cart_items) < 2 )&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return $packages;&nbsp; &nbsp; &nbsp; &nbsp; // Loop through spitted cart items&nbsp; &nbsp; &nbsp; &nbsp; foreach ( $splitted_cart_items as $key => $items ) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Set each cart items group in a shipping package&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $packages[$key] = array(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'contents'&nbsp; &nbsp; &nbsp; &nbsp; => $items,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'contents_cost'&nbsp; &nbsp;=> array_sum( wp_list_pluck( $items, 'line_total' ) ),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'applied_coupons' => WC()->cart->get_applied_coupons(),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'user'&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; => array( 'ID' => get_current_user_id() ),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'destination'&nbsp; &nbsp; &nbsp;=> $destination,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; );&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; 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 ) {&nbsp; &nbsp; extract(custom_shipping_settings()); // Load and extract settings&nbsp; &nbsp; $zone&nbsp; = wc_get_shipping_zone( $package ); // Get current shipping zone&nbsp; &nbsp; $found = false;&nbsp; &nbsp; // For all others shipping zones than allowed zones&nbsp; &nbsp; if ( ! in_array( $zone->get_id(), $allowed_zones_ids ) ) {&nbsp; &nbsp; &nbsp; &nbsp; // Loop through cart items for current shipping package&nbsp; &nbsp; &nbsp; &nbsp; foreach( $package['contents'] as $item ) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Look for defined specific product Ids&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if ( array_intersect( array($item['product_id'], $item['variation_id']), $product_ids ) ) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $found = true; // Flag as found&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break; // Stop the loop&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; // If any defined product is in cart&nbsp; &nbsp; &nbsp; &nbsp; if ( $found ) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Loop through shipping rates&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; foreach ( $rates as $rate_key => $rate ) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Hide all shipping methods keeping "Local pickup"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if ( 'local_pickup' !== $rate->method_id ) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; unset( $rates[$rate_key] );&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; return $rates;}代码位于活动子主题(或活动主题)的functions.php 文件中。经过测试并有效。刷新运输缓存:此代码已保存在您的functions.php 文件中。在运输区域设置中,禁用/保存任何运输方式,然后启用返回/保存。你已经完成了,你可以测试它。
打开App,查看更多内容
随时随地看视频慕课网APP