Magento Coding Solutions

how to add new attribute in configurable select options magento


step1)  /app/code/core/Mage/Catalog/Block/Product/View/Type/Configurable.php
Should be change in function

/**
     * Composes configuration for js
     *
     * @return string
     */
    public function getJsonConfig()
    {
        $attributes = array();
        $options    = array();
        $store      = $this->getCurrentStore();
        $taxHelper  = Mage::helper('tax');
        $currentProduct = $this->getProduct();

        $preconfiguredFlag = $currentProduct->hasPreconfiguredValues();
        if ($preconfiguredFlag) {
            $preconfiguredValues = $currentProduct->getPreconfiguredValues();
            $defaultValues       = array();
        }

        foreach ($this->getAllowProducts() as $product) {
            $productId  = $product->getId();

            foreach ($this->getAllowAttributes() as $attribute) {
                $productAttribute   = $attribute->getProductAttribute();
                $productAttributeId = $productAttribute->getId();
                $attributeValue     = $product->getData($productAttribute->getAttributeCode());
                if (!isset($options[$productAttributeId])) {
                    $options[$productAttributeId] = array();
                }

                if (!isset($options[$productAttributeId][$attributeValue])) {
                    $options[$productAttributeId][$attributeValue] = array();
                }
                $options[$productAttributeId][$attributeValue][] = $productId;
            }
        }

        $this->_resPrices = array(
            $this->_preparePrice($currentProduct->getFinalPrice())
        );

        foreach ($this->getAllowAttributes() as $attribute) {
            $productAttribute = $attribute->getProductAttribute();
            $attributeId = $productAttribute->getId();
            $info = array(
               'id'        => $productAttribute->getId(),
               'code'      => $productAttribute->getAttributeCode(),
               'label'     => $attribute->getLabel(),
               'options'   => array()
            );

            $optionPrices = array();
            $prices = $attribute->getPrices();
            if (is_array($prices)) {
                foreach ($prices as $value) {
                    if(!$this->_validateAttributeValue($attributeId, $value, $options)) {
                        continue;
                    }
                    $currentProduct->setConfigurablePrice(
                        $this->_preparePrice($value['pricing_value'], $value['is_percent'])
                    );
                    $currentProduct->setParentId(true);
                    Mage::dispatchEvent(
                        'catalog_product_type_configurable_price',
                        array('product' => $currentProduct)
                    );
                    $configurablePrice = $currentProduct->getConfigurablePrice();

                    if (isset($options[$attributeId][$value['value_index']])) {
                        $productsIndex = $options[$attributeId][$value['value_index']];
                    } else {
                        $productsIndex = array();
                    }
                $im=myimagepath;
                    $info['options'][] = array(
                        'id'        => $value['value_index'],
                        'label'     => $value['label'],
                        'price'     => $configurablePrice,
                        'oldPrice'  => $this->_prepareOldPrice($value['pricing_value'], $value['is_percent']),
                        'products'  => $productsIndex,
                         'image'     => $im, /*Add nwe Attribut in option*/
                    );
                    $optionPrices[] = $configurablePrice;
                }
            }
            /**
             * Prepare formated values for options choose
             */
            foreach ($optionPrices as $optionPrice) {
                foreach ($optionPrices as $additional) {
                    $this->_preparePrice(abs($additional-$optionPrice));
                }
            }
            if($this->_validateAttributeInfo($info)) {
               $attributes[$attributeId] = $info;
            }

            // Add attribute default value (if set)
            if ($preconfiguredFlag) {
                $configValue = $preconfiguredValues->getData('super_attribute/' . $attributeId);
                if ($configValue) {
                    $defaultValues[$attributeId] = $configValue;
                }
            }
        }

        $taxCalculation = Mage::getSingleton('tax/calculation');
        if (!$taxCalculation->getCustomer() && Mage::registry('current_customer')) {
            $taxCalculation->setCustomer(Mage::registry('current_customer'));
        }

        $_request = $taxCalculation->getDefaultRateRequest();
        $_request->setProductClassId($currentProduct->getTaxClassId());
        $defaultTax = $taxCalculation->getRate($_request);

        $_request = $taxCalculation->getRateRequest();
        $_request->setProductClassId($currentProduct->getTaxClassId());
        $currentTax = $taxCalculation->getRate($_request);

        $taxConfig = array(
            'includeTax'        => $taxHelper->priceIncludesTax(),
            'showIncludeTax'    => $taxHelper->displayPriceIncludingTax(),
            'showBothPrices'    => $taxHelper->displayBothPrices(),
            'defaultTax'        => $defaultTax,
            'currentTax'        => $currentTax,
            'inclTaxTitle'      => Mage::helper('catalog')->__('Incl. Tax')
        );

        $config = array(
            'attributes'        => $attributes,
            'template'          => str_replace('%s', '#{price}', $store->getCurrentCurrency()->getOutputFormat()),
            'basePrice'         => $this->_registerJsPrice($this->_convertPrice($currentProduct->getFinalPrice())),
            'oldPrice'          => $this->_registerJsPrice($this->_convertPrice($currentProduct->getPrice())),
            'productId'         => $currentProduct->getId(),
            'chooseText'        => Mage::helper('catalog')->__('Choose an Option...'),
            'taxConfig'         => $taxConfig
        );

        if ($preconfiguredFlag && !empty($defaultValues)) {
            $config['defaultValues'] = $defaultValues;
        }

        $config = array_merge($config, $this->_getAdditionalConfig());
                                //echo "<pre>";
                                //print_r($config);
                                //echo "</pre>";
                                //die;

        return Mage::helper('core')->jsonEncode($config);
    }


Step 2) skin/frontend/rwd/default/js/configurableswatches/swatches-product.js
should be change approx line no 77
Product.Config.prototype.loadOptions = function() {
    this.settings.each(function(element){
        element.disabled = false;
        element.options[0] = new Option(this.config.chooseText, '');
        var attributeId = element.id.replace(/[a-z]*/, '');
        var options = this.getAttributeOptions(attributeId);
        if(options) {
            var index = 1;
            for(var i=0;i<options.length;i++){
        options[i].allowedProducts = options[i].products.clone();
    element.options[index] = new Option(this.getOptionLabel(options[i], options[i].price), options[i].id);
                if (typeof options[i].price != 'undefined') {
                    element.options[index].setAttribute('price', options[i].price);
                }
                element.options[index].setAttribute('data-label', options[i].label.toLowerCase());
                element.options[index].setAttribute('image', options[i].image);
                element.options[index].config = options[i];
                index++;
            }
        }
        this.reloadOptionLabels(element);
    }.bind(this));

},


Conformable product  options





Inspect Element


How to Get A List Of All Attributes

<?php
$attributes = Mage::getResourceModel('catalog/product_attribute_collection')->getItems();
echo "<pre>";
foreach ($attributes as $attribute){
//print_r($attribute);
if($attribute->getIs_searchable()==1){

echo $attribute->getAttributecode();
echo '---';
echo $attribute->getFrontendLabel();
echo '<br>';
}
}
echo "</pre>";
?>

How to get all simple product which does not have in configurable product

$collection = Mage::getResourceModel('catalog/product_collection')
->addAttributeToFilter('type_id', array('eg'=>'simple'))
->addAttributeToSelect('*'); //or just the attributes you need

$collection->getSelect()->joinLeft(array('link_table' => 'catalog_product_super_link'),
'link_table.product_id = e.entity_id',array('product_id')
);
$collection->getSelect()->where('link_table.product_id IS NULL');
$notAssocative=array();
foreach($collection->getData() as $notAss){
array_push($notAssocative,$notAss['entity_id']);
}
print_r($notAssocative);

magento to create pdf.


download "dompdf" files from this link https://drive.google.com/file/d/0BxJfaJEh5g0rR2ZGUHNwOFBKMms/view?usp=sharing


public function generatePdf($pdfString,$fileName)
    {
   
include_once(Mage::getBaseDir()."/lib/dompdf/dompdf_config.inc.php");  //  https://github.com/matoilic/dompdf
   
    $dompdf = new DOMPDF();
    $dompdf->load_html( utf8_decode('<html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /></head>'.$pdfString.'</html>') , 'UTF-8' );
    $dompdf->set_paper("a4", "portrait");
    $dompdf->set_default_view('FitH',array());
    $dompdf->render();
   
    if(@file_put_contents($fileName, $dompdf->output())){ return true; } return false;
   
    }
$pdfString="<div>Hello Pdf</div>";  /*you can create  html  Stricter to create pdf file */

$filename = Mage::getBaseDir().'/media/pdf/12345.pdf';  /*you can assign file location*/
generatePdf($pdfString,$fileName);