2

So basically, I'm trying to filter my cart. I would like the message below displayed if products from the "cuvees" category are at the number of 4,5,7,8,9,10,11,13,14,15,16,17,19,21 in the cart.

So far here what I've did but it only works for one value : 7 . Do I need to put an array when I declare the function ?

add_action( 'woocommerce_check_cart_items', 'check_total' );
    function check_total() {
        // Only run in the Cart or Checkout pages
        if( is_cart() || is_checkout() ) {

            global $woocommerce, $product;
            $i=0;
            //$prod_id_array = array();
            //loop through all cart products
            foreach ( $woocommerce->cart->cart_contents as $product ) :


                // Set checking if there's y products in cuvees cart total
                $cart_product_total = 4;



                // See if any product is from the cuvees category or not
                if ( has_term( 'cuvees', 'product_cat', $product['product_id'] ) ) :

                    $total_quantity += $product['quantity'];
                    //array_push($prod_id_array, $product['product_id']);
                endif;

            endforeach;

            foreach ( $woocommerce->cart->cart_contents as $product ) :
                if ( has_term( 'cuvees', 'product_cat', $product['product_id'] ) ) :
                    if( $total_quantity == $cart_product_total && $i == 0 ) {
                        // Display our error message
                        wc_add_notice( sprintf( '<h5 style="letter-spacing:0.5px;color:white;text-align:center;">/!\&nbsp;    Une commande de %s bouteilles n&#39;est pas possible&nbsp;! &nbsp;   /!\ </h5><br /> <br /><p style="text-align:center;"> L&#39;envoi n&#39;est possible que pour 1 | 2 | 3 | 6 | 12 | 18 | 24 | 30 | 36 | 42 | 48 | 54 | 60  | 72 | 96 | 120 et plus bouteilles.</p>',
                            $cart_product_total,
                            $total_quantity ),
                        'error' );
                    }
                    $i++;
                endif;
            endforeach;
        }
    }        

Thank you :)

1 Answer 1

0

I'm not sure how the error message is being displayed for any number, as throughout most of the script you use the alternative structure for if statements and foreach statements (using : instead of {}) then at the bottom you mix in an if(){} inside an if : endif which is not allowed, see here for more info:

http://php.net/manual/en/control-structures.alternative-syntax.php

I have restructured your code a little but can't test it, so give this a try:

add_action( 'woocommerce_check_cart_items', 'check_total' );
function check_total() {
    // Only run in the Cart or Checkout pages
    if( is_cart() || is_checkout() ) {

        global $woocommerce, $product;

        $total_quantity = 0;
        $display_notice = 1;
        $i = 0;
        //loop through all cart products
        foreach ( $woocommerce->cart->cart_contents as $product ) {

            // See if any product is from the cuvees category or not
            if ( has_term( 'cuvees', 'product_cat', $product['product_id'] )) {
                $total_quantity += $product['quantity'];
            }

        }
        // Set up the acceptable totals and loop through them so we don't have an ugly if statement below.
        $acceptable_totals = array(1, 2, 3, 6, 12, 18, 24, 30, 36, 42, 48, 54, 60, 72, 96, 120);

        foreach($acceptable_totals as $total_check) {
            if ( $total_check == $total_quantity ) { $display_notice = 0; } 
        }

        foreach ( $woocommerce->cart->cart_contents as $product ) {
            if ( has_term( 'cuvees', 'product_cat', $product['product_id'] ) ) {
                if( $display_notice == 1 && $i == 0 ) {
                    // Display our error message
                    wc_add_notice( sprintf( '<h5 style="letter-spacing:0.5px;color:white;text-align:center;">/!\&nbsp;    Une commande de %d bouteilles n&#39;est pas possible&nbsp;! &nbsp;   /!\ </h5><br /> <br /><p style="text-align:center;"> L&#39;envoi n&#39;est possible que pour 1 | 2 | 3 | 6 | 12 | 18 | 24 | 30 | 36 | 42 | 48 | 54 | 60  | 72 | 96 | 120 et plus bouteilles.</p>', $total_quantity),
                    'error' );
                }
                $i++;
            }
        }
    }
}

EDIT - Small mistake in display notice logic.

EDIT2 - Updated code to return current number of products in error message.

3
  • Thanks a lot for the answer ! It works yes except to display the %s of the number of products in the cart (in the cuvees category ) -- it displays ARRAY instead of the number
    – nmnmnm
    Feb 28, 2015 at 5:55
  • Ah, I don't speak French so missed that part, I've made an edit and it should work now. Feb 28, 2015 at 6:02
  • @nmnmnm did this sort it out for you? If so can you accept the answer, thanks. Feb 28, 2015 at 20:46

Not the answer you're looking for? Browse other questions tagged or ask your own question.