Php Can';t从订单中获取差异属性,该订单在woocommerce中使用products属性进行过滤
在我的帐户中,我希望显示与变体属性相关的所有销售。在我的例子中,属性是艺人的名字。我想显示与艺术家相关的每种产品的每一订单的大小/数量和买家姓名。如果大小为空,我希望检索包含我添加的大小的变体\u sku 我从这个函数开始Php Can';t从订单中获取差异属性,该订单在woocommerce中使用products属性进行过滤,php,wordpress,woocommerce,sku,product-variations,Php,Wordpress,Woocommerce,Sku,Product Variations,在我的帐户中,我希望显示与变体属性相关的所有销售。在我的例子中,属性是艺人的名字。我想显示与艺术家相关的每种产品的每一订单的大小/数量和买家姓名。如果大小为空,我希望检索包含我添加的大小的变体\u sku 我从这个函数开始 函数从产品id($product\U id)检索订单id{ 全球$wpdb; //在此定义要包含在$artisteTag中的订单状态 ); //循环所有与$artistTag相关的产品 $loop=新的WP_查询($args); //我们有多少产品 $product\U co
函数从产品id($product\U id)检索订单id{
全球$wpdb;
//在此定义要包含在$artisteTag中的订单状态
);
//循环所有与$artistTag相关的产品
$loop=新的WP_查询($args);
//我们有多少产品
$product\U count=$loop->post\U count;
//如果有产品
如果($product\U count>0)
{
echo“合作名称:.$product_count.”;
回声“;
//对于每个产品,展示相关订单
而($loop->have_posts()):
$loop->the_post();
全球$产品;
全球$员额;
$productID=$product->get_id();
$thePostID=$post->post_title;
$orders\U id\U array=从产品id($productID)检索订单\U id\U;
//显示带有“cat_名称”的“产品名称”的“x”订单
echo“Il y a”.count($orders_id_array)。“ventes-pour:”.$thePostID.“(productID=”.$productID.)-”;
$terms=get_the_terms($productID,'product_cat');
foreach($terms作为$term)
{
$cat_id=$term->id;
$cat_name=$term->name;
$cat_slug=$term->slug;
$cat_description=$term->description;
$cat_count=$term->count;
echo$cat_name.“”;
}
//在订单上循环
foreach($orders\u id\u数组作为$order\u id){
$order=wc\u get\u order($order\u id);
$order_data=$order->get_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'];
//显示订单中的一些信息
回声“;
回音“命令”。$OrdIdID。“FaTeLE LE”。$OrdOutDATEY创建。“Par”。$OrrdBILIGIN FIRTSTYNEX。“”
回声“
”;
foreach($order->get\u items()作为$item\u key=>$item):
//var_dump(项目);
$order_product_id=$item->get_product_id();
$item_name=$item->get_name();
$quantity=$item->get_quantity();
$variation_id=$item->get_variation_id();
//需要像$variation\u sku=$get\u variations\u sku($product\u id)这样的东西;
$variation\u size=get\u post\u meta($variation\u id,'attribute\u pa\u taille',true);
//只想展示与艺术家相关的产品,而不是全部订单细节
如果($order\U product\U id===$productID)
{
echo“- ”$item\u name.”(订单商品标识=“.$order\u product\u id.”)(变体商品标识=“.$variation\u id.”)Quantité=“.$quantity.”
”;
如果($variation\u id)
{
echo$variation_size;//适用于90%的情况
}
其他的
{
//echo$variation_sku;
echo“variation_id为空,但是如果我们显示var_dump($item),我们可以在所有数据中找到大小;”;
}
}
endforeach;
回声“
”;
回声“;
}
结束时;
回声“;
}
}
}
关于在何处使用变体进行挖掘的任何帮助\u sku..在这项工作中没有找到任何帮助。我找到了解决方案
echo wc_display_item_meta( $item );
如果variation\u id为空,将返回good size属性 很好的解决方案!
$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>";
}
}
}
echo wc_display_item_meta( $item );