Veuillez suivre les étapes ci-dessous pour modifier le prix du super attribut
Utilisez d'abord les observateurs "catalog_product_get_final_price". Faites des observateurs comme ça:
Ouvrez votre module config.xml et utilisez le code ci-dessous:
<events>
<catalog_product_get_final_price>
<observers>
<Setblue_Banner_Model_Observer>
<type>singleton</type>
<class>Setblue_Banner_Model_Observer</class>
<method>getFinalPrice</method>
</Setblue_Banner_Model_Observer>
</observers>
</catalog_product_get_final_price>
</events>
Maintenant, créez le fichier Observer.php dans le modèle et le code ci-dessous
<?php
class Setblue_Banner_Model_Observer
{
public function getFinalPrice(Varien_Event_Observer $observer) {
$event = $observer->getEvent();
$product = $event->getProduct();
$qty = $event->getQty();
$selectedAttributes = array();
if ($product->getCustomOption('attributes')) {
Mage::log('yes-----', null, 'confPricing.log');
$selectedAttributes = unserialize($product->getCustomOption('attributes')->getValue());
}
if (sizeof($selectedAttributes)) return $this->getSimpleProductPrice($qty, $product);
}
public function getSimpleProductPrice($qty=null, $product)
{
$cfgId = $product->getId();
$product->getTypeInstance(true)
->setStoreFilter($product->getStore(), $product);
$attributes = $product->getTypeInstance(true)
->getConfigurableAttributes($product);
$selectedAttributes = array();
if ($product->getCustomOption('attributes')) {
$selectedAttributes = unserialize($product->getCustomOption('attributes')->getValue());
}
$db = Mage::getSingleton('core/resource')->getConnection('core_read');
$dbMeta = Mage::getSingleton('core/resource');
$sql = <<<SQL
SELECT main_table.entity_id FROM {$dbMeta->getTableName('catalog/product')} `main_table` INNER JOIN
{$dbMeta->getTableName('catalog/product_super_link')} `sl` ON sl.parent_id = {$cfgId}
SQL;
foreach($selectedAttributes as $attributeId => $optionId) {
$alias = "a{$attributeId}";
$sql .= ' INNER JOIN ' . $dbMeta->getTableName('catalog/product') . "_int" . " $alias ON $alias.entity_id = main_table.entity_id AND $alias.attribute_id = $attributeId AND $alias.value = $optionId AND $alias.entity_id = sl.product_id";
}
$id = $db->fetchOne($sql);
//Mage::log(Mage::getModel("catalog/product")->load($id)->getFinalPrice($qty), null, 'confPricing.log');
//return
$fp = Mage::getModel("catalog/product")->load($id)->getFinalPrice($qty);
return $product->setFinalPrice($fp);
}
}
?>
Maintenant, ouvrez app / design / frontend / default / yourtheme / template / catalogue / product / view / type / options / configurable.phtml et collez le code ci-dessous n'importe où dans le fichier
<ul class="productIds" style="display:none;">
<?php
$configurableProduct = Mage::getModel('catalog/product')->load($_product->getId());
$childProducts = Mage::getModel('catalog/product_type_configurable')->getUsedProducts(null,$configurableProduct);
foreach($childProducts as $child) {
$_productObj = Mage::getModel('catalog/product')->load($child->getId());
?>
<li id='simple_<?php echo $child->getId(); ?>'><?php echo Mage::helper('core')->currency($_productObj->getFinalPrice()); ?></li>
<?php
}
?>
</ul>
Maintenant, ouvrez js / varien / configurable.js et modifiez la fonction reloadPrice comme ci-dessous ou vous pouvez également remplacer cette fonction entière
reloadPrice: function(){
if (this.config.disablePriceReload) {
return;
}
var price = 0;
var oldPrice = 0;
for(var i=this.settings.length-1;i>=0;i--){
var selected = this.settings[i].options[this.settings[i].selectedIndex];
if(selected.config){
price += parseFloat(selected.config.price);
oldPrice += parseFloat(selected.config.oldPrice);
}
}
/* Edit Code By Chandresh Rana*/
//optionsPrice.changePrice('config', {'price': price, 'oldPrice': oldPrice});
optionsPrice.reload();
var existingProducts = new Object();
for(var i=this.settings.length-1;i>=0;i--)
{
var selected = this.settings[i].options[this.settings[i].selectedIndex];
if(selected.config)
{
for(var iproducts=0;iproducts<selected.config.products.length;iproducts++)
{
var usedAsKey = selected.config.products[iproducts]+"";
if(existingProducts[usedAsKey]==undefined)
{
existingProducts[usedAsKey]=1;
}
else
{
existingProducts[usedAsKey]=existingProducts[usedAsKey]+1;
}
}
}
}
for (var keyValue in existingProducts)
{
for ( var keyValueInner in existingProducts)
{
if(Number(existingProducts[keyValueInner])<Number(existingProducts[keyValue]))
{
delete existingProducts[keyValueInner];
}
}
}
var sizeOfExistingProducts=0;
var currentSimpleProductId = "";
for ( var keyValue in existingProducts)
{
currentSimpleProductId = keyValue;
sizeOfExistingProducts=sizeOfExistingProducts+1
}
if(sizeOfExistingProducts==1)
{
if($('product-price-'+this.config.productId)){
$('product-price-'+this.config.productId).innerHTML = jQuery("#simple_"+currentSimpleProductId).html();
}
}
// End Code By Chandresh Rana
return price;
if($('product-price-'+this.config.productId)){
$('product-price-'+this.config.productId).innerHTML = price;
}
this.reloadOldPrice();
},
Code extrait de: http://chandreshrana.blogspot.in/2016/03/set-simple-product-price-instead-of.html