Accueil 2026 › Forums › How to WooCommerce › WooCommerce – Changer de catégorie quand produit acheté
Étiqueté : woocommerce
- Ce sujet contient 2 réponses, 3 participants et a été mis à jour pour la dernière fois par
Robert Miller, le il y a 1 mois.
-
AuteurMessages
-
11 mai 2023 à 9 h 05 min #500446
Bonjour,
Je souhaite que lorsqu’un de mes produits est acheté, il change de catégorie (passe de en stock à résultats).
Voici mon code actuel :
add_action(‘woocommerce_order_status_changed’, ‘add_category_to_order_items_on_completed_status’ ,10, 1);
function add_category_to_order_items_on_completed_status( $order_id ) {
// la catégorie dans laquelle on achète
$your_category = ‘en-stock’;
// on récupère chaque commande
$order = wc_get_order( $order_id );
// si le statut de la commande est « complète »
if ( $order->has_status( ‘completed’ ) ) {
// pour chaque produit acheté
foreach ( $order->get_items() as $item_id => $product_item ) {
$product_id = $product_item->get_product_id();
// mettre le produit dans la catégorie résultats
wp_set_object_terms( $product_id, $your_category, ‘resultats’ );
} }}
...24 novembre 2025 à 6 h 53 min #545581It looks like the logic is almost there, but
wp_set_object_terms()is being used incorrectly. The second parameter should be the category slug you want to assign, and the third parameter should always be ‘product_cat’ for WooCommerce product categories — not the name of your target category.Also, if you want to move the product from one category to another, you need to remove the old category and assign the new one, not pass the old category into the function.
Here’s a corrected version:
add_action(‘woocommerce_order_status_changed’, ‘move_product_to_results_on_completed’, 10, 4);
function move_product_to_results_on_completed($order_id, $old_status, $new_status, $order){
if( $new_status !== ‘completed’ ) return;
// category slugs
$old_category = ‘in-stock’;
$new_category = ‘results’;
foreach ($order->get_items() as $item) {
$product_id = $item->get_product_id();
// Remove old category and assign new one
wp_remove_object_terms($product_id, $old_category, ‘product_cat’);
wp_set_object_terms($product_id, $new_category, ‘product_cat’, true);
}
}
...Enthusiastic canine advocate and proud pet parent
5 décembre 2025 à 11 h 01 min #546185Bonjour Laure,
Je pense que le problème vient de l’ordre des arguments dans
wp_set_object_terms. Le troisième argument doit être la taxonomie et non la catégorie cible. Donc si tu veux déplacer le produit vers la catégorieresultats, tu devrais faire :wp_set_object_terms( $product_id, 'resultats', 'product_cat' );
Si tu veux supprimer l’ancienne catégorie
en-stocket mettre seulementresultats, tu peux ajouter le paramètretruepour écraser :wp_set_object_terms( $product_id, 'resultats', 'product_cat', true );
Ton hook
woocommerce_order_status_changedet la vérification du statutcompletedsont corrects, donc après cette modification, ça devrait fonctionner comme attendu.N’hésite pas à tester sur un produit pour vérifier avant de passer sur tout le stock.
...Web-entrepreneur focused on building and optimizing WooCommerce stores. I’m passionate about WordPress, e-commerce growth and sharing tips on themes, plugins & performance. Looking forward to contributing and learning from the community.
-
AuteurMessages
- Vous devez être connecté pour répondre à ce sujet.