【发布时间】:2016-12-03 15:07:47
【问题描述】:
我有一家 WooCommerce 在线商店,可为大多数产品提供送货服务。部分产品供本地取货。我尝试在成本为零的运输区域上设置一个类别,并在产品上分配类别。但到目前为止,结帐仍然显示运费。有什么方法可以让部分产品免运费?
【问题讨论】:
标签: wordpress woocommerce
我有一家 WooCommerce 在线商店,可为大多数产品提供送货服务。部分产品供本地取货。我尝试在成本为零的运输区域上设置一个类别,并在产品上分配类别。但到目前为止,结帐仍然显示运费。有什么方法可以让部分产品免运费?
【问题讨论】:
标签: wordpress woocommerce
如果您正在寻找插件解决方案,请尝试WooCommerce Conditional Shipping and Payments。通过使用此插件,您可以对某些产品或产品类别添加限制。
【讨论】:
您可能想要查看woocommerce_package_rates 过滤器,它允许您过滤客户可用的一组运输选项。一个例子是这样的:
<?php
// add this snippet to functions.php:
add_filter( 'woocommerce_package_rates', function ( $rates, $package ) {
// examine $package for products. this could be a whitelist of specific
// products that you wish to be treated in a special manner...
$special_ids = array( 1, 2, 3, 4, 5 );
$special_product_present = false;
foreach ( $package['contents'] as $line_item ) {
if ( in_array( $line_item['product_id'], $special_ids ) ) {
$special_product_present = true;
}
}
$rates = array_filter( $rates, function ( $r ) use ( $special_product_present ) {
// do some logic here to return true (for rates that you wish to be displayed), or false.
// example: only allow shipping methods that start with "local"
if ( $special_product_present ) {
return preg_match( '/^local/', strtolower( $r->label ) );
} else {
return true;
}
} );
return $rates;
}, 10, 2 );
blog post here 展示了使用此钩子对该想法的一些变体,包括如何根据购物车价值、客户所在国家/地区、购物车中的商品数量等自定义可用费率。这是源代码:@987654322 @
【讨论】: