【发布时间】:2021-03-30 17:55:54
【问题描述】:
我正在尝试根据产品选择设置运输方式,我已经实现了,但我还想隐藏其他运输方式,这样用户就看不到它们,也无法选择它们。这是我正确选择运输方式的代码:
add_action( 'template_redirect', 'nhy_chosen_shipping_methods' );
function nhy_chosen_shipping_methods(){
remove_action(current_filter(), __FUNCTION__);
$items = WC()->cart->get_cart();
$max_dimension = 0;
$final_max = 0;
foreach( $items as $item => $values ){
$_product = wc_get_product( $values['data']->get_id());
if(empty($values['we_custom_dimensions'])){
$max_dimension = $_product->get_length() >= $_product->get_height() ? $_product->get_length() : $_product->get_height();
}
else{
$dims = explode('x', $values['we_custom_dimensions']);
$max_dimension = $dims[0] >= $dims[1] ? $dims[0] : $dims[1];
}
if($max_dimension > $final_max){
$final_max = $max_dimension;
}
}
if( WC()->cart->cart_contents_total >= 100 ){
WC()->session->set( 'chosen_shipping_methods', array('free_shipping:10') );
return;
}
if($final_max <= 70){
WC()->session->set( 'chosen_shipping_methods', array('flat_rate:8') );
return;
}
else{
WC()->session->set( 'chosen_shipping_methods', array('flat_rate:9') );
return;
}
}
更新
【问题讨论】:
-
@Bhautik 否,因为它使用了另一个钩子而不是'template_redirect'
-
但是你可以使用
woocommerce_package_rates过滤钩子。 -
@Bhautik 我试过了,但它不能与我的代码一起工作......我不知道如何组合它们
标签: php session woocommerce shipping-method