Phillip
Phillip

Reputation: 4306

Magento - reloadPrice() modifications

My client needs to make an operation on products custom options.

Using Magento CE, I create a product, and give it some custom options from within the built-in left hand side menu in "Manage products" > "Add new product", such as "mm" (millimeters) and "mt" (meters)

This product will have both radio options and a textbot input.

Let's say we have

Base price: 0 

MM:
Radio option A which costs 0,9
Radio option B which costs 1,2
Radio option C which costs 2,3 

MT:
Textbox value = unknown yet

Let's say user chooses Radio option B and enters 10 in the textfield

Price should be updates as such:

1,2 * 10 + 0

Which is

radio value cost * textbox value + base price 

Is there any way to tell the code to take the value of the radio button, multiply it for the value of the textbox and sum it all to the base price?

Where could I look to see the current behavior of a product's custom options?

EDIT

I saw that whenever a value is selected, the reloadPrice() function is called.

I thought to check if both inputs are radio and text, then get the value of the text and multiply it for the value of the radio.

Is that right? Can you point me better?

Upvotes: 12

Views: 815

Answers (2)

Ravi Mule
Ravi Mule

Reputation: 404

This helps me, I hope this will also helps you

 initialize : function(config){
                this.config = config;
                this.reloadPrice();
                document.observe("dom:loaded", this.reloadPrice.bind(this));

            },
            reloadPrice : function(){
               price = new Number();
                config = this.config;
                skipIds = [];
                skipIds = [];
                relatedword = [];
                relatedvalue = [];
                relatedid = [];
                counter_each=0;
                counter=1;
                first=0;
                areaValue=1;
                submitbutton=true;

                $$('body .product-custom-option').each(function(element){
                    var optionId = 0;
                    element.name.sub(/[0-9]+/, function(match){
                        optionId = match[0];
                    });
                    if (this.config[optionId]) {
                        if (element.type == 'checkbox' || element.type == 'radio') 
                        {
                            if (element.checked) 
                            {
                                if (config[optionId][element.getValue()]) 
                                {
                                    <?php if(Mage::getVersion() >= 1.7): ?>
                                        price += parseFloat(config[optionId][element.getValue()].price);
                                    <?php else: ?>
                                        price += parseFloat(this.config[optionId][selectOption.value]);
                                    <?php endif; ?>  
                                }
                            }
                        }
}

Upvotes: 1

Dushyant Joshi
Dushyant Joshi

Reputation: 3702

reloadPrice() does not update product price at server level. One way to do this product price update is by implementing checkout_cart_product_add_after event.

  • Do you custom logic at client level with javascript in detail page. Assign this value to some hidden variable under the form product_addtocart_form. A better way is to save to session to reduce client side vulnerabilities or you can find your own better way. Or Implement your login only in Observer method. Whatever you find secure.
  • Rewrite the class Mage_Checkout_Model_Cart to modify as,

    public function addProduct($productInfo, $requestInfo=null)
    {
     ...
    Mage::dispatchEvent('checkout_cart_product_add_after', array('quote_item' => $result, 'product' => $product,'request_data'=>$request));
     ...
    }
    
  • In your yourmodule/etc/config.xml:

    <config>
        ...
        <frontend>
            ...
            <events>
                <checkout_cart_product_add_after>
                    <observers>
                        <unique_name>
                            <class>modulename/observer</class>
                            <method>customPrice</method>
                        </unique_name>
                    </observers>
                </checkout_cart_product_add_after>
            </events>
            ...
        </frontend>
        ...
    </config>
    
  • And then create an Observer class at yourmodule/Model/Observer.php

    class <namespace>_<modulename>_Model_Observer
    {
        public function customPrice(Varien_Event_Observer $observer)
        {
            // Get the quote item
            $item = $observer->getQuoteItem();
            $request=$observer->getRequestData();
            // Ensure we have the parent item, if it has one
            $item = ( $item->getParentItem() ? $item->getParentItem() : $item );
            // Load the custom price
            $price = "custom price logic";//whatever from your hidden value or by some other mean. 
            // Set the custom price
            $item->setCustomPrice($price);
            $item->setOriginalCustomPrice($price);
            // Enable super mode on the product.
            $item->getProduct()->setIsSuperMode(true);
        }
    

    }

For more information check here

Upvotes: 0

Related Questions