Thanks to 7uc1f3r, I was able to programatically assign tags to products based on their stock status. Link here
However, I have run into an issue. There is no error. At times, I have to save the product twice for the correct tag to reflect. I have tried the code using else
& elseif
but the issue persists. If someone could guide me to use switch
case
, I can try that & see if the tags get assigned correctly on the first save. Alternatively, if there is information on why this would occur & possible remedies, please let me know. Looks like the product is in the process of saving in the backend & the tag gets assigned prior to that but I don't know for sure. Please advise.
add_action( 'woocommerce_admin_process_product_object', 'mycode_woocommerce_stockstatus_tag', 10, 1 );
function mycode_woocommerce_stockstatus_tag ($product) {
if ( !$product->is_in_stock() ) {
$product->set_tag_ids ( array (113) );
}
elseif ( $product->is_on_backorder(1) ) {
$product->set_tag_ids ( array (111) );
}
elseif ( $product->is_in_stock() && !$product->is_on_backorder(1) ) {
$product->set_tag_ids ( array (112) );
}
}
Aucun commentaire:
Enregistrer un commentaire