4

I am trying to modify the product price when user hit the add to cart button on product view page. For this I did some jQuery and hidden fields. Once user hit the add to cart button, I receive the hidden field data in the observer and modify the price and it is working fine.

PROBLEM

Now on the product page I have created some options with the product using html and jQuery (Not default custom option) and passing their values in the hidden filled. Now I have to show those selected option in the cart page as magento default custom option works. And for this I have created the observer to create option on the fly when user hit add to cart button. It is working fine if I pass static value in the observer for the option but I want to get those hidden value in the observer those are passed from product view page as I did earlier to modify the price before. Please my code below :

config.xml

<events>
    <checkout_cart_product_add_after>
        <observers>
            <san_change_price>
                <class>webroot/observer</class>
                <method>modifyPrice</method>
            </san_change_price>
        </observers>
    </checkout_cart_product_add_after>
    <sales_quote_collect_totals_before>
        <observers>
            <san_after_addtocart>
                <type>singleton</type>
                    <class>webroot/observer</class>
                <method>salesQuoteAddressCollectTotalsBefore</method>
            </san_after_addtocart>
        </observers>
    </sales_quote_collect_totals_before>
    <sales_convert_quote_item_to_order_item>
        <observers>
            <san_show_in_admin>
            <type>singleton</type>
            <class>webroot/observer</class>
            <method>salesConvertQuoteItemToOrderItem</method>
            </san_show_in_admin>
        </observers>
    </sales_convert_quote_item_to_order_item>
</events> 

Observer.php

public function modifyPrice(Varien_Event_Observer $obs)
{

    $item   = $obs->getQuoteItem();

    $item   = ( $item->getParentItem() ? $item->getParentItem() : $item );
    $price  = $this->_getPriceByItem($item);
    $data   = Mage::app()->getRequest()->getParams();
    // Update Price
    $price = $data['pay-amount'];
    $item->setCustomPrice($price);
    $item->setOriginalCustomPrice($price);

    $item->getProduct()->setIsSuperMode(true);
}

protected function _getPriceByItem(Mage_Sales_Model_Quote_Item $item)
{
    $price;
    return $price;
}

public function salesQuoteAddressCollectTotalsBefore($observer)
{
    $quote = $observer->getQuote();
    $quote_items = $quote->getItemsCollection();
    $data   = Mage::app()->getRequest()->getParams();
    $print_type =  $data['printing-type'];
    //echo $print_type;exit;
    //echo '<pre>';print_r($quote_items);exit;
    foreach ($quote_items as $item) {

        $additionalOptions = array(
            array(
                'code'  => '666',
                'label' => 'Printing Type',
                'value' => 'abc'            // Have to pass $print_type
            )

        );
        $item->addOption(
            array(
                 'code'  => 'additional_options',
                 'value' => serialize($additionalOptions),
            )
        );
    }
    // echo '<pre>';
    // print_r(serialize($additionalOptions));exit;
}
public function salesConvertQuoteItemToOrderItem(Varien_Event_Observer $observer)
{
    $quoteItem = $observer->getItem();
    if ($additionalOptions = $quoteItem->getOptionByCode('additional_options')) {
        $orderItem = $observer->getOrderItem();
        $options = $orderItem->getProductOptions();
        $options['additional_options'] = unserialize($additionalOptions->getValue());
        $orderItem->setProductOptions($options);
    }
}

In the above salesQuoteAddressCollectTotalsBefore() methode I have to set value $print_type = $data['printing-type']; for $additionalOptions array. but when I set value as $print_type it shows blank in the cart page however if i pass any static value like abc then it works fine.

Please help me to set those hidden value to the option ($additionalOptions) and those values should be visible in the Admin Panel under the respective Order Information section like default custom option works

Thank you.

5
  • Have you tried var_dump($print_type) ? What does it yield? I'm sure it's an array and you're trying to do an array to string conversion. Dec 6, 2016 at 9:38
  • Thank You @JulienLachal! Its an array. I already try this. And I already modify the price successfully but in the observer not able to set this value dynamically. Any Idea? Dec 6, 2016 at 9:47
  • array(12) { ["uenc"]=> string(80) "aHR0cDovL2xvY2FsaG9zdC9tYWdlbnRvL21hZ2UzL2luZGV4LnBocC90ZXN0LXByb2R1Y3QuaHRtbA,," ["product"]=> string(1) "2" ["form_key"]=> string(16) "E8Tw1VEHK60ruMRV" ["related_product"]=> string(0) "" ["s"]=> string(1) "0" ["m"]=> string(2) "12" ["l"]=> string(1) "2" ["xl"]=> string(1) "0" ["2xl"]=> string(1) "0" ["3xl"]=> string(1) "0" ["pay-amount"]=> string(3) "154" ["printing-type"]=> string(16) "Clothing Pricing" } Dec 6, 2016 at 9:47
  • What do you get if you pass the dynamic $printing_type and then var_dump(serialize($additionalOptions))? Dec 6, 2016 at 9:50
  • Value is showing here. But in the cart page, only label display and value field is empty :( Dec 6, 2016 at 9:51

3 Answers 3

2
+50

The problem is that the event sales_quote_collect_totals_before is dispatched in more places than you think.
Not sure about it, but I think it is dispatched when you add a product to the cart and when the cart page is reloaded. You can easily test this by adding a logger in your observer and see how many times it is executed.
Add this in your observer:

Mage::log('running my observer', null, 'sales.txt', true);

then add a product to the cart and see how many logs there are in the file var/log/sales.txt.
What I presume is that the first time the observer runs, your value gets added to the quote item, but the second time (when the cart page is loaded) that value is reset to null because there is no print-type in the request.
this explains why it works with a hard-coded value, because the second time the observer fires, you still get the same value attached to the quote item.

As a general rule, it is dangerous to use the request in any observer that is not dispatched from a controller.
You can try to set the value like this :

$data   = Mage::app()->getRequest()->getParams();
$print_type = isset($data['printing-type']) ? $data['printing-type'] : null;
if (!is_null($print_type))  {
    //your foreach loop here
}
3
  • 1
    Thank you so much @Marius Sir, for well Explanation! It is working as expected. All the options values are available in the admin panel under respective order information as well. Thank you :) Dec 15, 2016 at 5:56
  • Please check for typo above $data = Mage::app()()->getRequest()->getParams(); Dec 15, 2016 at 5:57
  • Sorry for that. Fixed it.
    – Marius
    Dec 15, 2016 at 5:59
1

First thing for post value

Mage::app()->getRequest()->getParams() will not work you have to use

Mage::app()->getRequest()->getPost()

14
  • Thank You @Murtuza ! But I have tried with both. Same issue Dec 6, 2016 at 10:03
  • is there any place where you get this post data ? Dec 6, 2016 at 10:04
  • if yes save it in registry and after use clear data form registry Dec 6, 2016 at 10:05
  • inside salesQuoteAddressCollectTotalsBefore() here i have to set value to array Dec 6, 2016 at 10:06
  • Have you tried this ` Mage::app()->getFrontController()->getRequest()->getParams();` Dec 6, 2016 at 10:07
1
public function modifyPrice(Varien_Event_Observer $obs)
{
    $data   = Mage::app()->getRequest()->getParams();
    if (isset($data['pay-amount']) && $data['pay-amount'] != '') {
            //do your changes here...
    }
}

You can try with below for custom option,

$additionalOptions = array(
            array(
                'label' => 'Printing Type',
                'value' => $print_type            // Have to pass $print_type
            )

        );
        $item->addOption(
            array(
                 'code'  => 'additional_options',
                 'value' => serialize($additionalOptions),
            )
        );

check with above code.

12
  • Thankyou @Rakesh! But I need to get posted values in salesQuoteAddressCollectTotalsBefore() and then set the value to custom option. You can see the discussion with @Murtuza Zabuawala Dec 12, 2016 at 13:06
  • are you want to get value inside salesQuoteAddressCollectTotalsBefore()? Dec 12, 2016 at 13:08
  • I am able to get all posted values but when I set any posted value in $additionalOptions (Where I set abc above). then it is showing empty in the cart page But if i set abc or any static value then it work fine. Dec 12, 2016 at 13:16
  • means dynamically value are not set in your above code, right? Dec 12, 2016 at 13:19
  • You are Right ! Dec 12, 2016 at 13:20

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.