7

I am trying to use the Magento reloadPrice() with jQuery to refresh the price. I have a configurable product with custom options. Without jQuery, the SELECT code for the option is :

<select id="select_281" class=" required-entry product-custom-option" title="" name="options[281]" onchange="opConfig.reloadPrice()">
<option value="0"></option>
<option rel="1" price="0" value="275"></option>
<option rel="2" price="0" value="276"></option>
</select>

With jQuery I remove the Prototype onchange code and try to calculate the price for my option (say $50) :

jQuery('#select_281').removeAttr('onchange').change(function(){

//Price of the option to add to a basic price of the conf product
price = 50;

optionsPrice.changePrice('opConfig', price);
optionsPrice.reload();

});

Price of the configurable product: $150.

Add an option (SELECT): we add $50.

The new price $200 is displayed in the product page, But not in the cart page : the cart page displays just $150 which is not correct.

2

The javascript code only executes on the client side.
So you can basically do anything with the prices (like you already did) but this does not affect the server side calculations of the price of the product when it is added to the cart.
If you want a custom logic for the price in the cart you need to write some code that changes the price.
I recommend observing one of these events checkout_cart_save_before or checkout_cart_add_product_complete or catalog_product_get_final_price. see what ever fits your needs.

1
  • Solved with 2 observers, checkout_cart_product_add_after observer to change price 1st time product is added to cart, and checkout_cart_update_items_after to change price for each item in cart (when user click "Modify cart" button on cart page). On cart page, by clicking link "Edit" product page displays so user can change quantity, ...and click "Update cart" button (which dont refresh price).How to force "Update cart" action to process code in checkout_cart_product_add_after observer? Is this code executed just the 1st time product is added to cart?
    – tokey
    Mar 19 '15 at 16:23
1

The solution : Use 2 observers :

//checkout_cart_product_add_after observer
    public function modifyPrice(Varien_Event_Observer $observer)
    {
        $item = $observer->getQuoteItem();
        $item = ( $item->getParentItem() ? $item->getParentItem() : $item );
        $productType = $product->getTypeID();
        $price = 100;//Unit price of product without engraving, 100 for example

        //Unit price with engraving, depending of 2 custom options of configurable product
        if($productType == 'configurable'){
            //get custom options of config here
            .
            .
            $engravingPrice = 1.2;//Get unit price with engraving from a special MySQL table
            $finalUnitPrice = $price + $engravingPrice;//Final custom price

            //Modify the price
            $item->setCustomPrice($finalUnitPrice);
            $item->setOriginalCustomPrice($finalUnitPrice);
            $item->getProduct()->setIsSuperMode(true);
        }
    }

    //checkout_cart_update_items_after observer
    public function modifyCartItems(Varien_Event_Observer $observer)
    {
        foreach ($observer->getCart()->getQuote()->getAllVisibleItems() as $item ) {
            if ($item->getParentItem()) {$item = $item->getParentItem();}
            $productType = $product->getTypeID();
            $productType = $product->getTypeID();
            $price = 100;//Unit price of product without engraving

            //Unit price with engraving, depending of 2 custom options of configurable product
            if($productType == 'configurable'){
                .
                .
                $engravingPrice = 1.2;//Get unit price with engraving from a special MySQL table
                $finalUnitPrice = $price + $engravingPrice;//Final custom price

                //Modify the price for the item
                $item->setCustomPrice($finalUnitPrice);
                $item->setOriginalCustomPrice($finalUnitPrice);
                $item->getProduct()->setIsSuperMode(true);
            }
        }
    }

It is running fine but.... one question remains :

On the cart page, when user clicks the link "Edit" to edit the product in product page so he can change quantity, ...and click the "Update cart" button, this update button does not read the checkout_cart_product_add_after to refresh price.

How to force this "Update cart" action to process the code in checkout_cart_product_add_after observer? Is this code executed just the first time the product is added to the cart?

Thanks.

tokey

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.