1

I want to add two variable in specific product using observer event controller_action_predispatch_checkout_cart_add. These two variable are use in checkout/cart page for display prupose.

Following config.xml.

<events>
    <controller_action_predispatch_checkout_cart_add>
        <observers>
         <test_deliverydate_cart_add>
          <class>Test_Deliverydate_Model_Observer</class>
           <method>logCartAdd</method>
         </test_deliverydate_cart_add>
        </observers>
    </controller_action_predispatch_checkout_cart_add>
</events>

Following Observer.php

public function logCartAdd($event)
{

    Mage::getModel('core/session')->setProductToShoppingCart(
        new Varien_Object(array(
            'productId' => Mage::app()->getRequest()->getPost('product'),
            'rentalDays' => Mage::app()->getRequest()->getPost('rentalDays'),
            'deliveryDate' => Mage::app()->getRequest()->getPost('delivery_date'),
        ))
    );
}

Following test-deliverydate.xml

<checkout_cart_index>
    <reference name="content">
        <block type="core/template" template="test/deliverydate/bookingdate.phtml" />
    </reference>
</checkout_cart_index>

Following bookingdate.phtml

<?php 
    $bookedProductDetail = Mage::getModel('core/session')->getProductToShoppingCart() ;
    $productSku = Mage::getModel('catalog/product')->load($bookedProductDetail->getData('productId'))->getSku();
    $rendDays = $bookedProductDetail->getData('rentalDays');

    $bookedDate = $bookedProductDetail->getData('deliveryDate');
    $bookedDate = strtotime($bookedDate);
    $bookedDate = date('Y-m-d', $bookedDate);

    <p> <?php echo $productSku; ?> </p>
    <p> <?php echo $rendDays; ?> </p>
    <p> <?php echo $bookedDate; ?> </p>
?>

When I add multiple products at that time last product's detail like : $rendDays, $bookedDate showing same for all added products. Please help me how can i solve this problem.

1

Perhaps something like

 $cartItems = Mage::getSingleton('checkout/session')
        ->getQuote()
        ->getAllItems();

Then

foreach ($cartItems as $item) {
    $_product = Mage::getModel('catalog/product')->load($item->getProductId());
    $rentalDays = $_product->getData('rentalDays');
    // then do something with it
    echo $rentalDays . '<br>';
}
1
  • may be this isn't work because i want update product value before add to cart so that $cartItems may be empty. – Hardik Visa Feb 27 '16 at 8:53
1

We Can use below code for get all post value for perticullar product.

$quote = $observer->getQuote();
    $quote_items = $quote->getItemsCollection();
    foreach ($quote_items as $item)
    {
        if ($item instanceof Mage_Sales_Model_Quote_Item) 
        {
            $infoBuyRequest = $item->getOptionByCode('info_buyRequest');
            $buyRequest = new Varien_Object(unserialize($infoBuyRequest->getValue()));
        } 
        else if ($item instanceof Mage_Sales_Model_Order_Item) 
        {
            $buyRequest = new Varien_Object($item->getProductOptions());
        }
        $buyRequest->getData('product');
        $buyRequest->getData('rentalDays');
        $buyRequest->getData('delivery_date');
    }

We can get all data of product.

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.