2016-12-21 3 views
2

Je trouve ce grand extrait de ce websiteWooCommerce - Vérifiez si l'article de sont déjà dans le panier

Ce qui suit est la fonction pour vérifier si un produit spécifique existe dans le panier:

 function woo_in_cart($product_id) { 
     global $woocommerce;   
     foreach($woocommerce->cart->get_cart() as $key => $val) { 
      $_product = $val['data']; 

      if($product_id == $_product->id) { 
       return true; 
      } 
     }   
     return false; 
     } 

Et cela à utiliser partout nécessaire:

 if(woo_in_cart(123)) { 
    // Product is already in cart 
    } 

Le problème est de savoir comment l'utiliser pour vérifier plusieurs produits comme celui-ci:

 if(woo_in_cart(123,124,125,126...)) { 
    // Product is already in cart 
    } 

Merci.

source

+0

Vous devez appeler la fonction à plusieurs reprises ou réécrire. – Twinfriends

+0

vous pouvez vérifier tout produit if (woo_in_cart (123)) { // Le produit est déjà dans le panier } –

+0

J'ai besoin de ceci pour être de cette façon de vérifier plusieurs produits: if (woo_in_cart (123,124,125,126 ...)) { // Le produit est déjà dans le panier } – mysticalghoul

Répondre

7

est ici une fonction personnalisée avec un argument qui accepte un ID de produit unique entier ou un tableau d'ID de produit, et qui retourne le nombre de Ids appariés qui sont dans le panier:

function matched_cart_items($product_ids) { 

    if(!WC()->cart->is_empty()): 

     // Initialise the count 
     $count = 0; 

     foreach(WC()->cart->get_cart() as $cart_item): 

      $items_id = $cart_item['product_id']; 

      // For an array of product IDS 
      if(is_array($product_ids) && in_array($items_id, $product_ids)) 
       $count++; // incrementing the counted items 

      // for a unique product ID (integer or string value) 
      if($product_ids == $items_id) 
       $count++; // incrementing the counted items 

     endforeach; 

     // returning counted items 
     return $count; 

    endif; 
} 

Ce code va dans le fichier function.php de votre thème enfant actif (thème actif ou dans un fichier plugin).

Le code a été testé et fonctionne.


USAGE:

1) pour un ID de produit unique (entier):

$product_id = 102; 

// Usage as a condition in an if statement 
if(0 < matched_cart_items($product_ids)){ 
    echo '<p>There is "'. matched_cart_items($product_ids) .'"matched items in cart</p><br>'; 
} else { 
    echo '<p>NO matched items in cart</p><br>'; 
} 

2) Pour un tableau d'ID de l'article:

$product_ids = array(102,107,118); 

// Usage as a condition in an if statement 
if(0 < matched_cart_items($product_ids)){ 
    echo '<p>There is "'. matched_cart_items($product_ids) .'"matched items in cart</p><br>'; 
} else { 
    echo '<p>NO matched items in cart</p><br>'; 
} 

3) Pour un tableau d'ID de produit pour 3 éléments de panier assortis ou plus, par exemple:

$product_ids = array(102, 107, 118, 124, 137); 

// Usage as a condition in an if statement (for 3 matched items or more) 
if(3 <= matched_cart_items($product_ids)){ 
    echo '<p>There is "'. matched_cart_items($product_ids) .'"matched items in cart</p><br>'; 
} else { 
    echo '<p>NO matched items in cart</p><br>'; 
} 
+0

Désolé pour la réponse tardive ne fonctionne pas pour moi. $ product_ids = array (7657,7995,8000,7999,7998,7997,7996); if (0 mysticalghoul

+0

@mysticalghoul Ok laissez-moi vérifier et retester-le sur mon serveur de test, cela devrait être résolu rapidement (l'idée de ce script est là) ... Si vous aimez nous pouvons discuter Skype (mon ID est "Marsloic") comme le chat ici est trop préhistorique ... – LoicTheAztec

+0

Merci monsieur beau travail ... – mysticalghoul

2

Cas 1: Passez tableau comme argument.

function woo_in_cart($arr_product_id) { 
     global $woocommerce;   
     $cartarray=array(); 

     foreach($woocommerce->cart->get_cart() as $key => $val) { 
      $_product = $val['data']; 
      array_push($cartarray,$_product->id); 
     }   
     $result = !empty(array_intersect($cartarray,$arr_product_id)); 
     return $result; 

     } 

Comment appeler la fonction

$is_incart=array(2,4,8,11); 
print_r(woo_in_cart($is_incart)); 

Cas n ° 2: Utilisez le code que vous exécutez.

$is_in_product_cart=array(123,124,125,126,..); 

foreach($is_in_product_cart as $is_in_cart) 
    if(woo_in_cart($is_in_cart)) 
    { 
     // Product is already in cart 
    } 
} 
+0

pouvez-vous s'il vous plaît montrer le code complet dans la fonction() – mysticalghoul

+0

"Ne peut utiliser la valeur de retour de fonction" – mysticalghoul

+0

s'il vous plaît vérifier le deuxième code –

0

Une erreur s'est produite dans la fonction woo_in_cart. Voici le correct:

function woo_in_cart($arr_product_id) { 
    global $woocommerce; 
    $cartarray=array(); 

    foreach($woocommerce->cart->get_cart() as $key => $val) { 
     $_product = $val['product_id']; 
     array_push($cartarray,$_product); 
    } 

    if (!empty($cartarray)) { 
     $result = array_intersect($cartarray,$arr_product_id); 
    } 

    if (!empty($result)) { 
     return true; 
    } else { 
     return false; 
    }; 

} 

Voici un exemple d'utilisation:

//Set IDs Array variable 

$my_products_ids_array = array(22,23,465); 
if (woo_in_cart($my_products_ids_array)) { 
    echo 'ohh yeah there some of that products in!'; 
}else { 
    echo 'no matching products :('; 
}