Php 在WooCommerce中的签出过程中获取产品id
在结帐时,我需要在某些城市阻止特定产品(并非所有产品)的交付。当用户根据我定义的列表填写一个应该被阻止的城市时,结帐过程会阻止订单,并且自定义通知错误会完全按照需要显示 我使用此代码阻止某些城市的配送:Php 在WooCommerce中的签出过程中获取产品id,php,wordpress,woocommerce,hook-woocommerce,Php,Wordpress,Woocommerce,Hook Woocommerce,在结帐时,我需要在某些城市阻止特定产品(并非所有产品)的交付。当用户根据我定义的列表填写一个应该被阻止的城市时,结帐过程会阻止订单,并且自定义通知错误会完全按照需要显示 我使用此代码阻止某些城市的配送: add_action( 'woocommerce_checkout_process', 'shipping_validate_city' ); function shipping_validate_city() { if ( in_array( $product_id, array( 3
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' );
}
}
}
我的问题是。如何仅针对特定产品执行此操作?我想现在可以了,我从购物车项目中获取产品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' );
}
}
}