我已经尝试从wootemes网站编辑以下代码,以隐藏一种运输方法,即:
// woocommerce_package_rates is a 2.1+ hook
add_filter( 'woocommerce_package_rates', 'hide_shipping_when_free_is_available', 10, 2 );
// Hide shipping rates when free shipping is available
// @param array $rates Array of rates found for the package
// @param array $package The package array/object being shipped
// @return array of modified rates
//
function hide_shipping_when_free_is_available( $rates, $package ) {
// Only modify rates if free_shipping is present
if ( isset( $rates['free_shipping'] ) ) {
// To unset a single rate/method, do the following. This example unsets flat_rate shipping
unset( $rates['flat_rate'] );
// To unset all methods except for free_shipping, do the following
$free_shipping = $rates['free_shipping'];
$rates = array();
$rates['free_shipping'] = $free_shipping;
}
return $rates;
}
我把代码从"free_shipping"编辑为"ups",我相信这就是我所需要做的,但遗憾的是,没有任何结果。
我使用Mike Jolley的Table Rate Shipping 2.9.0Wooccommerce 2.4.6和wootemes 的UPS运输方法3.1.1
如有任何帮助,我们将不胜感激。
我正在努力实现的是:并不是我所有的产品都有尺寸。对于那些有尺寸并且可以通过UPS结账的产品,我希望他们只能使用UPS结账。如果有一个混合推车或没有尺寸的产品,我希望它使用表费率运输。
我特别不希望UPS和餐桌价格同时显示。
使用该片段的主要问题是UPS和Table Rate Shipping有多个费率。。。。并且因此具有多个速率ID。因此,你不能用简单的isset($rates['ups'])
有效地测试特定速率的存在,也不能用unset($rates['table_rate']);
取消设置另一个速率
看看我的样品运费的var_dump
。我使用USPS是因为我手头有USPS,但我希望它与UPS非常相似。
因此,据我所知,为了实现您的目标,我们需要测试键在数组键中是否存在"ups"或"table_rate"字符串。幸运的是,使用strpos
非常容易。
我针对USPS进行了测试,结果似乎有效。我使用WooCommerce工具将网站设置为Shipping Debug模式。否则,WooCommerce会将费率临时存储一个小时。(请参阅您网站上的admin.php?page=wc-status&tab=tools
)
这是我的最后一个代码:
// remove any table_rate rates if UPS rates are present
add_filter( 'woocommerce_package_rates', 'hide_table_rates_when_ups_available', 10, 2 );
function hide_table_rates_when_ups_available( $rates, $package ) {
// Only modify rates if ups is present
if ( is_ups_available( $rates ) ) {
foreach ( $rates as $key => $rate ){
$pos = strpos( $key, 'table_rate' );
if( false !== $pos ){
unset( $rates[$key] );
}
}
}
return $rates;
}
// loops through the rates looking for any UPS rates
function is_ups_available( $rates ){
$is_available = false;
foreach ( $rates as $key => $rate ){
$pos = strpos( $key, 'ups' );
if( false !== $pos ){
$is_available = true;
break;
}
}
return $is_available;
}