gpt4 book ai didi

php - 根据 Woocommerce 中的国家/地区禁用特定产品的运输

转载 作者:行者123 更新时间:2023-12-01 22:09:50 24 4
gpt4 key购买 nike

如果客户发货国家/地区不是意大利,我正在尝试禁用特定产品的发货

这是我的代码,但我不知道如何设置国家条件:

function hide_shipping_when_class_is_in_cart( $rates, $package ) {
// shipping class IDs that need the method removed
$shipping_classes = array('bulky-items');
$if_exists = false;

foreach( $package['contents'] as $key => $values ) {
if( in_array( $values[ 'data' ]->get_shipping_class(), $shipping_classes ) )
$if_exists = true;
}

if( $if_exists ) unset( $rates['free_shipping:7'] );

return $rates;
}
add_filter( 'woocommerce_package_rates', 'hide_shipping_when_class_is_in_cart', 10, 2 );

如果选择的发货国家/地区不是意大利,我如何禁用产品发货?

最佳答案

Note: This code is made to work in Woocommerce 3+ (but not in very old version 2.3)

你的问题不是很清楚......所以你主要有 2 个选项 (并在最后检查添加到购物车这两个选项,当客户注册时,当检测到运送国家或已在购物车或结帐中设置时):

选项 1 - 与其删除无法运送到除意大利以外的所有其他国家/地区的产品的运送方式,不如删除显示自定义通知的相关购物车项目......您必须定义函数中仅可在意大利发货的产品 ID:

add_action( 'woocommerce_before_calculate_totals', 'checking_and_removing_items', 10, 1 );
function checking_and_removing_items( $cart ) {
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;

if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 )
return;

$custome_shipping_country = WC()->customer->get_shipping_country();

if( empty($custome_shipping_country) ){
$package = WC()->shipping->get_packages()[0];
if( ! isset($package['destination']['country']) ) return;
$custome_shipping_country = $package['destination']['country'];
}

// Only for NON Italians customers
if( $custome_shipping_country == 'IT' ) return;

// ==> HERE set your product IDs (ITALY ONLY)
$products_ids = array(37, 57);

// Iterate through each cart item
$found = false;
foreach( $cart->get_cart() as $cart_item_key => $cart_item )
if( in_array( $cart_item['data']->get_id(), $products_ids ) ){
$found = true;
$cart->remove_cart_item( $cart_item_key ); // remove item
}

if( $found ){
// Custom notice
wc_clear_notices();
wc_add_notice('Some products are not shippable to your country and have been removed', 'notice');
}
}

代码进入您的事件子主题(事件主题)的 function.php 文件。

Add to cart validation feature is at the end…


选项 2 - 删除无法运送到除意大利以外的所有其他国家/地区的产品的运送方式并显示自定义错误通知......您将必须在功能中定义产品 ID,仅可在意大利发货:

add_filter( 'woocommerce_package_rates', 'disable_shipping_methods', 20, 2 );
function disable_shipping_methods( $rates, $package ) {
if( ! ( isset($package['destination']['country']) && isset($package['contents']) ) )
return $rates;

// Only for NON Italians customers
if( $package['destination']['country'] == 'IT' ) return $rates;

// ==> HERE set your product IDs (ITALY ONLY)
$products_ids = array(37, 57);

// Loop through cart items and checking
$found = false;
foreach( $package['contents'] as $item )
if( in_array( $item['data']->get_id(), $products_ids ) ){
$found = true;
break;
}

if( ! $found ) return $rates; // If nothing is found: We EXIT

foreach( $rates as $rate_id => $rate )
unset($rates[$rate_id]); // Removing all shipping methods

// Custom notice
wc_clear_notices();
wc_add_notice('Some products are only shippable for Italy', 'error');

return $rates;
}

代码进入您的事件子主题(事件主题)的 function.php 文件。


使用自定义通知添加到购物车验证功能(对于两个选项)。

您必须在函数中定义仅可在意大利发货的产品 ID。

add_filter( 'woocommerce_add_to_cart_validation', 'avoid_products_for_non_italian', 20, 3 );
function avoid_products_for_non_italian( $passed, $product_id, $quantity ) {

$custome_shipping_country = WC()->customer->get_shipping_country();

if( empty($custome_shipping_country) ){
$package = WC()->shipping->get_packages()[0];
if( ! isset($package['destination']['country']) ) return $passed;
$custome_shipping_country = $package['destination']['country'];
}

// Only for NON Italians customers
if( $custome_shipping_country == 'IT' ) return $passed;

// ==> HERE set your product IDs (ITALY ONLY)
$products_ids = array(37, 57);

// The condition
if( in_array( $product_id, $products_ids ) ){
$passed = false;
wc_add_notice( 'This product is only shippable for Italy.', 'error' );
}
return $passed;
}

代码进入您的事件子主题(事件主题)的 function.php 文件。

所有代码都经过测试并适用于 Woocommerce 版本 3+ (也可能是 2.6.x)

关于php - 根据 Woocommerce 中的国家/地区禁用特定产品的运输,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48795558/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com