WooCommerce get_sku();产品变化顺序

WooCommerce get_sku(); on products variations in order

我正在为 Woocommerce 构建自定义功能,我需要在其中获取产品 SKU,如果 'variation' 产品获取特定于变体的 SKU。

我目前拥有的是:

// Query order data
$order = new WC_Order($order_id);
$items = $order->get_items();

// Output the loop
foreach ($order->get_items() as $item) {
    // Getting some information
    $product_name = $item['name'];
    $product_id = $item['product_id'];
    $product_qty = $item['qty'];
    $product_variation_id = $item['variation_id'];
    $product = new WC_Product($item['product_id']);
    // SKU
    $SKU = $product->get_sku();

它工作得很好,除了涉及到变体时,我花了很多时间试图解决这个问题并找到一个好的答案但没有成功。

我开始明白我应该使用这样的东西:

$available_variations = $product->get_available_variations();

疯狂的是我确实有这个工作,但我没有做出 Git 提交,因此我无法恢复正确的代码。我发现的所有示例几乎都包含大量代码,但我相信这可以使用更简单、性能更好的方法来完成。

谈论隧道视野..

 $product_variation_id = $item['variation_id'];
 $product = new WC_Product($item['product_id']);

解决方案正在切换 'product_id' 我已经到位 'variation_id'

$product = new WC_Product($item['variation_id']);

瞧,问题解决了!

工作示例 为了在 $sku 上获得预期的输出,我确实选择了这个解决方案

// Get order data
$order = new WC_Order($order_id);
$items = $order->get_items();

// Loop through ordered items
foreach ($items as $item) {
  $product_name = $item['name'];
  $product_id = $item['product_id'];
  $product_qty = $item['qty'];
  $product_variation_id = $item['variation_id'];

  // Check if product has variation.
  if ($product_variation_id) { 
    $product = new WC_Product($item['variation_id']);
  } else {
    $product = new WC_Product($item['product_id']);
  }

  // Get SKU
  $sku = $product->get_sku();
}

我发现以下 link 非常有用,因为它可以将 SKU 添加到订单项元数据中,因此无需额外调用函数。

https://majemedia.com/woocommerce-save-item-sku-to-order-item-meta-for-historical-reference/

WooCommerce 3.x 此行引发致命错误:

$product = new WC_Product($item['variation_id']);

未捕获异常 'Exception' 消息 'Invalid Product'。

你可以这样做:

$sku = get_post_meta( $item['variation_id'], '_sku', true );

使用 Woocommerce 3.8.1 测试


// Define HERE the accepted statuses of that orders 
$order_statuses = array('wc-completed');

// Define HERE the customer ID
$customer_user_id = get_current_user_id(); // current user ID here for example

// Getting current customer orders
$customer_orders = wc_get_orders( array(
    'meta_key' => '_customer_user',
    'meta_value' => $customer_user_id,
    'post_status' => $order_statuses,
    'numberposts' => -1
) );   

// Loop through each customer WC_Order objects
foreach($customer_orders as $order ){

    $order = new WC_Order($order->get_id());
    $items = $order->get_items();

    // Loop through each item of the order
    foreach ( $items as $item ) {
        $product_variation_id = $item['variation_id'];

        if ($product_variation_id) { // IF Order Item is Product Variantion then get Variation Data instead
            $product = wc_get_product($item['variation_id']);
        } else {
            $product = wc_get_product($item['product_id']);
        }

        if ($product) { // Product might be deleted and not exist anymore    
            $sku = $product->get_sku();             
            echo "<p>SKU: " . $sku . "</p>";                
        }  
    }    
}