【发布时间】:2018-12-04 05:05:05
【问题描述】:
我想根据产品的配送等级更改商店结账时显示的配送方式标题。
例如
运输方式标题目前是统一费率,我有 2 件产品:
- 如果正在购买产品 A,我需要它具有“易碎运输”
- 如果正在购买产品 B,我需要它具有“标准配送”
遗憾的是,我必须使用类来完成运输,因此其他方法不起作用。
任何帮助将不胜感激。
【问题讨论】:
标签: php wordpress woocommerce cart shipping-method
我想根据产品的配送等级更改商店结账时显示的配送方式标题。
例如
运输方式标题目前是统一费率,我有 2 件产品:
遗憾的是,我必须使用类来完成运输,因此其他方法不起作用。
任何帮助将不胜感激。
【问题讨论】:
标签: php wordpress woocommerce cart shipping-method
以下代码将根据您的“易碎”运输等级重命名您的统一运费:
您可能必须在“运输选项”选项卡下的常规运输设置中“启用调试模式”,以暂时禁用运输缓存。
代码:
add_filter('woocommerce_package_rates', 'change_shipping_method_name_based_on_shipping_class', 50, 2);
function change_shipping_method_name_based_on_shipping_class($rates, $package){
// HERE set the shipping class for "Fragile"
$shipping_class_id = 64;
$found = false;
// Check for the "Fragile" shipping class in cart items
foreach( $package['contents'] as $cart_item ) {
if( $cart_item['data']->get_shipping_class_id() == $shipping_class_id ){
$found = true;
break;
}
}
// Loop through shipping methods
foreach ( $rates as $rate_key => $rate ) {
// Change "Flat rate" Shipping method label name
if ( 'flat_rate' === $rate->method_id ) {
if( $found )
$rates[$rate_key]->label = __( 'Fragile shipping', 'woocommerce' );
else
$rates[$rate_key]->label = __( 'Standard shipping', 'woocommerce' );
}
}
return $rates;
}
代码进入您的活动子主题(或活动主题)的 function.php 文件中。经过测试并且可以工作。
别忘了在发货设置中重新启用“启用调试模式”选项。
【讨论】: