dongmaonao0505 2018-06-10 01:51
浏览 234
已采纳

如何从Woocommerce中的订单商品获取产品sku [重复]

For Api on page "woocommerce_thankyou" need get sku. I have:

$order = wc_get_order( $order_id ); 
foreach ($order->get_items() as $item_key => $item_values):
   $product = new WC_Product($item_id);
   $item_sku[] = $product->get_sku();
endforeach;

But not works.

</div>
  • 写回答

1条回答 默认 最新

  • doubu2730 2018-06-10 03:49
    关注

    I think you're fiddling around on the actual template page ;-) In Wordpress we mainly use action hooks to accomplish tasks like this.

    Try this, place it in the (child) theme functions.php.

    NOTE: only for WooCommerce 3+

    add_action( 'woocommerce_thankyou', 'order_created_get_skus', 10 );
    
    function order_created_get_skus($order_id){
    
      $item_sku = array();
      $order = wc_get_order( $order_id ); 
    
      foreach ($order->get_items() as $item) {
        $product = wc_get_product($item->get_product_id());
        $item_sku[] = $product->get_sku();
      }
    
      // now do something with the sku array 
    
    }
    

    Regards, Bjorn

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 5037端口被adb自己占了
  • ¥15 python:excel数据写入多个对应word文档
  • ¥60 全一数分解素因子和素数循环节位数
  • ¥15 ffmpeg如何安装到虚拟环境
  • ¥188 寻找能做王者评分提取的
  • ¥15 matlab用simulink求解一个二阶微分方程,要求截图
  • ¥30 乘子法解约束最优化问题的matlab代码文件,最好有matlab代码文件
  • ¥15 写论文,需要数据支撑
  • ¥15 identifier of an instance of 类 was altered from xx to xx错误
  • ¥100 反编译微信小游戏求指导