我对目前的答案并不满意,因为有时您需要检查多个产品。如果你对每个产品进行相同的搜索,那真是太浪费了,所以我把它变成了调度程序格式。
add_action('woocommerce_order_status_completed', 'onItemCheckout',10,1);
function onItemCheckout($order_id){
$order = wc_get_order($order_id);
foreach ($order->get_items() as $item_key => $item_values){
$product_id = $item_values->get_product_id();
switch($item_values->get_product_id()){
case 9999 : FreeShipping($order, $product_id); break;
case 1010 : RequireValidation($order, $product_id); break;
default: break;
}
}
}
或者,...
$ItemCheckoutHandler=[];
$ItemCheckoutHandler[9999]='FreeShipping';
$ItemCheckoutHandler[1010]='RequireValidation';
add_action('woocommerce_order_status_completed', 'onItemCheckout',10,1);
function onItemCheckout($order_id){
global $ItemCheckoutHandler;
$order = wc_get_order($order_id);
foreach ($order->get_items() as $item_key => $item_values){
$product_id=$item_values->get_product_id();
$ItemCheckoutHandler[ $product_id ]($order, $product_id);
} //Call the function assigned to that product id in the array
}
在任一情况下,所分配的功能将采取order
对象,而不是ID,而product_id
作为参数:
function FreeShipping($order, $product_id){ ... }
function RequireValidation($order, $product_id){ ... }
您当然可以根据自己的喜好自定义这些输入。
感谢它的工作,我如何使用order_id获得产品计费详细信息,可以请您告诉我这个 – ali
'$ item_values-> get_id()'不检索产品ID,即$ item_values-> get_product_id ()'。 – helgatheviking
@helgatheviking是的,你是对的...我做了更新。 – LoicTheAztec