在结帐时,我需要阻止某些产品在某些城市的交货(不是所有产品)。当用户填写根据我定义的列表应阻止的城市时,结帐流程将阻止该订单,并且自定义通知错误会根据需要完美显示。
我使用此代码在某些城市阻止送货:
add_action( 'woocommerce_checkout_process', 'shipping_validate_city' );
function shipping_validate_city() {
if ( in_array( $product_id, array( 3059, 3058) ) ) {
$disableCityList = array (
'Rabat',
'Temara',
'Sale',
'Tamessna',
);
$billingCity = isset( $_POST['billing_city'] ) ? trim( $_POST['billing_city'] ) : '';
$billingCity = str_replace(array('-','_'),' ',$billingCity);
$billingCity = ucwords($billingCity);
if (in_array($billingCity, $disableCityList))
{
wc_add_notice( __('this product not allowed shipping for the city you mentioned') , 'error' );
}
}
}
我的问题是。我该如何仅对特定产品执行此操作?
答案 0 :(得分:0)
我认为现在可以使用了,我从购物车中获取产品ID
foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
$pdt_id = $cart_item['product_id'];
}
我曾经检查产品是否存在于阵列中,
add_action( 'woocommerce_checkout_process', 'shipping_validate_city' );
function shipping_validate_city() {
$pdt_id= array();
foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
$pdt_id = $cart_item['product_id'];
}
if ( in_array( $pdt_id , array( 3059, 3058) ) ) {
$enableCityList = array (
'Rabat',
'Temara',
'Sale',
'Tamessna',
'Tamesna',
'RABAT',
'TEMARA',
'SALE',
'TAMESSNA',
'TAMESNA',
'Salé',
);
$billingCity = isset( $_POST['billing_city'] ) ? trim( $_POST['billing_city'] ) : '';
$billingCity = str_replace(array('-','_'),' ',$billingCity);
$billingCity = ucwords($billingCity);
if (!in_array($billingCity, $enableCityList))
{
wc_add_notice( __('Livraison à ville que vous avez indiquée non couverts pour ce produit ') , 'error' );
}
}
}