PHP SOLUTIONS

This is the blog for getting Idea regarding PHP, Magento, jQuery and JavaScript for Customization.

Showing posts with label observer. Show all posts
Showing posts with label observer. Show all posts

Tuesday, 9 June 2015

Magento : Update cart price using the observer event

Hello Friends,

Using Observer,we have seen how to set the custome price, Check Here

Now, We are going for update the product with the custome price, because in magento, for the set any custome price, update custome price, there are different Observer Events which is help us for making proper customization.

A) Open Module Config.xml file which is located at etc directory of Module
[This module is same which we use for the Set Custome price]

B) Here, the code of observer event for update price

Magento

<frontend>
...................
<events>
 <checkout_cart_product_update_after> <!--Here Event Name as per your Requirement -->
  <observers>
   <{Namespace}_{Module Name}_Model_Observer>
    <type>singleton</type>
    <class>{Namespace}_{Module Name}__Model_Observer</class>
    <method>updatecustomprice</method>
   </{Namespace}_{Module Name}__Model_Observer>
  </observers>
 </checkout_cart_product_update_after>
</events>
.................
</frontend>
C) Now, Create or Update [if alreay you have] Observer.php in Model Directory
{Namespace}/{Module Name}/Model/Observer.php
D) In Observer.php add updatecustomprice function

Magento

class {Namespace}/{Module Name}_Model_Observer
{

public function updatecustomprice(Varien_Event_Observer $observer) {

$item = $observer->getQuoteItem();
$custom_vals = $_SESSION['custom_vals'];
if($custom_vals != '')
{
$additionalOptions = array(array(
'label' => '',
'value' => $custom_vals,
));
$item->addOption(array(
'code' => 'additional_options',
'value' => serialize($additionalOptions),
));
unset($_SESSION['custom_vals']);
}
$price = 123; //Here, set the Custom Price for Item
$item->setCustomPrice($price); // This set the Custom Price in Quote of cart
$item->setOriginalCustomPrice($price);
$item->getProduct()->setIsSuperMode(true);
}

}
E) Now, Clear all cache of site from admin

Enjoy, This is work for me in Magento 1.7.0.2

Saturday, 6 July 2013

Magento : Using Observer add the cart quote details to order item

Hi,

For Set the Cart Quote Items to the Order using the Observer. Using the "sales_convert_quote_item_to_order_item" Observer we can Add Custom Details of cart to the Order in magento.


1) Refer this Post = http://developerforums.blogspot.com/2013/07/using-observer-add-custom-price-for.html

Notice ::::>
                      Using this Post you can only set the Custom Details and Price to cart only, so for Save that Details to the Order you need to use another Event of Magento.  


2) Then Open your Module config.xml

    Copy-Paste below code Inside  <event> ...................   </event>  Tag.

    <sales_convert_quote_item_to_order_item> <!--Here Event Name as per your Requirement -->
                <observers>   
                    <{Namespace}_{Module Name}_Model_Observer>              
                        <type>singleton</type>
                        <class>{Namespace}_{Module Name}__Model_Observer</class>
                        <method>setdetailstocart</method>
                    </{Namespace}_{Module Name}__Model_Observer>                  
                </observers>      
   </sales_convert_quote_item_to_order_item>

3)  Open your   "Model/Observer.php"

   Use below Code

  class {Namespace}/{Module Name}_Model_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);
            }
}

4) Clear all Cache and Index of your Site


Enjoy, This is work for me in Magento 1.7.0.2

    

Tuesday, 2 July 2013

Magento : Using Observer add custom price and details for cart product

Hi,

For Set the Custom Price in Cart using the Observer. Using the Observer we can perform many events in magento, There are so many different Events to make changes in Magento.



A) First Create Module using the Module Creator in admin

B) then Open Module Config.xml file which is located at etc directory of Module

C) Use Below code using as per your event requirement

<frontend>
...................
<events>
            <checkout_cart_product_add_after>  <!--Here Event Name as per your Requirement -->
                <observers>   
                    <{Namespace}_{Module Name}_Model_Observer>              
                        <type>singleton</type>
                        <class>{Namespace}_{Module Name}__Model_Observer</class>
                        <method>setcustomprice</method>
                    </{Namespace}_{Module Name}__Model_Observer>                  
                </observers>
            </checkout_cart_product_add_after>
        </events>
 .................      
 </frontend>

D) Now, Create Observer.php in Model Directory

    {Namespace}/{Module Name}/Model/Observer.php

E) Use Below Code

 
class {Namespace}/{Module Name}_Model_Observer
{
   
       public function setcustomprice(Varien_Event_Observer $observer) {
                      
        $item = $observer->getQuoteItem();
        // Check if Product have parent item.
        $item = ( $item->getParentItem() ? $item->getParentItem() : $item );
      
        // This is for add the Custom Information about Product Item
        $additionalDetail = array(array(
         'code' => 'custom_details',
         'label' => 'This text is displayed through additional options',
         'value' => 'ID is ' . $item->getProductId() . ' and SKU is ' . $item->getSku()
         ));
         $item->addOption(array(
         'code' => 'additional_options',
         'value' => serialize($additionalOptions),
         ));
        

        // Load the custom price
        $price = 123; //Here, set the Custom Price for Item
        // Set the custom price
        $item->setCustomPrice($price); // This set the Custom Price in Quote of cart
        $item->setOriginalCustomPrice($price);
        // Enable super mode on the product.
        $item->getProduct()->setIsSuperMode(true); 

        }
      
}

F) Clear all Cache and Index of your Site


Enjoy, This is work for me in Magento 1.7.0.2