0

I have products with one quantity for each product. So, I want to disable the Add to Cart button if that same product is in the cart and change the text of the button to Already in Cart, Go to Cart and to Change URL to Cart. How can I achieve this?? I am using Magento 1.9.3.8

Any help/suggestion is appreciated. Thank you!

1

3 Answers 3

1

This worked for me in magento 1.9

add this code in list.phtml in product foreach section to check if product is already in cart or not

<?php $productid = $_product->getId(); ?>
<?php $quote = Mage::getSingleton('checkout/session')->getQuote();
      $product_in_cart = $quote->hasProductId($productid);
      if ($product_in_cart) {
        echo '<center class="in_cart">Already in cart</center>';
      }else{ ?>
        echo '<center class="in_cart">Product not in cart</center>';
      <?php } ?>
1
  • Oky here is code what i used and its working perfect May some one else can use it , i would not recommend to copy and past the complete code you can edit alerts as per your needs , add this code into addtocart.phtml in app/design/frontend/yourtheme/template/catalog/product/view/addtocart.phtml
    – Zuby
    Commented Feb 23, 2019 at 20:29
0

The quickest solution I can think of would probably be to simply to modify the addtocart.phtml tempalte (app/design/frontend/rwd/default/template/catalog/product/view/addtocart.phtml) get the current quote ($quote = Mage::helper('checkout/cart')->getCart()->getQuote();), get all the items from it ($items = $quote->getAllItems();) and put them into an array. Last but not least check whether the currently viewed product is within this array of items (if(!in_array($_product->getId(), $ids))).

<?php $_product = $this->getProduct(); ?>
<?php $buttonTitle = Mage::helper('core')->quoteEscape($this->__('Add to Cart')); ?>

<?php $quote = Mage::helper('checkout/cart')->getCart()->getQuote(); ?>
<?php $items = $quote->getAllItems(); ?>
<?php $ids = array(); ?>
<?php foreach($items as $item): ?>
    <?php $ids[] = $item->getProductId(); ?>
<?php endforeach; ?>

<?php if($_product->isSaleable()): ?>
    <div class="add-to-cart">
        <?php if(!in_array($_product->getId(), $ids)): ?>
            <?php if(!$_product->isGrouped()): ?>
            <div class="qty-wrapper">
                <label for="qty"><?php echo $this->__('Qty:') ?></label>
                <input type="text" pattern="\d*(\.\d+)?" name="qty" id="qty" maxlength="12" value="<?php echo $this->getProductDefaultQty() * 1 ?>" title="<?php echo Mage::helper('core')->quoteEscape($this->__('Qty')) ?>" class="input-text qty" />
            </div>
            <?php endif; ?>
            <div class="add-to-cart-buttons">
                <button type="button" title="<?php echo $buttonTitle ?>" class="button btn-cart" onclick="productAddToCartForm.submit(this)"><span><span><?php echo $buttonTitle ?></span></span></button>
                <?php echo $this->getChildHtml('', true, true) ?>
            </div>
        <?php else: ?>
            <div><?php echo $this->__('Already in Cart <a href="%s" class="button">Go to Cart</a>', Mage::helper('checkout/cart')->getCartUrl()) ?></div>
        <?php endif; ?>
    </div>
<?php endif; ?>

Drawback: tons of products in a cart might affect the performance (that risk should be rather small though, given the fact that you're just iterating through the products array once to get the product ids).

A more advanced solution would be to create an observer, for example on catalog_product_is_salable_after:

public function hideButton (Varien_Event_Observer $observer) {

    $productId = $observer->getEvent()->getProduct()->getId();

    $quote = Mage::helper('checkout/cart')->getCart()->getQuote();
    $items = $quote->getAllItems();
    $ids = array();
    foreach($items as $item) {
        $ids[] = $item->getProductId();
    }

    if(in_array($productId, $ids)) {
        $observer->getEvent()->getSalable()->setIsSalable(false);
    }

    return $this;
}

Information about how to create observers are available here on SE en masse.

0

Oky here is code what i used and its working perfect May some one else can use it , i would not recommend to copy and past the complete code you can edit alerts as per your needs , add this code into addtocart.phtml in app/design/frontend/yourtheme/template/catalog/product/view/addtocart.phtml

getProduct(); ?> __('Add to Cart'); ?> isSaleable()): ?> isGrouped()): ?> __('Qty:') ?> getProductDefaultQty() * 1 ?>" title="__('Qty') ?>" class="input-text qty" />
    <?php $productid = $_product->getId(); ?>
getQuote(); $product_in_cart = $quote->hasProductId($productid); if ($product_in_cart) { echo ' This product is already added in cart please go to cart for checkout

Go to My Cart'; }else{ ?>
      <button type="button" title="<?php echo $buttonTitle ?>" class="button btn-cart" onclick="productAddToCartForm.submit(this)"><span><span><?php echo $buttonTitle ?></span></span></button>';
  <?php } ?>

    <?php echo $this->getChildHtml('', true, true) ?>
</div>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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