当 UPS 运输方式可用时,我试图隐藏 Table 运费
I'm attempting to hide Table Rate Shipping when UPS Shipping Method is Available
我已经尝试从 woothemes 网站编辑以下代码以隐藏送货方式:
// 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.0
电子商务 2.4.6
和 UPS 运送方式 3.1.1 by woothemes
如有任何帮助,我们将不胜感激。
我想要完成的是:
并非我所有的产品都有尺寸。对于确实有尺寸并且可以通过 UPS 结帐的产品,我希望它们只能使用 UPS 结帐。
如果有混合购物车或没有尺寸的产品,我希望它使用 Table 运费。
我特别不希望同时显示 UPS 和 table 费率。
使用该片段的主要问题是 UPS 和 Table Rate Shipping 有多个费率....因此有多个费率 ID。因此,您无法使用简单的 isset($rates['ups'])
有效地测试特定速率的存在,也无法使用 unset($rates['table_rate']);
取消设置另一个速率
看看我的运费示例 var_dump
。我正在使用 USPS,因为这是我手头上的东西,但我希望它与 UPS 非常相似。
据我所知,为了实现您的目标,我们需要测试数组键中是否存在 "ups" 或 "table_rate" 字符串的键。幸运的是,使用 strpos
.
很容易
我针对 USPS 测试了它,它似乎有效。我使用 WooCommerce 工具将站点设置为运输调试模式。否则,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;
}
我已经尝试从 woothemes 网站编辑以下代码以隐藏送货方式:
// 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.0 电子商务 2.4.6 和 UPS 运送方式 3.1.1 by woothemes
如有任何帮助,我们将不胜感激。
我想要完成的是: 并非我所有的产品都有尺寸。对于确实有尺寸并且可以通过 UPS 结帐的产品,我希望它们只能使用 UPS 结帐。 如果有混合购物车或没有尺寸的产品,我希望它使用 Table 运费。
我特别不希望同时显示 UPS 和 table 费率。
使用该片段的主要问题是 UPS 和 Table Rate Shipping 有多个费率....因此有多个费率 ID。因此,您无法使用简单的 isset($rates['ups'])
有效地测试特定速率的存在,也无法使用 unset($rates['table_rate']);
看看我的运费示例 var_dump
。我正在使用 USPS,因为这是我手头上的东西,但我希望它与 UPS 非常相似。
据我所知,为了实现您的目标,我们需要测试数组键中是否存在 "ups" 或 "table_rate" 字符串的键。幸运的是,使用 strpos
.
我针对 USPS 测试了它,它似乎有效。我使用 WooCommerce 工具将站点设置为运输调试模式。否则,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;
}