簡體   English   中英

從WooCommerce中的產品中檢索庫存數量的功能

[英]Function that retrieves the stock quantity from a product in WooCommerce

我正在嘗試創建一個函數,該函數從WooCommerce中的產品中檢索庫存數量。 如果我注釋掉以下行,則代碼將運行,但如果將其保留在白屏中:$ product = is_a($ product,'WC_Product')嗎? $ product:wc_get_product($ product_id);

有任何想法我在這里做錯了嗎?

function wc_get_variable_product_stock_quantity( $output = 'raw', $product_id = 0 ){
    global $wpdb, $product;

    // Get the product ID (can be defined)
    $product_id = $product_id > 0 ? $product_id : get_the_id();

    // Check and get the instance of the WC_Product Object
    $product = is_a( $product, 'WC_Product' ) ? $product : wc_get_product($product_id);
    $stock_quantity = $product->get_stock_quantity();

    return $stock_quantity;

}

編輯(更新):

我的最終目標是對訂單狀態更改完成后所有變體的庫存量求和,然后將總數寫入母產品的庫存量,以便總庫存量始終是最新的。

我上面的過度簡化示例實際上應該看起來像以下代碼,主要基於“ 從Woocommerce中從可變產品中獲取所有變體的所有變體的總庫存 ”答案代碼,並且變化不大 (請參見其他SQL語句)

    function wc_get_variable_product_stock_quantity( $output = 'raw', $product_id = 0 ){
        global $wpdb, $product;

        // Get the product ID (can be defined)
        $product_id = $product_id > 0 ? $product_id : get_the_id();

        // Check and get the instance of the WC_Product Object
        $product = is_a( $product, 'WC_Product' ) ? $product : wc_get_product($product_id);
        // Get the stock quantity sum of all product variations (children)
        $stock_quantity = $wpdb->get_var("
            SELECT SUM(pm.meta_value)
            FROM {$wpdb->prefix}posts as p
            JOIN {$wpdb->prefix}postmeta as pm ON p.ID = pm.post_id
            WHERE p.post_type = 'product_variation'
            AND p.post_status = 'publish'
            AND p.post_parent = '$product_id'
            AND pm.meta_key = '_stock'
            AND pm.meta_value IS NOT NULL
        ");   
        return $stock_quantity;
    }


// Update stock totals for parent product when order status changes to completed...
function mysite_woocommerce_order_status_completed( $order_id ) {

    // For each item in the order, calculate the total stock quantity for all variations and write the total to the parent products's stock quantity
    $order = wc_get_order( $order_id );
    $order_id = $order->get_id();

    $log_txt = '';
    $items = $order->get_items();
    foreach($items as $k=>$val){     
        $product_id = $val[‘product_id’];
        $total_stock_quantity = wc_get_variable_product_stock_quantity( 'raw', $product_id );
        $log_txt .= 'Product ID:'.$val['product_id'].', Name: '.$val['name'].', Total Stock: '.$total_stock_quantity.'\n<br>';
        // Next: add code to write the stock total to the parent product
    }
}

add_action( 'woocommerce_order_status_completed', 'mysite_woocommerce_order_status_completed', 10, 1 );

您說您想創建一個從WooCommerce中的產品中檢索庫存數量的函數,因此為什么不直接使用其中之一

  • 對於產品對象實例,請使用WC_Product方法get_stock_quantity()
  • 或使用產品ID使用get_post_meta( $product_id, '_stock', true )

現在,基於“ 從Woocommerce中從可變產品中獲取所有變體的全部庫存 ”答案,您的代碼具有一些不需要的東西,並且可以以更加輕巧,緊湊和有效的方式進行更改

function wc_get_variable_product_stock_quantity( $product_id = 0 ){
    // Get the product ID (can be defined)
    $product_id = $product_id > 0 ? $product_id : get_the_ID();

    return get_post_meta( $product_id, '_stock', true );
}

代碼進入活動子主題(或活動主題)的function.php文件中。 它應該更好地工作。

或者,您可以直接使用:

 get_post_meta( $product_id, '_stock', true ); 

要么

 get_post_meta( get_the_ID(), '_stock', true ); 

暫無
暫無

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

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