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>';
}
Vous devez appeler la fonction à plusieurs reprises ou réécrire. – Twinfriends
vous pouvez vérifier tout produit if (woo_in_cart (123)) { // Le produit est déjà dans le panier } –
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