簡體   English   中英

如何在woocommerce中讀取vartaion產品的庫存數量

[英]How to read stock quantity of a vartaion product in woocommerce

我想在創建新產品后將產品的庫存數量及其變體以及 Product_id(Post ID) 存儲在自定義表中。 為此,我使用 'transition_post_status' 鈎子 & 讀取股票價值,我使用 wp_postmeta 表$prodcut_qty = get_post_meta($post->ID,'_stock'); .

假設有產品 A(庫存數量 = 10)和 A1(庫存數量 = 10),A2(庫存數量 = 20),A3(庫存數量 = 30)是 A 的變體,所以我想存儲上述所有細節在自定義表中。

My DB schema is {post_id bigint (20), user_id bigint (20), stock_quantity int (11),log_date DATE}

我無法讀取產品變化的庫存值。 我的代碼是 -

function insert_custom_table($new_status, $old_status, $post ) 
{
    
    global $post;
    global $wpdb;
    global $current_user;
    
    if(isset($post->ID))
    {
        $current_post = $post->ID;
    }
   wp_get_current_user();
    $user_id = $current_user->ID;
    
    if ( $post->post_type !== 'product' ) {
        return;
    }else{

            if ( 'publish' !== $new_status or 'publish' === $old_status ){
                //Check if someone published a post first time.
                return;
            }else{

                $prodcut_qty = get_post_meta($post->ID,'_stock');
               //Unable to read stock values for product variations.
                    
            }

    }      
    $stock_notes = 'New Stock';
    $wpdb->insert( 'wp_woocommerce_stock_custom', array( 'post_ID' => $current_post, 'user_ID' => $user_id, 'stock_quantity'=> $prodcut_qty ,'notes'=> $stock_notes) );


}

我正在使用 woocommerce 5.5.2

更新 :

我發現如果我們想在發布帖子后立即讀取 postmeta 表,對於這種類型的任務鈎子added_post_meta工作正常。

我做了一些測試,這應該可以解決問題

add_action( 'woocommerce_product_set_stock', 'wc_updated_product_stock_callback' );
add_action( 'woocommerce_variation_set_stock', 'wc_updated_product_stock_callback' );

function wc_updated_product_stock_callback( $product_id ) {
    // get an instance of the WC_Product Object
    $product      = wc_get_product( $product_id );
    $stock_qty    = $product->get_stock_quantity();

    // Here add your functionality
    
    error_log('Quantity is '.$stock_qty ); // For testing before working with DB
}

我得到了解決方案:

我發現如果我們想在發布帖子后立即讀取 postmeta 表,對於這種類型的任務鈎子 added_post_meta 工作正常。

我修改了我之前的代碼

function insert_custom_table($meta_id, $post_id, $meta_key, $meta_value )
{
if(get_post_type( $post_id ) == 'product' && get_post_status( $post_id ) == 'publish')
        {
            $product_id = $post_id;
            $tablename = $wpdb->prefix.'woocommerce_stock_master';
            if(!$product_id)
            {
                return;
            }  

            wp_get_current_user();
            $user_id = $current_user->ID;

            // get an instance of the WC_Product Object
            // get product type
            // get stock _manage  
            if ($product->is_type( 'variable' )){
                 //$product_id = $product->get_id(); 
                
                $variations = $product->get_children(); // get variations
                foreach ($variations as $variations_value) {
                    $single_variation = new WC_Product_Variation($variations_value);
                    $single_variation_stock = $single_variation->get_stock_quantity();
                    
                    // insert into db 
                   
                }
           }
           if ($product->is_type( 'simple' )){
                if($manage_stock == 'yes')
                {
                  //insert in to db
                }
            }
        }else{
            return;
        }
        error_log('Quantity is '.$stock_qty);  
    }
    add_action( 'added_post_meta', 'insert_custom_table',10,4);

暫無
暫無

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

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