此功能会抓取我购物车中的特定产品,如果该产品存在,则会删除送货服务。例如。如果产品ID:40在购物车中,则禁用数组中的送货服务值($ shipping_services_to_hide)。
我想向客户显示一条消息,告知他们运输限制,以警报的形式或最好将代码注入特定地点的购物车中(基于类别-no div id available-。此外,使用:之前或之后用css内容:可以接受)(不知道这部分是否可以完成)
这样做的好方法是什么?
add_filter( 'woocommerce_package_rates','hide_shipping_method_if_particular_product_available_in_cart' , 10, 2 );
function hide_shipping_method_if_particular_product_available_in_cart( $available_shipping_methods ) {
global $woocommerce;
// products_array should be filled with all the products ids
// for which shipping method (stamps) to be restricted.
$products_array = array(
40
);
// You can find the shipping service codes by doing inspect element using
// developer tools of chrome. Code for each shipping service can be obtained by
// checking 'value' of shipping option.
$shipping_services_to_hide = array(
'wf_shipping_ups:12',
'wf_shipping_ups:02',
'wf_shipping_ups:59',
'wf_shipping_ups:01',
'wf_shipping_ups:13',
'wf_shipping_ups:14',
'wf_shipping_ups:11',
'wf_shipping_ups:07',
'wf_shipping_ups:54',
'wf_shipping_ups:08',
'wf_shipping_ups:65'
);
// Get all products from the cart.
$products = $woocommerce->cart->get_cart();
// Crawl through each items in the cart.
foreach ( $products as $key => $item ) {
// If any product id from the array is present in the cart,
// unset all shipping method services part of shipping_services_to_hide array.
if( in_array( $item['product_id'], $products_array ) ) {
foreach ( $shipping_services_to_hide as &$value ) {
unset( $available_shipping_methods[ $value ] );
}
break;
}
}
// return updated available_shipping_methods;
return $available_shipping_methods;
}
答案 0 :(得分:0)
您可以使用wc_add_notice()
if( in_array( $item['product_id'], $products_array ) ) {
foreach ( $shipping_services_to_hide as &$value ) {
unset( $available_shipping_methods[ $value ] );
// add your notice here
wc_add_notice( 'No shipping error.', 'your-plugin' ), 'notice' );
}
break;
}