如何确定使用该产品购买了产品的哪个WooCommerce变体

时间:2019-07-18 20:17:34

标签: php woocommerce

在woocommerce_payment_complete挂钩期间,我收到了订单,然后使用get_items()从中提取产品。从这里开始,当我遍历商品并使用get_sku()获取SKU时,它将返回主要产品的SKU,而不是购买的变体的SKU。

我整天都在搜索这个问题,并且有大量的帖子告诉我,一旦有了变体ID,该如何使用变体做各种事情……但是对于我来说,我可以不知道如何获取变体ID!

简单地说: 我的网站上有一个产品(帽子),并且该产品有3个变体(棒球,三角帽,斯泰森),每个变体都带有自定义SKU,并将Item_hat_stetson设置为默认值。我唯一想知道的是“此人选择购买哪三顶帽子?但我只能确定的是:

  1. 是的,他买了一顶帽子:SKU Item_hat
  2. 有棒球,特里比和斯泰森这些品种

我如何确定选择的人的版本?

简单,对吧? ...但对于我的一生,我只是想不通。 :(请帮助...

谢谢

2 个答案:

答案 0 :(得分:0)

在您的woocommerce付款完成挂钩中尝试此操作。

$order = wc_get_order( $order_id ); 

// Loop though order "line items"
foreach( $order->get_items() as $item_id => $item_product ){
    $product_id = $item_product->get_product_id(); //Get the product ID
    $quantity = $item_product->get_quantity(); //Get the product QTY
    $product_name = $item_product->get_name(); //Get the product NAME

     // Get an instance of the WC_Product object (can be a product variation  too)
    $product = $item_product->get_product();

     // Get the product description (works for product variation)
    $description = $product->get_description();

    // Only for product variation
    if($product->is_type('variation')){
         // Get the variation attributes
        $variation_attributes = $product->get_variation_attributes();
        // Loop through each selected attributes
        foreach($variation_attributes as $attribute_taxonomy => $term_slug){
            $taxonomy = str_replace('attribute_', '', $attribute_taxonomy );
            // The name of the attribute
            $attribute_name = get_taxonomy( $taxonomy )->labels->singular_name;
            // The term name (or value) for this attribute
            $attribute_value = get_term_by( 'slug', $term_slug, $taxonomy )->name;
        }
    }
}

答案 1 :(得分:0)

function after_purchase( $order_id )
{
    global $wpdb;   
    $order = new WC_Order( $order_id );

    foreach ($order->get_items() as $item_id => $item)
    {
        if ($item['quantity'] <= 0) continue;

        $product = wc_get_product($item['product_id']);
        if (!$product) continue;

        $item_data = $item->get_data();

        if (function_exists('wcs_get_users_subscriptions'))
        {
            $subscriptions = wcs_get_users_subscriptions();
            foreach($subscriptions as $subscription)
            {
                if ($subscription->has_product($product->id))
                {
                    //can't use this. Don't know how
                    $order_key = get_post_meta($subscription->get_id(),'_order_key',true);

                    $item_post = $subscription->get_id();
                    $query = "SELECT order_item_id FROM {$wpdb->prefix}woocommerce_order_items "
                            ."WHERE order_id = '$item_post'";
                    $order_number = $wpdb->get_var($query);
                    $query2 = "SELECT meta_value FROM {$wpdb->prefix}woocommerce_order_itemmeta "
                            . "WHERE order_item_id = '$order_number' AND meta_key ='edition'";
                    $variation_identifier = $wpdb->get_var($query2);
                }
            }
        }
    }

这为我获取了我想要的数据,但是这样做会使GOT成为错误的方法.....

在此代码段中,我获取了为产品创建的自定义属性,对此我很满意,因为这是一种可相互识别产品的方式,但是我应该做的(显然)是针对meta_key搜索_variation_id'而不是'edition'来获取发布表ID,该ID将为我提供产品,然后我可以使用WC_Product t加载该产品,最终从中获取SKU ...这太麻烦了,要进行如此简单的调用太多了一个SKU ...

有人可以告诉我正确的方法吗?

谢谢