[英]How to read stock quantity of a vartaion product in woocommerce
I want to store stock quantity of product & its variations along with Product_id(Post ID) in a custom table after the creation of a new product.我想在创建新产品后将产品的库存数量及其变体以及 Product_id(Post ID) 存储在自定义表中。 for this i am using 'transition_post_status' hook & for reading the stock value i am using wp_postmeta table
$prodcut_qty = get_post_meta($post->ID,'_stock');
为此,我使用 'transition_post_status' 钩子 & 读取股票价值,我使用 wp_postmeta 表
$prodcut_qty = get_post_meta($post->ID,'_stock');
. .
Suppose that there is product A(stock quantity = 10) & A1 (stock quantity = 10) ,A2(stock quantity = 20),A3(stock quantity = 30) are variations of A.So i want to store all the above details in custom table.假设有产品 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}
I am unable to read stock values for product variation.我无法读取产品变化的库存值。 My Code is -
我的代码是 -
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) );
}
I am using woocommerce 5.5.2我正在使用 woocommerce 5.5.2
Update :更新 :
I found that if we want to read postmeta table immediate after publish the post,for this type of task hook added_post_meta works fine.我发现如果我们想在发布帖子后立即读取 postmeta 表,对于这种类型的任务钩子added_post_meta工作正常。
I did some tests and this should do the trick我做了一些测试,这应该可以解决问题
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
}
I got the solution:我得到了解决方案:
I found that if we want to read postmeta table immediate after publish the post,for this type of task hook added_post_meta works fine.我发现如果我们想在发布帖子后立即读取 postmeta 表,对于这种类型的任务钩子 added_post_meta 工作正常。
I modified my previous code我修改了我之前的代码
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.