我正在创建“自行创建”页面。从选项中选择4种产品后,将1种主要产品添加到购物篮,并将其中4种所选产品作为meta_data添加到该主要产品下。
您可以在下面看到带有4个选定产品(ID)的主要产品。
付款后,我需要将每个选定的产品添加到订单中,以便它们位于后端的订单中。我必须这样做,因为我需要选择的产品的库存下降,最终进入我们使用的库存管理系统(veeqo)
感谢您的帮助。下面的代码使我可以获取woocommerce_thankyou的一些meta_data,但是我不确定它是否能正常工作……
add_action('woocommerce_thankyou', 'BuildYourOwn', 10, 1);
function BuildYourOwn( $order_id ) {
if ( !$order_id ){
return;
}
$firstTime = get_post_meta( $order_id, '_thankyou_action_done', true );
// Allow code execution only once
if( !$firstTime ) {
// Get an instance of the WC_Order object
$order = wc_get_order( $order_id );
$exItems = '';
// Loop through order items
foreach ( $order->get_items() as $item_id => $item ) {
//print_r($item);
// Get the product object
$product = $item->get_product();
// Get the product sku
$product_sku = $product->get_sku();
// Get the product name
$product_id = $product->get_name();
$extras = $item->get_formatted_meta_data('_', true);
$exItems.=$product_sku;
if(!empty($extras)){
$exItems.=$product_sku.' -';
foreach($extras as $extra){
$exItems.= ' ['.$extra->key.' : '. preg_replace("/[^A-Za-z0-9?@,.&%!\s]/","",$extra->value).'] ';
}
}
$exItems.="\n";
}
var_dump($exItems);
答案 0 :(得分:0)
也许我说错了这个问题-但是我发现了:
我使用下面的代码来获取meta_data,然后我将其循环浏览并获得单个商品的ID,并以这种方式将其添加到购物篮中。
// Get the product meta data
$extras = $item->get_formatted_meta_data('_', true);
if ($product_id == 60023){
if(!empty($extras)){
$args = array(
'subtotal' => 0,
'total' => 0,
);
foreach($extras as $extra){
$mini_name = $extra->value;
$mini = get_page_by_title( $mini_name, OBJECT, 'product' );
$mini_id = $mini->ID;
$order->add_product( wc_get_product($mini_id), 1, $args); // Add Minis
}
}
}
唯一的小问题是woocommerce_thankyou如果支付宝用户在付款后没有回到网站,则钩子不会触发。