Wednesday 27 April 2016

Set simple product price instead of attribute price in configurable product magento


Use simple product price instead of Super attribute price in configurable product magento

Kindly follow the below steps to change the super attribute price

First Use observers "catalog_product_get_final_price". Make observers like this :

Open your module config.xml and use below code :

<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>

Now make the Observer.php file in model and past below code

<?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);
}

}

?>

Now open app/design/frontend/default/yourtheme/template/catalog/product/view/type/options/configurable.phtml and paste below code anywhere in file

<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>  

Now open js/varien/configurable.js and change reloadPrice function as below or you can replace this whole function aswell

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();
},

That's it. Enjoy Chandresh rana Coding....

2 comments:

Unknown said...

Hi, your guide is very interesting and wanted to stay, I just can not understand what I have to open the file config.xml and observer.php ...

Rahul said...

The function in observer.php is not called, so the price in cart is not updating. I have followed all your steps but not working, please help me out.

Thanks