Php WooCommerce使用item Order meta将项目添加到订单
我想删除现有的WooComerce项目,并从中添加x个新的WooComerce项目。这取决于$item->quantity的高度 我的代码如下所示:Php WooCommerce使用item Order meta将项目添加到订单,php,wordpress,methods,woocommerce,orders,Php,Wordpress,Methods,Woocommerce,Orders,我想删除现有的WooComerce项目,并从中添加x个新的WooComerce项目。这取决于$item->quantity的高度 我的代码如下所示: add_action( 'woocommerce_thankyou', 'my_change_status_function', 10, 1 ); function my_change_status_function( $order_id ) { $order = new WC_Order( $order_id ); fore
add_action( 'woocommerce_thankyou', 'my_change_status_function', 10, 1 );
function my_change_status_function( $order_id ) {
$order = new WC_Order( $order_id );
foreach ( $order->get_items() as $item_id => $item ) {
if ( $item->get_quantity() > 1 ) {
wc_delete_order_item( $item_id );
for ( $i = 1; $i <= $item->get_quantity(); $i++ ) {
$new_item_ids = woocommerce_add_order_item(
$order_id,
array(
'order_item_name' => $item->get_name() . '_' . $i,
'order_item_type' => 'line_item',
)
);
if ( $new_item_ids ) {
foreach ( $metavalues as $key => $value ) {
wc_add_order_item_meta( $new_item_ids, $key, $value );
}
}
}
}
}
}
添加行动('woocommerce\u Thankyu'、'my\u change\u status\u function',10,1);
函数my\u change\u status\u函数($order\u id){
$order=新WC\U订单($order\U id);
foreach($order->get\u items()作为$item\u id=>$item){
如果($item->get_quantity()>1){
wc_删除_订单_项目($item_id);
对于($i=1;$i获取_数量();$i++){
$new\u item\u ids=woocommerce\u add\u order\u item(
$order_id,
排列(
'order_item_name'=>$item->get_name()。'$i,
“订单项目类型”=>“行项目”,
)
);
如果($new\u item\u id){
foreach($key=>$value的元值){
wc\u添加\u订单\u项目\u元($new\u item\u id,$key,$value);
}
}
}
}
}
}
我有点绝望的地方是:
if ( $new_item_ids ) {
foreach ( $metavalues as $key => $value ) { <-- I dont know where i get the $metavalues
wc_add_order_item_meta( $new_item_ids, $key, $value );
}
}
如果($new\u item\u id){
foreach($key=>metavalues作为$key=>value){因为WooCommerce 3,您的代码已经过时,可以使用一些WC\u Abstract\u Order
方法简化。另外,您最好使用WooCommerce\u checkout\u Order\u created
hook,这是在创建订单后触发的:
add_action( 'woocommerce_checkout_order_created', 'separate_order_items_by_quantity' );
function separate_order_items_by_quantity( $order ) {
$items_processed = false; // Initializing
// Loop through order items
foreach ( $order->get_items() as $item_id => $item ) {
$quantity = $item->get_quantity(); // Get quantity
$product = $item->get_product(); // Get the WC_Product Object
// When item has quatity more than 1, separete items by quantity
if ( $item->get_quantity() > 1 ) {
$order->remove_item( $item_id ); // Remove item
$items_processed = true; // Flag it
// Loop through quantity
for ( $i = 1; $i <= $quantity; $i++ ) {
// Add the same productb (separated) n times the quantity
$new_item_id = $order->add_product( $product, 1, array(
'_custom_index' => $i, // (required) to have separated products
'name' => $product->get_name() . ' ' . $i, // (optional) change product name appending the index
'Some meta key' => 'some meta value', // (optional) Here you can set a custom meta key and its value (custom order item meta data) … To hide it from customer the metakey should start with an underscore.
) );
}
}
}
if( $items_processed ) {
$order->apply_changes(); // Merge changes with data and clear
$order->save(); // Save data
}
}
add_action('woocommerce_checkout_order_created','separate_order_items_by_quantity');
功能按数量(订单)分隔订单项目{
$items\u processed=false;//正在初始化
//循环浏览订单项
foreach($order->get\u items()作为$item\u id=>$item){
$quantity=$item->get_quantity();//获取数量
$product=$item->get_product();//获取WC_产品对象
//当项目数量大于1时,按数量分离项目
如果($item->get_quantity()>1){
$order->remove_item($item_id);//remove item
$items\u processed=true;//标记它
//循环通过量
对于($i=1;$i添加产品($product,1,数组)(
“_custom_index”=>$i,//(必需)有单独的产品
“name'=>$product->get_name().”.$i,//(可选)更改附加索引的产品名称
'Some meta key'=>'Some meta value',//(可选)在这里,您可以设置自定义meta key及其值(自定义订单项元数据)…要向客户隐藏此meta key,metakey应以下划线开头。
) );
}
}
}
如果($items\u processed){
$order->apply_changes();//将更改与数据合并并清除
$order->save();//保存数据
}
}
代码放在活动子主题(或活动主题)的functions.php文件中。经过测试,可以正常工作。另外,你能告诉我我尝试的逻辑是否有用吗?当我的代码有点难以承受时,请告诉我。我一直在这里学习更多信息。