- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
如果客户发货国家/地区不是意大利,我正在尝试禁用特定产品的发货
这是我的代码,但我不知道如何设置国家条件:
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/
[编辑:在 functions.php 中添加代码并省略代码 WC 文件中的更改时,它实际上起作用了。重要提示:它仅在 ONE 属性存在时有效。但是,当有 2 个属性(例如尺寸和颜色)时,它就不起作用
我使用以下方法获取购物车中产品的版本ID。 $cartdetails = $woocommerce->cart->get_cart(); foreach($cartdetails as $ca
我目前使用 $order = new WC_Order($order_id); $order->update_status('completed', 'Order has been delivered
您好,我无法从 woocommerce 订单送货地址获取,我收到错误: fatal error :在 .. 中的非对象上调用成员函数 get_formatted_address() 我正在使用代码:
我不是在谈论向产品添加属性,而是我想添加属性本身.. 您可以从“产品”->“属性”下的 UI 执行此操作,但从代码中它是如何完成的(例如:要调用哪些函数或要更新的表)? 最佳答案 属性只是一个自定义分
是否可以使产品变化来改变价格? 例如,我有一个基本产品及其变体: 数量 -10 -20 -50 颜色 - 蓝色 -红色 项目 -是的 -没有 我有数量的价格,不管是什么颜色。但是,如果您选择“项目-否
有没有办法将其他收件人添加到 woocommerce 发票邮件中。我尝试使用“woocommerce_email_headers” Hook ,但它不起作用: add_filter( 'woocomm
我不是在谈论向产品添加属性,而是我想添加属性本身.. 您可以从“产品”->“属性”下的 UI 执行此操作,但从代码中它是如何完成的(例如:要调用哪些函数或要更新的表)? 最佳答案 属性只是一个自定义分
只是想看看是否有办法为我的送货选项添加工具提示或弹出窗口,以便在客户悬停或点击(通过移动设备)时更详细地向客户解释此选项的含义? 最佳答案 简单的解决方案是通过将以下内容添加到您的 functions
如何获取 woocommerce 中的类别列表?使用这段代码,我得到了 wordpress 类别列表: function gaga_lite_category_lists(){ $catego
我一直试图在 WooCommerce 上禁用产品价格四舍五入,但没有成功。即使我在 WooCommerce > Settings > General > Number of decimals 上将 d
我一直在尝试定制 woocommerce 中的相关产品。我使用 woocommerce_output_related_products_args 添加 meta_query 选项,但它们没有效果,这是
由于 WooCommerce 2.1 页面(例如订单接收)已被删除并替换为 WC 端点。我的结帐页面有一个自定义页面模板 (page-checkout.php),现在所有结帐端点也都在使用这个自定义页
我正在向 WooCommerce 的结帐页面添加额外的字段, 我已经添加了基本的文本字段,但我想要一个带有几个选项的下拉列表或选择框, 这是我到目前为止所做的,但我在某处犯了错误 $fields['b
我试图在我的 invoice.php 中拼出 WooCommerce 订单总额模板文件可以达到订单总金额。 首先我试过: $total = $order->get_total(); - format
我需要在购物车页面和结帐页面中显示的价格(小计、总计...)后添加文字提及。 我知道如何在单个页面和商店页面上执行此操作,但不知道如何在其他页面上执行此操作。我唯一找到的就是这段代码,但它不起作用。
在 Woocommerce 中,在管理产品列表中,我应该需要为特定属性过滤产品。 根据“Add a filter dropdown for product tags in woocommerce ad
Woocommerce 有一个带有“woocommerce”类的 div 我想添加另一个类或删除该类。那是哪个文件? 最佳答案 虽然 WooCommerce 没有提供任何支持的方法来实现这一点
您好,目前我想获取 woocommerce 中的税率名称。我有什么办法可以得到这张图中圈出的值:/image/pyGAH.png 我当前的代码: $items = $order->get_items(
在 Woocommerce 中,我尝试显示可变产品的销售日期。 我找到了a code ,如果我将此代码准确地发布在functions.php中,它不会执行任何操作。我尝试修改它,因为我只需要在起始页上
我是一名优秀的程序员,十分优秀!