I LOVE TO DEVELOPMENT

I AM

Blog

Magento 2 Email Sending With Attachment


I will explain how to add attachment function with magento 2 so we can use that function anywhere as we wish.

First of all we will need custom module if not have we can create bsic module with module creator.


Second thing is we will need etc/di.xml in our custom module it will looks something like


<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <preference for="\Magento\Framework\Mail\Template\TransportBuilder" type="\Namespace\CustomModule\Magento\Mail\Template\TransportBuilder" />
</config>

Next we need to extend \Magento\Framework\Mail\Template\TransportBuilder it will looks like

    <?php
    namespace Namespace\CustomModule\Magento\Mail\Template;
    
    class TransportBuilder 
        extends \Magento\Framework\Mail\Template\TransportBuilder
    {
        public function addAttachment(
            $body,
            $mimeType    = \Zend_Mime::TYPE_OCTETSTREAM,
            $disposition = \Zend_Mime::DISPOSITION_ATTACHMENT,
            $encoding    = \Zend_Mime::ENCODING_BASE64,
            $filename    = null
        ) {
            $this->message->createAttachment($body, $mimeType, $disposition, $encoding, $filename);
            return $this;
        }

    }

That's it. we can Reuse anywhere as our need...!!!

Move Category Description Bottom Content


We have start with the topic how we can move category description bottom of the content part. so It is old fashion when we have category image with category banner. we can move it after category products and category static block to look some difference from other websites.

Let's Start...

We will need catalog_category_view.xml file under our new theme which we are using for customization.

Path Sould be like :-

app/design/frontend/Company_name/theme_name/Magento_Catalog/layout/catalog_category_view.xml


we can add by move tag as we use to move any element of the page.

<move element="category.description" destination="content.bottom" />


So, our code will looks like in catalog_category_view.xml

<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <body>
        <move element="category.description" destination="content.bottom" />
    </body>
</page>

Clear Cache and check your front category page. Looks some different than other web sites. if we use auto load pagination that it will looks so amazing.

Have A Nice Day...!!!


Add breadcrumbs on contact page magento 2

Contact us page doesn't  provide default breadcrumbs in magento 2. We need to change contact_indax_index file to show breadcrumbs on custom page.

Override contact_indax_index



I am describing which i had implement to show breadcrumbs on contact us page.



<?xml version="1.0"?>
<!--
/**
 * Copyright © 2015 Magento. All rights reserved.
 * See COPYING.txt for license details.
 */
-->
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" layout="2columns-left" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <head>
        <title>Contact Us</title>
    </head>
    <body>
        <referenceBlock name="breadcrumbs">
            <action method="addCrumb">
                <argument name="crumbName" xsi:type="string">Home</argument>
                <argument name="crumbInfo" xsi:type="array">
                    <item name="title" xsi:type="string">Home</item>
                    <item name="label" xsi:type="string">Home</item>
                    <item name="link" xsi:type="string">{{baseUrl}}</item>
                </argument>
            </action>
            <action method="addCrumb">
                <argument name="crumbName" xsi:type="string">Contact Us</argument>
                <argument name="crumbInfo" xsi:type="array">
                    <item name="title" xsi:type="string">Contact Us</item>
                    <item name="label" xsi:type="string">Contact Us</item>
                </argument>
            </action>
        </referenceBlock>
        <referenceContainer name="content">
            <block class="Magento\Contact\Block\ContactForm" name="contactForm" template="Magento_Contact::form.phtml">
                <container name="form.additional.info" label="Form Additional Info"/>
            </block>
        </referenceContainer>
    </body>
</page>



That's it...!!!

Product.OptionsPrice is not a constructor magento 1.9.3

This issue i have faced and challenging to sort out at that time. i am giving you brief how to solve this issue.


i have found that  product_options.js file not added into my custom theme which cause the issue after upgrade my magento.


add js file in either local.xml or catalog.xml and add under the <catalog_product_view> tag or page handle you're having the same issue.


<action method="addJs"><script>varien/product_options.js</script></action>


Thaat's it...!!!

Magento 1.9 header and footer files outside magento in simple php file

call design area  Mage::app()->loadArea('frontend'); in php file outside magento.

<?php
include_once "app/Mage.php";
umask(0);
Mage::app()->loadArea('frontend');

$layout = Mage::getSingleton('core/layout');

//load default xml layout handle and generate blocks
$layout->getUpdate()->load('default');
$layout->generateXml()->generateBlocks();

//get the loaded head and header blocks and output
$headBlock = $layout->getBlock('head');
$headerBlock = $layout->getBlock('header');
echo $headBlock->toHtml() . $headerBlock->toHtml();
get footer

$footerBlock = $layout->getBlock('footer');
echo $footerBlock->toHtml();

Refer :- My Answer On Magento Stackexchange


Easy One...!!!

Magento 2 “Whoops, our bad” product page

Try re-indexing using the Magento 2 CLI

ssh into the server where your Magento files are and go into your Magento root folder.

cd /var/www/html/magento root folder

From that folder you can access the magento 2 cli with bin/magento

If you can't start it, use chmod +x bin/magento

in order to re-index data via the CLI, use the following command

php bin/magento indexer:reindex

If you want to be sure that NO cache is kept, you can remove the following folders from the var directory with:

rm -rf var/cache var/generation var/pagecache var/di

and then re-compile magento using command

php bin/magento setup:di:compile


Refer my answer on :- My Answer On Magento Stackexchange

Magento 2 mass update product attribute

Magento is so powerful than we think we can use magento's in-built functionality as i describe steps with an example to clear all short description field from all products.

Step 1 - Login to admin panel

Step 2 - catalog > products

Step 3 - select all products/Need to update fields by applying filter.

Step 4 - select action as update attributes

Step 5 - Select checkbox which wants to update attribute for selected products.

Step 6 - save.

Note :- please select short description as not required attribute.

re-index and clear cache after remove.

We can use this trick for any admin product attribute mass Field update.


Easy one...!!!

Magento 1.9 Mass update product attributes value


Magento is so powerful than we think we can use magento's in-built functionality as i describe steps with an example to clear all short description field from all products.

Step 1 - Login to admin panel

Step 2 - open admin > catalog > products

Step 3 - select all products/Need to update fields by applying filter.

Step 4 - select action as update attributes

Step 5 - you have to check short description checkbox with empty text there.

Step 6 - save.

Note :- please select short description as not required attribute.

re-index and clear cache after remove.

We can use this trick for any admin product attribute mass Field update.


Easy one...!!!

Magento 2 custom js file in require js in custom module

Add custom mycustom.js file into magento 2

Place requirejs-config.js file in your module at path app/code/Denish/Example/view/frontend


var config = {
    map: {
        '*': {
            mycstomjsfile: 'Denish_Example/js/mycustom',
        }
    }
};

map :- maps your file with the alias name which you defined in the object.

Now add js file


app/code/Denish/Example/view/frontend/web/js/mycustom.js


we can use with code to test

require([
        'jquery',
        'mycstomjsfile'
    ], function ($, script) {
        //Your code
        alert('Here');
    });


Now remove cache and deploy static content. refer previous post i have posted in MAGENTO 2 BASIC COMMANDS.

Check your console mycustom.js will be loaded.


Easy one...!!!


Add custom css file into magento 2 for all pages

will explain example to add mycustom.css file to magento 2.

Need default_head_blocks.xml in theme as per given path.

path shold be

Vendor/themename/Magento_Theme/layout/default_head_blocks.xml


Find <head> tag and place 


<css src="css/mycustom.css"/>

Add css file


app/design/frontend/{Vendor}/{theme}/web/css/styles.css

Now remove cache and deploy static content. refer previous post i have posted in MAGENTO 2 BASIC COMMANDS.

Check your console mycustom.css will be loaded.


Easy one...!!!




Override Magento 2 frontend layout xml and phtml files in our custom theme


1) Override template/phtml file:

vendor/magento/module-catalog/view/frontend/templates/product/list.phtml

path should be

app/design/frontend/Vendor/theme/Magento_Catalog/templates/product/list.phtml


2) Override layout/xml file:

vendor/magento/module-catalog/view/frontend/layout/catalog_product_view.xml

path should be

app/design/frontend/Vendor/theme/Magento_Catalog/layout/catalog_product_view.xml

Magento 2 Preference

Magento 2 Preference is used for rewrites as like in in Magento 1. we can extend class using preference. powerful feature, but as responsible as Magento 1.  Basically, we can't attempt to rewrite the same class in two different modules. if we try than that can be conflict.

How we can create preference ? yes we can use xml file for the same. i am giving you brief for the same.

Let's Start with simple module creation.

app/code/Denish/Example/etc/
app/code/Denish/Example/Block/

for register a module we will need two files.


- module.xml
- registration.php

app/code/Denish/Example/etc/module.xml


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

app/code/Denish/Example/registration.php


<?php
\Magento\Framework\Component\ComponentRegistrar::register(
\Magento\Framework\Component\ComponentRegistrar::MODULE,
'Denish_Example',
__DIR__
);

for enable and install/upgrade module please refer previous post i have posted in MAGENTO 2 BASIC COMMANDS.


Now creating our own preference using di.xml


app/code/Denish/Example/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">
<preference for="Magento\Catalog\Block\Product\ListProduct" type="Denish\Example\Block\Product\ListProduct" />
</config>


Now whenever code will ask for load ListProduct, preference will check and load Denish\Example\Block\Product\ListProduct instead of Magento\Catalog\Block\Product\ListProduct

That's it...!!!


Add div using layout file magento 2

add below code in layout file as per needed based on router.

<referenceContainer name="columns.top">
    <container name="custom.view.container" htmlTag="div" htmlClass="custom-view" after="breadcrumbs">
        <block class="namespace\module\Block\entity" name="customView" template="namespace_module::custom.phtml" />
    </container>
</referenceContainer>


referenceContainer name can be any where we want to insert div. please make sure name where want to inject div.


if wants to move from one place to another use code as per given below

<move element="custom.view.container" destination="columns.top" before="page.messages" />

element - name which element needs to move.
destination - name of the destination container element.
before/after - as per needed sequence.

That's it...!!!

add/remove links on my account navigation magento 2

Add New layout handle

Method 1 - 

If creating new module: VendorName/ModuleName/view/frontend/layout/customer_account.xml

Method 2 - 

Apply using theme.

app/design/frontend/VendorName/themeName/Magento_Customer/layout/customer_account.xml.

<?xml version="1.0"?>
<!--
/**
 * Copyright © 2016 Magento. All rights reserved.
 * See COPYING.txt for license details.
 */
-->
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <body>
        <referenceBlock name="customer-account-navigation-customer-balance-link" remove="true"/>
        <referenceBlock name="customer-account-navigation-downloadable-products-link" remove="true"/>
        <referenceBlock name="customer-account-navigation-newsletter-subscriptions-link" remove="true"/>
        <referenceBlock name="customer-account-navigation-billing-agreements-link" remove="true"/>
        <referenceBlock name="customer-account-navigation-product-reviews-link" remove="true"/>
        <referenceBlock name="customer-account-navigation-my-credit-cards-link" remove="true"/>
        <referenceBlock name="customer-account-navigation-account-link" remove="true"/>
        <referenceBlock name="customer-account-navigation-account-edit-link" remove="true"/>
        <referenceBlock name="customer-account-navigation-address-link" remove="true"/>
        <referenceBlock name="customer-account-navigation-orders-link" remove="true"/>
        <referenceBlock name="customer-account-navigation-wish-list-link" remove="true"/>
        <referenceBlock name="customer-account-navigation-gift-card-link" remove="true"/>
        <referenceBlock name="customer-account-navigation-checkout-sku-link" remove="true"/>
        <referenceBlock name="customer-account-navigation-giftregistry-link" remove="true"/>
        <referenceBlock name="customer-account-navigation-reward-link" remove="true"/>
    </body>
</page>

Add Link

<referenceBlock name="customer_account_navigation">
    <block class="Magento\Framework\View\Element\Html\Link\Current" name="customer-account-navigation-custom-link" after="-" >
         <arguments>
             <argument name="label" xsi:type="string" translate="true">Custom Link Label</argument>
             <argument name="path" xsi:type="string">custom-path</argument>
         </arguments>
    </block>
</referenceBlock>


That's it... !!!

Popular Posts

Have You Any Questins Let Us Know.

Contact Us
DENISH VACHHANI
Ahmedabad India