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.
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.$printing_type
and thenvar_dump(serialize($additionalOptions))
?