php WooCommerce get_sku(); 关于产品变化的顺序

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28662963/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 00:00:30  来源:igfitidea点击:

WooCommerce get_sku(); on products variations in order

phpwordpresswoocommerce

提问by Mathias Asberg

I'm building a custom function for Woocommerce where I need to get product SKU and if 'variation' product get variation specific SKU.

我正在为 Woocommerce 构建一个自定义函数,我需要在其中获取产品 SKU,如果“变体”产品获取变体特定的 SKU。

What I currently have is the following:

我目前拥有的是以下内容:

// 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();

It works good except when it comes to variations, I Have spent much time trying to figure this out and to find a good answer without any succes.

它工作得很好,除非涉及到变化,我花了很多时间试图弄清楚这一点并找到一个没有任何成功的好答案。

I come to understand that I should use something like this:

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

$available_variations = $product->get_available_variations();

The crazy thing is that I did have this working but I didn't made a Git commit, thus I'm not able to revert the correct code. All the examples I found pretty much exist of a lot of code, but I'm sure this can be done using a much simpler and better performing method.

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

回答by Mathias Asberg

Talk about tunnel vision..

谈谈隧道视觉..

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

Solution was switching 'product_id' for my already in place 'variation_id'

解决方案是为我已经就位的“variation_id”切换“product_id”

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

Voila, problem solved !

瞧,问题解决了!

Working exampleTo get the expected output on $skui did go for this solution

工作示例为了获得预期的输出,$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();
}

回答by Mike

On WooCommerce 3.xthis line throws a fatal error:

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

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

Uncaught exception 'Exception' with message 'Invalid Product'.

未捕获的异常“异常”,消息为“产品无效”。

You can do this:

你可以这样做:

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

回答by MikeeeGeee

I found this following link massively helpful as adds the SKU to the order item meta so no need for extra calling of functions.

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

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

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

回答by optimiertes

Tested with Woocommerce 3.8.1

使用 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>";                
        }  
    }    
}