'woocommerce set_manage_stock not work in woocommerce_process_product_meta action
I try to change the stock fileds programmatically , But it fail woth some fileds.
My question is: Why the set_manage_stock in the action not work , the set_stock_status work:
add_action('woocommerce_process_product_meta', function($post_id) {
$product = wc_get_product($post_id);
if( $_POST['_myvariable']==='yes'){
$product->set_manage_stock(false);
$product->set_stock_status('instock');
$product->set_stock_quantity(99);
}
else{
$product->set_manage_stock(true);
$product->set_stock_status('instock');
$product->set_stock_quantity(10);
}
$product->save();
}
Solution 1:[1]
Tested OK with WooCommerce 6.3. This will create a product automatically when you add a new product from the admin.
/**
* Save meta box data.
*
* @param int $post_id WP post id.
* @param WP_Post $post Post object.
*/
function action_woocommerce_process_product_meta($post_id, $post) {
// make action magic happen here...
$product = new WC_Product_Simple();
$product->set_name('Sample product ');
$product->set_status('publish');
$product->set_catalog_visibility('visible');
$product->set_price(19.99);
$product->set_regular_price(19.99);
$product->set_sold_individually(true);
$product->set_manage_stock(true);
$product->set_stock_status('instock');
$product->set_stock_quantity(10);
$product->save();
}
// add the action
add_action('woocommerce_process_product_meta', 'action_woocommerce_process_product_meta', 10, 2);
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | mujuonly |
