2

I am trying to create a page that updates the quantity off all items that are in the shopping cart to a minimum value. I am using an adaptation off the cart page for this. Below is the code I am using:

<?php 
$x= $this->getQty();
$minValue = 8;
if ($x>= $minValue) { 
echo 'Proceed with number: '.$x;
}
else {
    $x= 8;
    echo 'Minimum number is: '.$x;
}

?>

    <input type="text" pattern="\d*" name="cart[<?php echo $_item->getId() ?>][qty]" value="<?php echo $x?>" size="4" title="<?php echo $this->__('Qty') ?>" class="input-text qty" maxlength="12"/>

    <button type="submit" name="update_cart_action" value="update_qty" title="<?php echo $this->__('Update'); ?>" class="button btn-update"><span><span><?php echo $this->__('Update'); ?></span></span>
    </button>

This code is working as expected. Unfortunately users will need to hit the update button for the update to have effect. When the user just continues to the checkout the original value is transfered to this page.

Any idea on how to update the value in the input box without the user having to click a link?

Note: I know the best way off having a similar function is to have a minimum quantity placed in your shopping cart in the first place (which is in Magento's basic settings). Unfortunately in this case I need an update on the cart page.

Update. This adaptation (Please do not change core files) off the cart page seems to do the trick. It retrieves the number off items and updates the cart. Next to this I removed all links directly to the checkout page so people are forced to visit the cart page first.

AS anybody can see who has any experience in coding can see. I am not a programmer. So any recommendations to make this code better are greatly appreciated.

template/checkout/cart.phtml

<?php echo $this->getMessagesBlock()->toHtml() ?>
<div class="aantal">Number of people:
<input type="text" id="mhpeople" size="6" value="" class="input-text qty" maxlength="12" />
<button onclick="myEventCalc()">Calculate</button>
<br /><span class="min-aantal">(minimum amount is 8)</span> 
</div>

<script>

function myEventCalc() {   
var y = document.getElementById("mhpeople").value;
var x = document.getElementsByClassName("qty");
var i;

for (i = 0; i < x.length; i++) {
    x[i].value = y ;
}

$('cart-form').submit();
}

Adaptation off template/checkout/cart/item/default.phtml

<script language="javascript">
var mhy = document.getElementById("mhcart").value;
var mhx = document.getElementById("mhpeople");
mhx.value = mhy ;
var mhz = document.getElementsByClassName("qty");

if (<?php echo $y?> < 8)   {
    $('cart-form').submit();
}

var y = document.getElementById("mhpeople").value;

if (y != <?php echo $z?>) {
    myEventCalc()

}
0

I would suggest you to achieve this from observer.

This event checkout_cart_product_add_after should help you.

You will need to create a simple extension to fire this event.

In your config.xml have this:

<?xml version="1.0" encoding="UTF-8"?>

<config>
    <modules>
        <NameSpace_ModuleName>
            <version>1.0.8</version>
        </NameSpace_ModuleName>
    </modules>

    <global>
        <models>
            <namespace>
                <class>NameSpace_ModuleName_Model</class>
            </namespace>
        </models>
        <events>
            <checkout_cart_product_add_after>
                <observers>
                    <update_qty_on_product_add_to_cart>
                        <type>singleton</type>
                        <class>NameSpace_ModuleName_Model_Observer</class>
                        <method>updateQty</method>
                    </update_qty_on_product_add_to_cart>
                </observers>
            </checkout_cart_product_add_after>
        </events>
    ............
    </global>
</config>

Now, your NameSpace/ModuleName/Model/Observer.php

<?php
class NameSpace_ModuleName_Model_Observer
{
    public function updateQty(Varien_Event_Observer $observer)
    {
       $item = $observer->getEvent()->getQuoteItem();
          $x= $item->getQty();
          $minValue = 8;
          if ($x>= $minValue) { 
             Mage::log('Proceed with number: '.$x, null, 'custom-log.log'); //check in var/log folder  after product adding to cart
          }
          else {
          $x = 8;
             Mage::log('Minimum number is: '.$x, null, 'custom-log.log');
          }
          $item->setQty($x)->save();
    }
?>

Check if file custom-log.log is created in var/log folder after you add product to cart. If it is then your event is firing.

Hope this helps.

  • Thank you for answering. But I can't seem to get this to work. I will edit my post with what I did so far. – matthijshofstede Dec 16 '16 at 9:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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