【问题标题】:How to read stock quantity of a vartaion product in woocommerce如何在 woocommerce 中读取 vartaion 产品的库存数量
【发布时间】:2021-11-09 13:34:29
【问题描述】:

我想在创建新产品后将产品的库存数量及其变体与 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 工作正常。

【问题讨论】:

  • 你用什么钩子?你应该使用 woocommerce_update_product 钩子hookr.io/actions/woocommerce_update_product
  • @MartinMirchev 我正在使用 transition_post_status' 钩子 & 我正在检查帖子类型为 'product' 像这样 $post->post_type !== 'product'

标签: php wordpress woocommerce


【解决方案1】:

我做了一些测试,这应该可以解决问题

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
}

【讨论】:

  • 谢谢马丁,我测试了这个钩子'woocommerce_product_set_stock',当库存值改变时它总是有效的。由于存储在我的自定义主表中的这个值也发生了变化。我不想在每次交易后更新我的主表。所以我要求在产品创建时间读取股票价值。我正在尝试创建一个库存检查系统,该系统将监控 WooCommerce 内置库存管理的行为
  • 嗨,马丁,我在 woocommerce 中找到了用于读取变体产品库存数量的挂钩 'add_post_meta'。它工作正常。感谢 error_log() 的想法。
【解决方案2】:

我得到了解决方案:

我发现如果我们想在发布帖子后立即读取 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);

【讨论】:

  • 我使用了 added_post_meta 动作钩子。
猜你喜欢
  • 2018-07-20
  • 1970-01-01
  • 2017-04-17
  • 2020-10-02
  • 2018-05-25
  • 2016-05-06
  • 2015-12-26
  • 1970-01-01
  • 2019-07-10
相关资源
最近更新 更多