1

When I click "Add to cart" button, an ajax request is sent and a pop up is displayed with its image to say that the product has been added.
I want some more information about that product like I want to show the next tier price and the quantity upon which the customer would get some discount.
So I need both the quantity and the tier price .But I don't know how to get it.
This the file which is called in the ajax request..

 <?php
 require_once 'Mage/Checkout/controllers/CartController.php';
 class Excellence_Ajax_IndexController extends Mage_Checkout_CartController
{
    public function addAction()
   {
    $cart   = $this->_getCart();
    $params = $this->getRequest()->getParams();
    if($params['isAjax'] == 1){
        $response = array();
        try {
            if (isset($params['qty'])) {
                $filter = new Zend_Filter_LocalizedToNormalized(
                array('locale' => Mage::app()->getLocale()->getLocaleCode())
                );
                $params['qty'] = $filter->filter($params['qty']);
            }

            $product = $this->_initProduct();
            $related = $this->getRequest()->getParam('related_product');

            /**
             * Check product availability
             */
            if (!$product) {
                $response['status'] = 'ERROR';
                $response['message'] = $this->__('Unable to find Product ID');
            }

            $cart->addProduct($product, $params);
            if (!empty($related)) {
                $cart->addProductsByIds(explode(',', $related));
            }

            $cart->save();

            $this->_getSession()->setCartWasUpdated(true);

            /**
             * @todo remove wishlist observer processAddToCart
             */
            Mage::dispatchEvent('checkout_cart_add_product_complete',
            array('product' => $product, 'request' => $this->getRequest(), 'response' => $this->getResponse())
            );

            if (!$cart->getQuote()->getHasError()){
                $message = $this->__('%s was added to your shopping cart.', Mage::helper('core')->htmlEscape($product->getName()));
                $response['status'] = 'SUCCESS';
                $response['message'] = $message;
           $response['product_name'] = $this->__('%s', Mage::helper('core')->htmlEscape($product->getName()));
                $response['product_image'] = $this->__('%s', Mage::helper('catalog/image')->init($product, 'small_image')->resize(180));
                $this->loadLayout();
                $sidebar_block = $this->getLayout()->getBlock('cart_sidebar');
                Mage::register('referrer_url', $this->_getRefererUrl());
                $sidebar = $sidebar_block->toHtml();
                $response['sidebar'] = $sidebar;
            }
        } catch (Mage_Core_Exception $e) {
            $msg = "";
            if ($this->_getSession()->getUseNotice(true)) {
                $msg = $e->getMessage();
            } else {
                $messages = array_unique(explode("\n", $e->getMessage()));
                foreach ($messages as $message) {
                    $msg .= $message.'<br/>';
                }
            }

            $response['status'] = 'ERROR';
            $response['message'] = $msg;
        } catch (Exception $e) {
            $response['status'] = 'ERROR';
            $response['message'] = $this->__('Cannot add the item to shopping cart.');
            Mage::logException($e);
        }
        $this->getResponse()->setBody(Mage::helper('core')->jsonEncode($response));
        return;
    }else{
        return parent::addAction();
    }
}  

}`

1

Checking the Mage_Catalog_Block_Product_Abstract class that is used on the frontend prices are displayed with the method getTierPrices.

In this method the products are retrieved via $product->getFormatedTierPrice();

So that should give us a hint on how to do this.

[...]
if (!$cart->getQuote()->getHasError()){
    $message = $this->__('%s was added to your shopping cart.', Mage::helper('core')->htmlEscape($product->getName()));
    $response['status'] = 'SUCCESS';
    $response['message'] = $message;
    $response['product_name'] = $this->__('%s', Mage::helper('core')->htmlEscape($product->getName()));
    $response['product_image'] = $this->__('%s', Mage::helper('catalog/image')->init($product, 'small_image')->resize(180));

    // here comes the tierprice code
    $prices = $product->getFormatedTierPrice(); // get tierprices as array

    if (is_array($prices)) {
        foreach ($prices as $price) {
            if ($params['qty'] <= $price['price_qty']) continue; // skip all quantities lower than what we currently have

            // set next price and qty in the response
            $response['product_tier_qty'] = $price['price_qty']; 
            $response['product_tier_price'] = $price['price'];
            break; //and break the loop
        }
    }

    [...]
    here the original code
| improve this answer | |
  • Thanks @Sander Mangel It helped me getting 60% of my solution . Can you suggest how can I get the quantity of a particular product added to the cart by a customer so that I can show him the exact next tier price quantity. – Kishore Patra Nov 3 '15 at 9:28
  • $response['product_tier_qty'] should have the next qty for tierprices – Sander Mangel Nov 3 '15 at 14:40
  • But first I want to calculate the quantity of the particular product which is added to cart. Then By comparing this quantity with the next tier price quantity I will show the customer that " buy ''x' more and get discount upto 'y'. So I need to know quantity of the particular product in the cart. – Kishore Patra Nov 4 '15 at 7:07
  • ah ok, the qty added that time to the cart is found in $params['qty'] if you need the absolute qty you'll need to fetch that from the quote items collection – Sander Mangel Nov 4 '15 at 7:39
  • yes exactly I want absolute qty for a particular product from the cart (qutoe items collection I guess). I have only the product_id. How can I find the quantity of the product from cart using its id ? – Kishore Patra Nov 4 '15 at 10:18

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.