WooCommerce中基于位置和产品类别的运输成本和限制 [英] Shipping costs and restrictions based on location and product category in WooCommerce

查看:122
本文介绍了WooCommerce中基于位置和产品类别的运输成本和限制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Woocommerce,我想限制航运类别酒精饮料,仅在意大利而不是其他任何国家/地区允许使用,您能帮我提供此代码吗?非常感谢您的帮助!

I'm using Woocommerce and I'd like to restrict the shipping class 'alcoholics' and allow it just for just Italy and no other countries, can you help me with this code? Thank you very much for your help!

function( $is_available ) {

$cart_items = WC()->cart->get_cart();

foreach ( $cart_items as $cart_item ) {

    $product = $cart_item['data'];
    $class   = $product->get_shipping_class();

    if ( $class === 'alcoholics' ) {
        return false;
    }
}

return $is_available;
 }

add_filter( 'woocommerce_states', 'AL_woocommerce_states' );


function AL_woocommerce_states( $states ) {


  $states['IT'] = array(
  'AG' => __( 'Agrigento', 'woocommerce' ),
  'AL' => __( 'Alessandria', 'woocommerce' ),
  'AN' => __( 'Ancona', 'woocommerce' ),
  'AO' => __( 'Aosta', 'woocommerce' ),
  'AR' => __( 'Arezzo', 'woocommerce' ),
  'AP' => __( 'Ascoli Piceno', 'woocommerce' ),
  'AT' => __( 'Asti', 'woocommerce' ),
  'AV' => __( 'Avellino', 'woocommerce' ),
  'BA' => __( 'Bari', 'woocommerce' ),
  'BT' => __( 'Barletta-Andria-Trani', 'woocommerce' ),
  'BL' => __( 'Belluno', 'woocommerce' ),
  'BN' => __( 'Benevento', 'woocommerce' ),
  'BG' => __( 'Bergamo', 'woocommerce' ),
  'BI' => __( 'Biella', 'woocommerce' ),
  'BO' => __( 'Bologna', 'woocommerce' ),
   );

   return $states;
}

谢谢

推荐答案

您不能自己限制运输类。国家/地区限制由女巫中的运输区域设置,您可以为每个运输区域设置特定的运输方式。运输类允许在每种统一费率运输方法中操纵费率。

You can't restrict Shipping Classes themselves. The country restriction is maid by the Shipping Zones in witch you can set specific shipping methods for each of them. Shipping classes allow to manipulate the rates in each "flat rate" shipping method.

最后,您可以设置产品(或产品变体)的运输类别,这将使您能够为所选产品设置自定义运输方式费率。

Finally You can set a shipping class for a product (or a product variation), that will allow you to have a custom shipping method rate for the chosen products.

相反,您可以使用 woocommerce_package_rates 过滤钩子:

Instead you can conditionally manipulate each Shipping method set in your Shipping zones by its unique rate ID using woocommerce_package_rates filter hook:

add_filter( 'woocommerce_package_rates', 'manipulating_shipping_flat_rates', 10, 2 );
function custom_delivery_flat_rate_cost_calculation( $rates, $package )
{
    // Initializing variables
    $has_products = false;
    $has_cat = false;

    // Iterating through all cart items
    foreach(WC()->cart->get_cart() as $cart_item ):

        $product_id = $cart_item['product_id']; // product ID
        $variation_id = $cart_item['variation_id']; // variation ID

        // Detecting a targeted product category
        if( has_term( 'clothing', 'product_cat', $product_id ) )
            $has_cat = true;

        // Detecting specific product IDs
        if ( in_array( $cart_item['product_id'], array(20, 22, 28, 47, 58)) )
            $has_products = true;
    endforeach;


    foreach($rates as $rate_key => $rate_values){
        $method_id = $rate_values->method_id;
        $rate_id = $rate_values->id;

        // Targetting Flat rate Shipping Method
        if ( 'flat_rate' === $rate_values->method_id ) {


            ## 1) UNSETTING RATE IDs

            // for a product category
            if ( $has_cat ) {
                unset( $rates['flat_rate:5'] );
            }
            // for a product Ids
            if ( $has_products ) {
                unset( $rates['flat_rate:14'] );
            }

            ## 2) Manipulating specif Rates IDs prices  

            // for a product category and a specific rate ID
            if ( $has_cat && 'flat_rate:8' == $rate_id ) {
                $rates[$rate_id]->cost = $rates[$rate_id]->cost * 0.1;
                $rates[$rate_id]->taxes[1] = $rates[$rate_id]->taxes[1] * 0.1;
            }
            // for a product Ids and a specific rate ID
            if ( $has_products && 'flat_rate:11' == $rate_id ) {
                $rates[$rate_id]->cost = $rates[$rate_id]->cost * 0.1;
                $rates[$rate_id]->taxes[1] = $rates[$rate_id]->taxes[1] * 0.1;
            }
        }
    }

    return $rates;
}




您需要更改代码设置自己的产品类别,产品ID,运费ID和运费,以使其满足您的需求……

You will need to change the code setting your own product categories, product IDs, Shipping rates IDs and rates costs, to make it working for your needs …

代码起作用了。您的活动子主题(或主题)或任何插件文件中的php文件。

Code goes in function.php file of your active child theme (or theme) or also in any plugin file.

相关答案: WooCommerce:在结帐验证失败后更新自定义字段

这篇关于WooCommerce中基于位置和产品类别的运输成本和限制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆