pbootcms网站模板|日韩1区2区|织梦模板||网站源码|日韩1区2区|jquery建站特效-html5模板网

如何獲取訂單商品 ID 以獲取一些產(chǎn)品元數(shù)據(jù)?

How to get order items ids to get some product meta data?(如何獲取訂單商品 ID 以獲取一些產(chǎn)品元數(shù)據(jù)?)
本文介紹了如何獲取訂單商品 ID 以獲取一些產(chǎn)品元數(shù)據(jù)?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學(xué)習吧!

問題描述

限時送ChatGPT賬號..

我正在嘗試使用以下方法從 Woocommerce 的訂單中提取項目元值:

I'm trying to extract item meta value from Woocommerce's orders by using:

$data = wc_get_order_item_meta( $item, '_tmcartepo_data', true );

但是,我找不到獲取 order_item_id 作為第一個參數(shù)的方法(使用 get_items)

However, I can't find a way to get order_item_id as the first parameter (using get_items)

global $woocommerce, $post, $wpdb;
$order = new WC_Order($post->ID);
$items = $order->get_items(); 

foreach ( $items as $item ) {
    $item_id = $item['order_item_id']; //???
    $data = wc_get_order_item_meta( $item_id, '_tmcartepo_data', true );
    $a = $data[0]['value'];
    $b = $data[1]['value'];
    echo $a;
    echo $b;
}

我的意思是這個訂單 item_id (1 和 2)

And I mean this order item_id (1 and 2)

數(shù)據(jù)庫中的Order_item_id - 圖片

我該怎么做?

謝謝.

推薦答案

2018 年更新:

  • 用兩種可能的情況澄清答案
  • 添加了對 woocommerce 3+ 的兼容性

所以可能有兩種情況:

1) 獲取商品元數(shù)據(jù)(不在訂單商品元數(shù)據(jù)中設(shè)置):

1) Get product meta data (not set in order item meta data):

您將需要在 foreach 循環(huán)中為 WC_Order 獲取產(chǎn)品 ID,并為該產(chǎn)品獲取一些元數(shù)據(jù),您將使用 get_post_meta() 函數(shù) ( 但不是 wc_get_order_item_meta() ).

You will need to get the product ID in the foreach loop for a WC_Order and to get some metadata for this product you wil use get_post_meta() function ( but NOT wc_get_order_item_meta() ).

這是您的代碼:

global $post;
$order = wc_get_order( $post->ID );
$items = $order->get_items(); 

foreach ( $order->get_items() => $item ) {

    // Compatibility for woocommerce 3+
    $product_id = version_compare( WC_VERSION, '3.0', '<' ) ? $item['product_id'] : $item->get_product_id();

    // Here you get your data
    $custom_field = get_post_meta( $product_id, '_tmcartepo_data', true); 

    // To test data output (uncomment the line below)
    // print_r($custom_field);

    // If it is an array of values
    if( is_array( $custom_field ) ){
        echo implode( '<br>', $custom_field ); // one value displayed by line 
    } 
    // just one value (a string)
    else {
        echo $custom_field;
    }
}

<小時>

2) 獲取訂單項元數(shù)據(jù)(自定義字段值):

global $post;
$order = wc_get_order( $post->ID );
$items = $order->get_items(); 

foreach ( $order->get_items() as $item_id => $item ) {

    // Here you get your data
    $custom_field = wc_get_order_item_meta( $item_id, '_tmcartepo_data', true ); 

    // To test data output (uncomment the line below)
    // print_r($custom_field);

    // If it is an array of values
    if( is_array( $custom_field ) ){
        echo implode( '<br>', $custom_field ); // one value displayed by line 
    } 
    // just one value (a string)
    else {
        echo $custom_field;
    }
}

<小時>

如果自定義字段數(shù)據(jù)是數(shù)組,可以在foreach循環(huán)中訪問數(shù)據(jù):


If the custom field data is an array, you can access the data in a foreach loop:

// Iterating in an array of keys/values
foreach( $custom_field as $key => $value ){
    echo '<p>key: '.$key.' | value: '.$value.'</p>';
} 

所有代碼都經(jīng)過測試且有效.

訂單中數(shù)據(jù)相關(guān)參考:

  • 如何獲取 WooCommerce 訂單詳情(也適用于 woocommerce 3)
  • 獲取訂單商品和 Woocommerce 3 中的 WC_Order_Item_Product

這篇關(guān)于如何獲取訂單商品 ID 以獲取一些產(chǎn)品元數(shù)據(jù)?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請聯(lián)系我們刪除處理,感謝您的支持!

相關(guān)文檔推薦

Add programmatically a downloadable file to Woocommerce products(以編程方式將可下載文件添加到 Woocommerce 產(chǎn)品)
Get today#39;s total orders count for each product in Woocommerce(獲取今天 Woocommerce 中每種產(chǎn)品的總訂單數(shù))
Add Custom registration fields in WooCommerce and phone field validation issue(在 WooCommerce 和電話字段驗證問題中添加自定義注冊字段)
Add a select field that will change price in Woocommerce simple products(在 Woocommerce 簡單產(chǎn)品中添加一個將更改價格的選擇字段)
Add custom columns to admin products list in WooCommerce 3(在 WooCommerce 3 中將自定義列添加到管理產(chǎn)品列表)
Customizing checkout quot;Place Orderquot; button output html(自定義結(jié)帳“下訂單按鈕輸出html)
主站蜘蛛池模板: 模具硅橡胶,人体硅胶,移印硅胶浆厂家-宏图硅胶科技 | TTCMS自助建站_网站建设_自助建站_免费网站_免费建站_天天向上旗下品牌 | Magnescale探规,Magnescale磁栅尺,Magnescale传感器,Magnescale测厚仪,Mitutoyo光栅尺,笔式位移传感器-苏州连达精密量仪有限公司 | 冷油器-冷油器换管改造-连云港灵动列管式冷油器生产厂家 | 定硫仪,量热仪,工业分析仪,马弗炉,煤炭化验设备厂家,煤质化验仪器,焦炭化验设备鹤壁大德煤质工业分析仪,氟氯测定仪 | 拉力机-拉力试验机-万能试验机-电子拉力机-拉伸试验机-剥离强度试验机-苏州皖仪实验仪器有限公司 | 大_小鼠elisa试剂盒-植物_人Elisa试剂盒-PCR荧光定量试剂盒-上海一研生物科技有限公司 | 多功能三相相位伏安表-变压器短路阻抗测试仪-上海妙定电气 | 迪威娱乐|迪威娱乐客服|18183620002 | 环氧乙烷灭菌器_压力蒸汽灭菌器_低温等离子过氧化氢灭菌器 _低温蒸汽甲醛灭菌器_清洗工作站_医用干燥柜_灭菌耗材-环氧乙烷灭菌器_脉动真空压力蒸汽灭菌器_低温等离子灭菌设备_河南省三强医疗器械有限责任公司 | PO膜_灌浆膜及地膜供应厂家 - 青州市鲁谊塑料厂 | 加气混凝土砌块设备,轻质砖设备,蒸养砖设备,新型墙体设备-河南省杜甫机械制造有限公司 | 超细粉碎机|超微气流磨|气流分级机|粉体改性设备|超微粉碎设备-山东埃尔派粉碎机厂家 | 冲击式破碎机-冲击式制砂机-移动碎石机厂家_青州市富康机械有限公司 | 耙式干燥机_真空耙式干燥机厂家-无锡鹏茂化工装备有限公司 | 可程式恒温恒湿试验箱|恒温恒湿箱|恒温恒湿试验箱|恒温恒湿老化试验箱|高低温试验箱价格报价-广东德瑞检测设备有限公司 | 仓储笼_仓储货架_南京货架_仓储货架厂家_南京货架价格低-南京一品仓储设备制造公司 | 喷码机,激光喷码打码机,鸡蛋打码机,手持打码机,自动喷码机,一物一码防伪溯源-恒欣瑞达有限公司 | 仿真植物|仿真树|仿真花|假树|植物墙 - 广州天昆仿真植物有限公司 | 干式变压器厂_干式变压器厂家_scb11/scb13/scb10/scb14/scb18干式变压器生产厂家-山东科锐变压器有限公司 | 云南外加剂,云南速凝剂,云南外加剂代加工-普洱澜湄新材料科技有限公司 | 食品无尘净化车间,食品罐装净化车间,净化车间配套风淋室-青岛旭恒洁净技术有限公司 | 广东健伦体育发展有限公司-体育工程配套及销售运动器材的体育用品服务商 | 信阳网站建设专家-信阳时代网联-【信阳网站建设百度推广优质服务提供商】信阳网站建设|信阳网络公司|信阳网络营销推广 | 免费个人pos机申请办理-移动pos机刷卡-聚合收款码办理 | 共享雨伞_共享童车_共享轮椅_共享陪护床-共享产品的领先者_有伞科技 | 数控专用机床,专用机床,自动线,组合机床,动力头,自动化加工生产线,江苏海鑫机床有限公司 | 阜阳成人高考_阜阳成考报名时间_安徽省成人高考网 | 首页-恒温恒湿试验箱_恒温恒湿箱_高低温试验箱_高低温交变湿热试验箱_苏州正合 | 大白菜官网,大白菜winpe,大白菜U盘装系统, u盘启动盘制作工具 | 双舌接地线-PC68数字式高阻计-ZC36|苏海百科 | 上海公司注册-代理记账-招投标审计-上海昆仑扇财税咨询有限公司 上海冠顶工业设备有限公司-隧道炉,烘箱,UV固化机,涂装设备,高温炉,工业机器人生产厂家 | 电子万能试验机_液压拉力试验机_冲击疲劳试验机_材料试验机厂家-济南众标仪器设备有限公司 | 广州办公室设计,办公室装修,写字楼设计,办公室装修公司_德科 | 破碎机锤头_合金耐磨锤头_郑州宇耐机械工程技术有限公司 | 振动台-振动试验台-振动冲击台-广东剑乔试验设备有限公司 | 蓄电池回收,ups电池后备电源回收,铅酸蓄电池回收,机房电源回收-广州益夫铅酸电池回收公司 | 三氯异氰尿酸-二氯-三氯-二氯异氰尿酸钠-优氯净-强氯精-消毒片-济南中北_优氯净厂家 | 东莞ERP软件_广州云ERP_中山ERP_台湾工厂erp系统-广东顺景软件科技有限公司 | 润滑脂-高温润滑脂-轴承润滑脂-食品级润滑油-索科润滑油脂厂家 | 面粉仓_储酒罐_不锈钢储酒罐厂家-泰安鑫佳机械制造有限公司 |