我试图更新woocommerce订单商品价格,如果它是一个可变产品。
我遇到的问题是它似乎只是获得了woocommerce产品变体的第一个价格,并且即使它的变化不同,它也会使用顺序中的每个项目变化,它应该从正确的变异ID获得价格但是我不确定我错过了什么?
以下代码:
function update_variable_product_price( ){
$query = new WC_Order_Query( array(
'status' => 'on-hold',
'orderby' => 'date',
'order' => 'DESC',
'return' => 'ids',
) );
$order_ids = $query->get_orders();
foreach( $order_ids as $order_id ) {
$order = new WC_Order($order_id);
$items = $order->get_items();
echo "Order Number: ". $order_id;
echo "</br>";
echo "Product Name: ". $order_id["name"];
echo "</br>";
foreach ($items as $item) {
$product_name = $item->get_name();
$product_id = $item->get_product_id();
$product_variation_id = $item->get_variation_id();
$product_variation_price = $item->get_total();
$product = new WC_Product_Variable($product_id);#1 Get product variations
$product_variations = $product->get_available_variations();
$product_qty = $item['qty'];
echo "</br>-----------------";
echo "Product Name: " . $product_name;
echo "</br>";
echo "Qty".$product_qty;
echo "</br>";
echo "Product Price: " . $product_variation_price;
echo "</br>";
echo "Product ID: " . $product_id;
echo "</br>";
echo "Variation ID: " . $product_variation_id;
echo "</br>";
var_dump($product_variations) ;
echo "</br>";
foreach ($product_variations as $product_variation) {
$variation_product_id = $product_variation ['variation_id'];
$variation_product = new WC_Product_Variation($variation_product_id);
echo "</br>";
echo $variation_product->get_price();
echo "</br>";
echo $variation_product->get_id();
echo "</br>";
if (isset($product_variation_id)){
if($product_id == $product_variation_id) {
return wc_update_order_item_meta($item->$product_id, $product_variation_id, $variation_product->get_price());
}
}
}
}
}
}