Thursday 21 June 2018

Friday 15 June 2018

How to implemented Magento 2 Sort By Price: Low to High and High to Low.


How to implemented Magento 2 Sort By Price: Low to High and High to Low.

Step 1: Create plugins
    app/code/Vendor/Module/etc/di.xml

    <?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">

    <type name="Magento\Catalog\Block\Product\ProductList\Toolbar">
        <plugin name="custom_custom_block_toolbar" type="Vendor\Module\Plugin\Catalog\Block\Toolbar" />
    </type>

    <type name="Magento\Catalog\Model\Config">
        <plugin name="custom_catalog_model_config" type="Vendor\Module\Plugin\Catalog\Model\Config" />
    </type>

</config>


Step 2: Create Config.php
app/code/Vendor/Module/Plugin/Catalog/Model/Config.php

<?php

namespace Vendor\Module\Plugin\Catalog\Model;

class Config
{
    public function afterGetAttributeUsedForSortByArray(
    \Magento\Catalog\Model\Config $catalogConfig,
    $options
    ) {

        $options['low_to_high'] = __('Price - Low To High');
        $options['high_to_low'] = __('Price - High To Low');
        return $options;

    }

}

Step 3: Create Toolbar.php
app/code/Vendor/Module/Plugin/Catalog/Block/Toolbar.php

<?php
namespace Vendor\Module\Plugin\Catalog\Block;

class Toolbar
{

    /**
    * Plugin
    *
    * @param \Magento\Catalog\Block\Product\ProductList\Toolbar $subject
    * @param \Closure $proceed
    * @param \Magento\Framework\Data\Collection $collection
    * @return \Magento\Catalog\Block\Product\ProductList\Toolbar
    */
    public function aroundSetCollection(
    \Magento\Catalog\Block\Product\ProductList\Toolbar $subject,
    \Closure $proceed,
    $collection
    ) {
    $currentOrder = $subject->getCurrentOrder();
    $result = $proceed($collection);

    if ($currentOrder) {
        if ($currentOrder == 'high_to_low') {
            $subject->getCollection()->setOrder('price', 'desc');
        } elseif ($currentOrder == 'low_to_high') {
            $subject->getCollection()->setOrder('price', 'asc');
        }
    }

    return $result;
    }

}

Saturday 2 June 2018

How can I display indian price symble properly in invoice pdf in magento 1.9

1.Download the font that support Indian Rupee symbol. recommended dejavu-sans font.

2.place the font in lib directory.
3.open app/code/core/Mage/Sales/Model/Order/Pdf/Abstract.php and app/code/core/Mage/Sales/Model/Order/Pdf/Items/Abstract.php

and replace
$font = Zend_Pdf_Font::fontWithPath(Mage::getBaseDir() . '/lib/LinLibertineFont/LinLibertine_Re-4.4.1.ttf');

With
$font = Zend_Pdf_Font::fontWithPath(Mage::getBaseDir() . '/lib/dejavu-sans/DejaVuSans.ttf');

(in _setFontRegular(), _setFontBold(), _setFontItalic() functions in both files.)
 

Friday 1 June 2018

How to get data from Magento 1.9 System Configuration

<?php
$store = Mage::app()->getStore(); // store info
$configValue = Mage::getStoreConfig('sectionName/groupName/fieldName', $store);
?>
example
<?php
$store = Mage::app()->getStore(); // store info
$configValue = Mage::getStoreConfig('rene_banner_settings/rene_banner_rule/igstnumber', $store);
?>

Admin config with extendable amount of fields system.xml in magento 1.9

app/code/local/Abhi/Banner/etc/system.xml

<config>
    <sections>
        <abhi_banner_settings translate="label" module="banner">
             <label>HSN Number Settings</label>
            <tab>general</tab>
            <frontend_type>text</frontend_type>
            <sort_order>304</sort_order>
            <show_in_default>1</show_in_default>
            <show_in_website>1</show_in_website>
            <show_in_store>1</show_in_store>
            <groups>
                <abhi_banner_rule translate="label">
                    <label>HSN </label>
                    <frontend_type>text</frontend_type>
                    <sort_order>10</sort_order>
                    <show_in_default>1</show_in_default>
                    <show_in_website>1</show_in_website>
                    <show_in_store>1</show_in_store>
                    <fields>

                        <hsnnumber translate="label">
                            <label>HSN Number</label>
                            <frontend_type>text</frontend_type>
                            <sort_order>10</sort_order>
                            <show_in_default>1</show_in_default>
                            <show_in_website>1</show_in_website>
                            <show_in_store>1</show_in_store>
                        </hsnnumber>
                    </fields>
                </abhi_banner_rule>
            </groups>
        </abhi_banner_settings>
    </sections>
</config>

===============================
app/code/local/Abhi/Banner/etc/adminhtml.xml

<config>
    <acl>
        <resources>
            <admin>
                <children>
                    <system>
                        <children>
                            <config>
                                <children>
                                    <abhi_banner_settings translate="title">
                                        <title>HSN Number Settings</title>
                                        <sort_order>55</sort_order>
                                    </abhi_banner_settings>
                                </children>
                            </config>
                        </children>
                    </system>
                </children>
            </admin>
        </resources>
    </acl>
</config>

get value:-
<?php
$store = Mage::app()->getStore(); // store info
echo $configValue = Mage::getStoreConfig('abhi_banner_settings/abhi_banner_rule/hsnnumber', $store);
?>