[Magento] reloadPrice() pour des options personnalisables en jQuery
Bonjour tous,
J'ai un souci avec utilisé avec jQuery qui n'actutalise pas le panier. Je pars d'un produit configurable avec des options personnalisables. Normalement, sans jQuery, le code d'un SELECT avec option est le suivant :
Code:
1 2 3 4 5
| <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> |
Avec jQuery je gèle le (pris en charge par Prototype) tout en donnant mon propre prix de l'option de 50 euros :
Code:
1 2 3 4 5 6 7 8 9
| jQuery('#select_281').removeAttr('onchange').change(function(){
//Calcul du prix
price = 50;
optionsPrice.changePrice('opConfig', price);
optionsPrice.reload();
}); |
Le prix de mon produit au départ :150 euros
En choisissant l'option : on ajoute 50 euros
Le nouveau prix de 200 euros s'affiche bien sur la page produit, mais pas sur le panier qui continue d'afficher 150 euros.
Deuxième question : quelqu'un a une idée sur comment afficher sur le panier une ligne pour le prix normal, puis une ligne pour le prix des options.
COM
Résolu avec deux observers
Hi,
The reloadPrice() cant change any price serverside.
So we solved this issue by using 2 observers :
Code:
checkout_cart_product_add_after
observer to change the price the first time the product is added to the cart, and
Code:
checkout_cart_update_items_after
observer to change the price for each item in the cart (when user click the "Modify cart" button on cart page).
The code here. Code tested for configurable products with 2 custom options numberOfColors and engravingType. For each couple numberOfColors/engravingType we have tierprices stored in special MySQL table, we may change price with a custom price. Each simple product has its tierPrices.
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
| //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);
}
}
} |
But.....one problem 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.
Dont't know 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