问题
Hi I'm facing an issue regarding wordpress and WooCommerce
I want to automate the process"when ever product goes out of stock it will automatically added to some other specific category and a tag 'out of stock is assigned to it"
like these images attached:
- When product out of stock
- Product added auto to specified category
- I can show that in this category widget
- In the end also a tag assigned to it
I just need the taxonomy to be applied in the edit page
I do this manual process every time when the product run out of stock
- I go to product edit page-> add it to a product category named 'out-of-stock'
- and then assign a tag 'out of stock' to it
I want to automate that. I tried every solution but failed, even I tried many plugins
Can someone help me or there is any other way of doing this from the backend or by adding any custom function to wordpress?
回答1:
The available hooks after stock change events, triggers emails and adds order notes are located in
https://github.com/woocommerce/woocommerce/blob/master/includes/wc-stock-functions.php
Where we find the function
wc_trigger_stock_change_notifications()
which contains some action hooks
Available hooks:
// No stock
function action_woocommerce_no_stock( $wc_get_product ) {
// make action magic happen here...
}
add_action( 'woocommerce_no_stock', 'action_woocommerce_no_stock', 10, 1 );
// Low stock
function action_woocommerce_low_stock( $wc_get_product ) {
// make action magic happen here...
}
add_action( 'woocommerce_low_stock', 'action_woocommerce_low_stock', 10, 1 );
// On backorder
function action_woocommerce_product_on_backorder( $array ) {
// make action magic happen here...
}
add_action( 'woocommerce_product_on_backorder', 'action_woocommerce_product_on_backorder', 10, 1 );
So for what you want you can use woocommerce_no_stock
and CRUD Objects in 3.0
function action_woocommerce_no_stock( $wc_get_product ) {
// Set category ids
$wc_get_product->set_category_ids( array( 39, 2 ) );
// Product set tag ids
$wc_get_product->set_tag_ids( array( 40 ) );
// Save
$wc_get_product->save();
}
add_action( 'woocommerce_no_stock', 'action_woocommerce_no_stock', 10, 1 );
来源:https://stackoverflow.com/questions/62184276/automatically-add-a-specific-category-and-tags-to-a-product-when-it-goes-out-of