WooFrance Dépannage et support WordPress WooCommerce Forums Forum de Support WooCommerce Bloquer panier si quantité catégorie inférieur à X

Affichage de 1 message (sur 1 au total)
  • Auteur
    Messages
  • #203245
    perdixo75
    Participant
    Points: 18 pts

    Bonjour à tous,

    Sur ma boutique en ligne, j’ai plusieurs catégories. Si l’utilisateur ajoute dans son panier au moins 1 produit de la catégorie LAMBDA, j’aimerais pouvoir faire la chose suivante :

    – Si il y a au moins 1 produit de la catégorie LAMBDA dans le panier : bloquer le paiement si l’utilisateur à moins de 3 objets de cette catégorie et afficher une notice

    Pour l’instant, j’ai créé une fonction mais le problème est que si l’utilisateur essaie d’acheter des produits dans une autre catégorie, la fonction s’applique également. J’aimerais pouvoir bloquer simplement pour cette catégorie !

    Voici ma fonction actuelle :

    
    /**
     * Renders notices and prevents checkout if the cart
     * does not contain a minimum number of products in a category
     */
    function sww_check_category_for_minimum()
    {
    
        // set the minimum quantity in the category to purchase
        $min_quantity = 3;
    
        // set the id of the category for which we're requiring a minimum quantity
        $category_id = 40;
    
        // get the product category
        $product_cat = get_term($category_id, 'product_cat');
        $category_name = '' . $product_cat->name . '';
    
        // get the quantity category in the cart
        $category_quantity = sww_get_category_quantity_in_cart($category_id);
    
        if ($category_quantity < $min_quantity) {
            // render a notice to explain the minimum
            wc_add_notice(sprintf('Vous devez commander au moins %1$s produits de la catégorie %2$s pour pouvoir profiter de la livraison par coursier!', $min_quantity, $category_name), 'error');
        }
    }
    add_action('woocommerce_check_cart_items', 'sww_check_category_for_minimum');
    
    /**
     * Returns the quantity of products from a given category in the WC cart
     *
     * @param int $category_id the ID of the product category
     * @return in $category_quantity the quantity of products in the cart belonging to this category
     */
    function sww_get_category_quantity_in_cart($category_id)
    {
    
        // get the quantities of cart items to check against
        $quantities = WC()->cart->get_cart_item_quantities();
    
        // start a counter for the quantity of items from this category
        $category_quantity = 0;
    
        // loop through cart items to check the product categories
        foreach ($quantities as $product_id => $quantity) {
    
            $product_categories = get_the_terms($product_id, 'product_cat');
    
            // check the categories for our desired one
            foreach ($product_categories as $category) {
    
                // if we find it, add the line item quantity to the category total
                if ($category_id === $category->term_id) {
                    $category_quantity += $quantity;
                }
            }
        }
    
        return $category_quantity;
    }
    depanagewp
Affichage de 1 message (sur 1 au total)
  • Vous devez être connecté pour répondre à ce sujet.