簡體   English   中英

來自訂單 ID WooCommerce 的產品元數據

[英]Product meta data from order id WooCommerce

嘗試從最后一個訂單中提取產品數據,例如[key] => pa_size

使用automatewoo_update_print_file調用file.php其中兩個功能所在,當一個新的注釋被添加到一個訂單,它被稱為:

 function automatewoo_update_print_file( $workflow ) {

 include '/home/***/public_html/wp-content/themes/***-child/woocommerce/checkout/file.php'; 


}

更新這可以很好地提取最新的訂單 ID 並獲取產品 ID,然后是來自 ID 的元數據以及其余訂單數據。 但我仍然需要拉[key] => pa_size

function get_last_order_id(){
global $wpdb;
$statuses = array_keys(wc_get_order_statuses());
$statuses = implode( "','", $statuses );

// Getting last Order ID (max value)
$results = $wpdb->get_col( "
    SELECT MAX(ID) FROM {$wpdb->prefix}posts
    WHERE post_type LIKE 'shop_order'
    AND post_status IN ('$statuses')
" );
return reset($results);
}

$latest_order_id = get_last_order_id(); // Last order ID
$order = wc_get_order( $latest_order_id ); // Get an instance of the WC_Order oject
$order_details = $order->get_data(); // Get the order data in an array
$order_status = $order_details['status'];


foreach ($order->get_items() as $item_key => $item ):
$product_id   = $item->get_product_id();
$variation_id = $item->get_variation_id();
$item_name    = $item->get_name(); // Name of the product
$quantity     = $item->get_quantity();  

$product        = $item->get_product(); // Get the WC_Product object

$product_price  = $product->get_price();

endforeach;

$print_file = get_post_meta( $product_id, 'print_file_url', true );

// Raw output for testing
echo 'Product Price<pre> '; print_r( $product_price ); echo '</pre>';
echo 'Product Name<pre> '; print_r( $item_name ); echo '</pre>';
echo 'Product Quantity<pre> '; print_r( $quantity ); echo '</pre>';
echo 'Product ID<pre> '; print_r( $product_id ); echo '</pre>';
echo 'Variation ID<pre> '; print_r( $variation_id ); echo '</pre>';
echo 'Print File Url<pre> '; print_r( $print_file ); echo '</pre>';
echo 'Order Status<pre>'; print_r( $order_status ); echo '</pre>';
echo 'Latest Order ID<pre>'; print_r( $latest_order_id ); echo '</pre>';
echo 'Order Details<pre>'; print_r( $order_details ); echo '</pre>';

使用此函數獲取最后訂單 ID

$last_order_id = wc_get_customer_last_order($user_id);
$order = wc_get_order( $order_id );
$order->get_items();
foreach ($order->get_items() as $item_key => $item ){
    // Item ID is directly accessible from the $item_key in the foreach loop or
    $item_id = $item->get_id();
    ## Using WC_Order_Item_Product methods ##
    $product      = $item->get_product(); // Get the WC_Product object
    $product_id   = $item->get_product_id(); // the Product id
    $variation_id = $item->get_variation_id(); // the Variation id
    $item_type    = $item->get_type(); // Type of the order item ("line_item")
    $item_name    = $item->get_name(); // Name of the product
    $quantity     = $item->get_quantity();  
    $tax_class    = $item->get_tax_class();
    $line_subtotal     = $item->get_subtotal(); // Line subtotal (non discounted)
    $line_subtotal_tax = $item->get_subtotal_tax(); // Line subtotal tax (non discounted)
    $line_total        = $item->get_total(); // Line total (discounted)
    $line_total_tax    = $item->get_total_tax(); // Line total tax (discounted)
    $product        = $item->get_product(); // Get the WC_Product object
    $product_type   = $product->get_type();
    $product_sku    = $product->get_sku();
    $product_price  = $product->get_price();
    $stock_quantity = $product->get_stock_quantity();
}

嘗試這個

function get_names( $order_id ) {
    $order = wc_get_order( $order_id );
    if (empty($order)) return false;
    $items = $order->get_items();
    foreach ( $items as $item ) {
        $item_name    = $item->get_name();
    }
    return $item_name;
}

謝謝

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM