Bonjour,
Ma configuration WP actuelle
- Version de PHP/MySQL : 7.4
- Thème utilisé : Swiss Delight
- Extensions en place : woocommerce
- Nom de l’hébergeur : O2Switch
- Adresse du site :
Problème(s) rencontré(s) :
Bonjour,
J’ai ajouté à mon fichier functions.php de mon thème enfant du code permettant à un client qui effectue sur le site un achat d’un montant minimum de 1€, de recevoir un produit offert (code récupéré dans ce forum). Jusque là tout va bien. Cependant, je souhaite appliquer certaines conditons notamment :
- Exclure la e-carte cadeau de cette offre.
- Exclure la méthode de livraison « Retrait en magasin »)
- Valable pour les 10 premières commandes (c’est un nouveau site).
Pouvez-vous svp m’aider pour ajouter une ligne de code pour se faire ?
Merci de votre aide.
Voici ce que j’ai actuellement
function ferman_add_product_to_cart() {
global $woocommerce;
$cart_total = 1;
$free_product = wc_get_product( 16307 );
$total = WC()->cart->total;
$free_product_id = $free_product->get_ID(); // Product Id of the free product which will get added to cart
$free_product_price = $free_product->get_regular_price($free_product_id);
$free_product_reduced_price = $free_product->get_sale_price($free_product_id);
$found = false;
//check if product already in cart
if ( sizeof( WC()->cart->get_cart() ) > 0 ) {
foreach ( WC()->cart->get_cart() as $cart_item_key => $values ) {
$product = $values['data'];
if ( $product->get_id() == $free_product_id )
$found = true;
}
// if product not found, add it
if ( ! $found && $woocommerce->cart->total >= $cart_total ){
WC()->cart->add_to_cart( $free_product_id );
$total = total;
}
elseif ($found && $woocommerce->cart->total-$free_product_price < $cart_total ){
$product_cart_id = WC()->cart->generate_cart_id( $free_product_id );
$cart_item_key = WC()->cart->find_product_in_cart( $product_cart_id );
if ( $cart_item_key ) {WC()->cart->remove_cart_item( $cart_item_key );}
else {return;}
}
}
else {
return;
}
}
add_action( 'template_redirect', 'ferman_add_product_to_cart' );
Ajout automatique d'un produit offert dans panier avec montant minimum