0

在 woocommerce 我的帐户中,我想显示与变体属性相关的所有销售。在我的例子中,属性是艺人姓名。我想显示与艺术家相关的每个产品的每个订单的尺寸/数量和买家姓名。如果 size 为空,我想检索包含我添加的 size 的 variant_sku。

我从这个功能开始从 Woocommerce 中的产品 ID 获取所有订单 ID

function retrieve_orders_ids_from_a_product_id( $product_id ) {
    global $wpdb;

    // Define HERE the orders status to include in  <==  <==  <==  <==  <==  <==  <==
    $orders_statuses = "'wc-completed'";
    //$orders_statuses = "'wc-completed', 'wc-processing', 'wc-on-hold'";

    # Get All defined statuses Orders IDs for a defined product ID (or variation ID)
    return $wpdb->get_col( "
            SELECT DISTINCT woi.order_id
            FROM {$wpdb->prefix}woocommerce_order_itemmeta as woim, 
                     {$wpdb->prefix}woocommerce_order_items as woi, 
                     {$wpdb->prefix}posts as p
            WHERE  woi.order_item_id = woim.order_item_id
            AND woi.order_id = p.ID
            AND p.post_status IN ( $orders_statuses )
            AND woim.meta_key IN ( '_product_id', '_variation_id' )
            AND woim.meta_value LIKE '$product_id'
            ORDER BY woi.order_item_id DESC"
    );
}

对于 90% 的情况,结果都很好,除了一些订单。“variation_id”为空,但变量属性存在,但我无法显示它们。(例如包含尺寸的 pa_size 或变体-sku)

这是工作代码

$artisteTag = "artiste-stack";                  
$args = array( 
        'post_type'      => 'product', 
        'posts_per_page' => 99, // dont need pagination
        'product_tag'    => $artisteTag  
        );

// Loop for all products with $artistTag related
$loop = new WP_Query( $args );

// how much product do we have
$product_count = $loop->post_count;

// If have products
if( $product_count > 0 )
    {
    echo "Nombre de Collaborations : ".$product_count."<br>";
    echo "<div class='collabs'>";
    // for each product show orders related
    while ( $loop->have_posts() ) : 
        $loop->the_post(); 
        global $product;
        global $post;
        $productID = $product->get_id();
        $thePostID = $post->post_title;
        $orders_ids_array = retrieve_orders_ids_from_a_product_id( $productID );
        // Show "x" orders for "product name" with "cat_name"
        echo "<p>Il y a ".count($orders_ids_array)." ventes pour : ".$thePostID." (productID = ".$productID.") - ";
        $terms = get_the_terms ( $productID, 'product_cat' );
        foreach ( $terms as $term ) 
            {
            $cat_id = $term->id;
            $cat_name = $term->name;
            $cat_slug = $term->slug;
            $cat_description = $term->description;
            $cat_count = $term->count;  
            echo $cat_name."</p>";  
            }
        // Loop on the orders 
        foreach($orders_ids_array as $order_id){
            $order = wc_get_order($order_id);
            $order_data = $order->get_data();
            //var_dump($order_data);
            $order_date_created = $order_data['date_created']->date('Y-m-d H:i:s');
            $order_billing_first_name = $order_data['billing']['first_name'];
            $order_billing_last_name = $order_data['billing']['last_name'];
            // show some infos from the order
            echo "<div class='order'>";

            echo "<p>Commande ".$order_id." faite le ".$order_date_created." par ".$order_billing_first_name." ".$order_billing_last_name."</p>";

            echo "<ul>";
            foreach ($order->get_items() as $item_key => $item ):
                //var_dump($item);
                $order_product_id   = $item->get_product_id();
                $item_name              = $item->get_name();
                $quantity               = $item->get_quantity();
                $variation_id           = $item->get_variation_id();

             // Need something like $variation_sku = $get_variations_sku($product_id);
                $variation_size     = get_post_meta( $variation_id, 'attribute_pa_taille', true);
// only want to show the product related to the artist and not the total order details
                if($order_product_id === $productID )
                    {
                    echo "<li>".$item_name." (order_product_id = ".$order_product_id.") (variation_id = ".$variation_id.") Quantité = ".$quantity."</li>";
                    if ($variation_id) 
                        { 
                        echo $variation_size; // works for 90% of case
                        } 
                    else 
                        {

                        // echo $variation_sku;   
                        echo "variation_id is empty but if we show var_dump($item) we can find size in all the data;";
                        }
                    }
            endforeach;
            echo "</ul>";

        echo "</div>";
        }
    endwhile;
    echo "</div>";
    }

        }
    }

任何关于在哪里用variation_sku 挖掘的帮助..没有找到任何关于这个工作的东西。

4

1 回答 1

0

我找到了解决方案

echo wc_display_item_meta( $item ); 

如果variation_id 为空,将返回良好的尺寸属性!

于 2020-03-10T19:29:44.010 回答