Si rien de tout cela n’est sélectionné, la taxonomie sera simplement ignorée.
Explications
The hook of action polaris_update_product
uses the product identifier WooCommerce $product_id
and the Polaris data $polaris_data
.
We therefore use the function add_action()
with the default priority (10) and 2 as the number of expected parameters.
add_action( 'polaris_update_product', 'NOM_DE_LA_FONCTION', 10, 2 );
In this example, we check that the Polaris data does not contain an attribute medias
or that it is empty and that the current product is not in draft:
if ( ( ! isset( $polaris_data['medias'] ) || ! count( $polaris_data['medias'] ) ) && get_post_status( $product_id ) !== 'draft' ) {
// Faire quelquechose
}
Here, we want to make it a draft
$product = wc_get_product( $product_id ); // Récupération du produit WooCommerce
$product->set_status( 'draft' ); // Affectation de l'état
$product->save(); // Enregistrement
A similar process can be used to (re)publish products that have an image.
You can use the following code in the file functions.php
of your theme or in your custom extension.
add_action( 'polaris_update_product', function ( $product_id, $polaris_data ) {
if ( ( ! isset( $polaris_data['medias'] ) || ! count( $polaris_data['medias'] ) ) && get_post_status( $product_id ) !== 'draft' ) {
$product = wc_get_product( $product_id );
$product->set_status( 'draft' );
$product->save();
}
if ( isset( $polaris_data['medias'] ) && count( $polaris_data['medias'] ) && get_post_status( $product_id ) !== 'publish' ) {
$product = wc_get_product( $product_id );
$product->set_status( 'publish' );
$product->save();
}
}, 10, 2 );