在Woocommerce 3中检索自定义订单商品元数据值


Frui tenebris、
2025-03-12 03:49:52 (9天前)


我有以下代码,它基于一些自定义字段创建购物车项目数据:

function parcel_add_shipment_text_to_cart_item($ cart_item_data,$ product_id,$ variation_id){

$ service = …

2 条回复
  1. 0# 甲基蓝 | 2019-08-31 10-32



    在woocommerce_payment_complete操作挂钩中使用您的自定义订单商品元数据(或在您可以获取订单ID或订单对象的任何其他挂钩中):




    1. add_action( woocommerce_payment_complete’, on_action_payment_complete’, 10, 1 );
      function on_action_payment_complete( $order_id ) {
      // Get an instance of the WC_Order Object
      $order = wc_get_order( $order_id );

    2. // Loop through order items
    3. foreach( $order->get_items() as $item_id => $item ){
    4.     $servicelevel = $item->get_meta('Service Level');
    5.     $servicecode  = $item->get_meta('Your Reference');
    6.     $serviceprice = $item->get_meta('Sell Price');
    7.     $chargeweight = $item->get_meta('Charge Weight');
    8. }
    9. }

    10. </code>


    代码位于活动子主题(或活动主题)的function.php文件中。它应该有效。钩子里面的代码经过测试,确实有效。


登录 后才能参与评论