Si vous souhaitez ajouter automatiquement un produit au panier lors de la visite d’un internaute dans votre boutique Woocommerce.
Ajouter le code suivant dans le fichier function.php de votre thème WordPress :
// Ajouter un produit automatiquement add_action( 'init', 'add_product_to_cart' ); function add_product_to_cart() { if ( ! is_admin() ) { global $woocommerce; $product_id = 22; $found = false; //On vérifie si il y a déja un produit dans le panier if ( sizeof( $woocommerce->cart->get_cart() ) > 0 ) { foreach ( $woocommerce->cart->get_cart() as $cart_item_key => $values ) { $_product = $values['data']; if ( $_product->id == $product_id ) $found = true; } if ( ! $found ) $woocommerce->cart->add_to_cart( $product_id ); } else { $woocommerce->cart->add_to_cart( $product_id ); } } }
A la ligne 6 $product_id = 22 remplacer 22 par l’identifiant du produit à ajouter automatiquement.