12

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?

Phillip
  • 4,276
  • 7
  • 42
  • 74
  • 3
    I think it's because your info is so sparse. You really need to explain what specific cart you're using, or plug in, give code samples. Magento is not a language, it's a platform. Your question is not answerable with info given – Dave Alperovich May 06 '15 at 17:18
  • I'm not using any plugin for the cart, it' just Magento's default. I can't give any code example because I have no idea where should I place any custom code to make custom option behave like I want them to. I'll try to give further info, but there aren't much really. – Phillip May 06 '15 at 17:39
  • Did you consider configurable products with qty field instead of an additional textfield? – Simon H May 08 '15 at 07:36
  • No, actually I didn't, but is that possible? I mean, what would the attribute be? – Phillip May 10 '15 at 09:14
  • What's the textbox for? Quantity? – MatteoSp May 11 '15 at 08:29
  • No, number of meters (for example) of a specific product (let's say, a rope) – Phillip May 11 '15 at 09:24

2 Answers2

1

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; ?>  
                                }
                            }
                        }
}
Ravi Mule
  • 404
  • 3
  • 16
0

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

Community
  • 1
  • 1
Dushyant Joshi
  • 3,672
  • 3
  • 28
  • 52