WooCommerce 中基于位置和产品类别的运费和限制
Shipping costs and restrictions based on location and product category in WooCommerce
我正在使用 Woocommerce,我想限制运输 class 'alcoholics' 并且只允许意大利而不是其他国家,你能帮我解决这个代码吗?非常感谢您的帮助!
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;
}
谢谢
您不能限制运送 类 自己。国家/地区限制由 中的运输区域进行,您可以为每个区域设置特定的运输方式。运输 classes 允许在每个“统一费率”运输方式中操纵费率。
最后,您可以为产品(或产品变体)设置运费 class,这样您就可以为所选产品自定义运费。
相反,您可以使用 woocommerce_package_rates
过滤器通过其唯一的 费率 ID 有条件地操纵在您的送货区域中设置的每种送货方式挂钩:
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;
}
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 …
代码进入您的活动子主题(或主题)的 functions.php 文件或任何插件文件。
相关回答:
您可以使用以下插件完成此操作:
https://wordpress.org/plugins/advanced-shipping-validation-for-woocommerce
这是我观察到的。还有另一个非常相似的线程,用户要求在某些国家/地区限制下载,其中另一个用户共享了我在 functions.php 文件中使用的代码,这里是我由于缺乏技术知识而面临的问题:
- 代码有效 site-wide 意味着您不能只限制特定产品,但所选国家/地区的所有商店都受到限制
- 每当我需要限制任何其他国家/地区时,都需要编辑代码
- 该代码需要国家和 ip 地址的 ISO 代码才能进行限制
- 需要单独的代码才能在限制和隐藏添加到 cart/price 按钮的情况下显示自定义消息(对于像我这样的人来说编码太多)
然后在同一个线程中,我找到了一种方法来限制国家/地区的默认 WooCommerce 设置,但这是我在启用这些设置时遇到的问题
- 也是一个site-wide解决方案
- WooCommerce geo-location 在某些情况下检测不准确
厌倦了所有这些解决方案,我选择使用这个名为 Country Restrictions for WooCommerce 的插件(https://addify.store/product/woocommerce-country-restrictions/),它易于安装,然后它还可以选择创建多个规则和批量规则应用程序,这是正是我正在寻找的解决方案。
我正在使用 Woocommerce,我想限制运输 class 'alcoholics' 并且只允许意大利而不是其他国家,你能帮我解决这个代码吗?非常感谢您的帮助!
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;
}
谢谢
您不能限制运送 类 自己。国家/地区限制由 中的运输区域进行,您可以为每个区域设置特定的运输方式。运输 classes 允许在每个“统一费率”运输方式中操纵费率。
最后,您可以为产品(或产品变体)设置运费 class,这样您就可以为所选产品自定义运费。
相反,您可以使用 woocommerce_package_rates
过滤器通过其唯一的 费率 ID 有条件地操纵在您的送货区域中设置的每种送货方式挂钩:
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;
}
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 …
代码进入您的活动子主题(或主题)的 functions.php 文件或任何插件文件。
相关回答:
您可以使用以下插件完成此操作: https://wordpress.org/plugins/advanced-shipping-validation-for-woocommerce
这是我观察到的。还有另一个非常相似的线程,用户要求在某些国家/地区限制下载,其中另一个用户共享了我在 functions.php 文件中使用的代码,这里是我由于缺乏技术知识而面临的问题:
- 代码有效 site-wide 意味着您不能只限制特定产品,但所选国家/地区的所有商店都受到限制
- 每当我需要限制任何其他国家/地区时,都需要编辑代码
- 该代码需要国家和 ip 地址的 ISO 代码才能进行限制
- 需要单独的代码才能在限制和隐藏添加到 cart/price 按钮的情况下显示自定义消息(对于像我这样的人来说编码太多)
然后在同一个线程中,我找到了一种方法来限制国家/地区的默认 WooCommerce 设置,但这是我在启用这些设置时遇到的问题
- 也是一个site-wide解决方案
- WooCommerce geo-location 在某些情况下检测不准确
厌倦了所有这些解决方案,我选择使用这个名为 Country Restrictions for WooCommerce 的插件(https://addify.store/product/woocommerce-country-restrictions/),它易于安装,然后它还可以选择创建多个规则和批量规则应用程序,这是正是我正在寻找的解决方案。