Search code examples
phpmagento-1.9

Check how much left to the nearest multiple of four in PHP


I am creating a magento website where I'm allowing to buy at least 4 products and then multiples of four. The thing I'm struggling with is the else statement which should give a number to the following multiple of four. For e.g. If the person has 5 products it should display "Add 3 more products to your bundle" , or if person has 10 products " Add 2 more products to your bundle". I hope I've described it well.

My code is :

      `<?php else: ?>
        <p class="empty"><?php echo $this->__('Add  more products to your bundle before checking out') ?></p>
          <?php endif ?>`

Thank you for any helpful answers.


Solution

  • You are probably looking for the modulus-operator

    $modulo = 11 % 4; //this will return 3 as the remainder
    echo $modulo; //echos 3
    if($modulo!=0){ //only execute this when items are needed
        $needed = 4 - $modulo;
        echo "you need ". $needed ." more items"; //echos "you need 1 more items
    }