Sunday 24 November 2019

How to Call CMS Static Block in Phtml File in magento 2

Display CMS Static Block In Phtml File:
<?php
    echo $block->getLayout()
               ->createBlock('Magento\Cms\Block\Block')
               ->setBlockId('block_identifier')
               ->toHtml();
?>

Tuesday 19 November 2019

Magento2 cannot load some transaction emails

error : main.CRITICAL: Zend_Json_Exception: Decoding failed: Syntax error in /home/forge/domain.com/vendor/magento/zendframework1/library/Zend/Json.php:97

This is a bug in Magento 2 and here is the solution:
This will fixed the invalid json objects in the order email templates.

See this on vendor/magento/module-sales/view/frontend/email/order_new.html file.

<!--@vars { "var formattedBillingAddress|raw":"Billing Address", "var order.getEmailCustomerNote()":"Email Order Note", "var order.increment_id":"Order Id", "layout handle=\"sales_email_order_items\" order=$order area=\"frontend\"":"Order Items Grid", "var payment_html|raw":"Payment Details", "var formattedShippingAddress|raw":"Shipping Address", "var order.getShippingDescription()":"Shipping Description" "var shipping_msg":"Shipping message" } @-->

Take a look at this line:

"var order.getShippingDescription()":"Shipping Description"

It needs comma (,) at the end of this line to make it work. So it goes with the other files or template files.

So it should be like this:

<!--@vars { "var formattedBillingAddress|raw":"Billing Address", "var order.getEmailCustomerNote()":"Email Order Note", "var order.increment_id":"Order Id", "layout handle=\"sales_email_order_items\" order=$order area=\"frontend\"":"Order Items Grid", "var payment_html|raw":"Payment Details", "var formattedShippingAddress|raw":"Shipping Address", "var order.getShippingDescription()":"Shipping Description", "var shipping_msg":"Shipping message" } @-->

Monday 18 November 2019

Discount not applied to Grand Total in magento 1.9

We fixed this by adding the following into /app/code/core/Mage/Sales/etc/config.xml sales/quote section.

<discount>
   <class>sales/order_invoice_total_discount</class>
    <after>subtotal</after>
</discount>

This is a terrible solution, obviously, and I have no idea why it is needed. We will have to research it further to come up with a better solution than editing a core file!

Tuesday 12 November 2019

Creating profile image module in magento 2



  1. Create folders under magento root
<magento root>/code/<vendor>/<module>/

  1. Create a file ‘registration.php’ and put the below contents in it
\Magento\Framework\Component\ComponentRegistrar::register(\Magento\Framework\Component\ComponentRegistrar::MODULE,'<vendor>_<module>',__DIR__);

  1. Create folder ‘etc’ and create file ‘module.xml’ in it,, and put the below content in it.
<?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="<vendor>_<module>" setup_version="1.0.0">
</module>
</config>

  1. Create a route for the controllers which are needed to be made. Create e folder ‘frontend’ in etc folder and create file ‘routes.xml’ in it, and enter the following details in it.
<?xml version="1.0" ?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:App/etc/routes.xsd">
<router id="standard">
<route frontName="<route_path>" id="<route_path>">
<module name="<vendor>_<module>"/>
</route>
</router>
</config>

  1. Create folder ‘Controller’ and create two folder in it.
Delete’ and ‘Index’. Create Index.php in both of it.

  1. In Delete -> Index.php , paste the following code

namespace <vendor>\<module>\Controller\Delete;

use Magento\Framework\App\Filesystem\DirectoryList;

class Index extends \Magento\Framework\App\Action\Action
{
protected $_filesystem;
protected $image;
protected $_file;

public function __construct(
\Magento\Framework\App\Action\Context $context,
\<vendor>\<module>\Block\Imageprofile $image,
\Magento\Framework\Filesystem\Driver\File $file,
\Magento\Framework\Filesystem $fileSystem
) {
parent::__construct($context);
$this->image = $image;
$this->_file = $file;
$this->_filesystem = $fileSystem;

}

public function execute(){

$fileName = 'IMG_'.$this->image->currentUser()->getId();
$mediaRootDir = $this->_filesystem->getDirectoryRead(DirectoryList::MEDIA)->getAbsolutePath('images/'.$this->image->currentUser()->getId().'/');
if ($this->_file->isExists($mediaRootDir.$fileName)) {
$this->_file->deleteFile($mediaRootDir.$fileName);
}
$this->_redirect('customer/account/');
}
}

  1. In Index -> Index.php , paste the following code

namespace <vendor>\<module>\Controller\Index;

use Magento\Framework\App\Filesystem\DirectoryList;


class Index extends \Magento\Framework\App\Action\Action
{
protected $_filesystem;
protected $image;
public function __construct(
\Magento\Framework\App\Action\Context $context,
\Magento\Framework\Filesystem $fileSystem,
\<vendor>\<module>\Block\Imageprofile $image
) {
parent::__construct($context);
$this->_filesystem = $fileSystem;
$this->image = $image;
}
public function execute()
{
$result = array();
if ($_FILES['profile_image']['name']) {
try {
// init uploader model.
$uploader = $this->_objectManager->create(
'Magento\MediaStorage\Model\File\Uploader',
['fileId' => 'profile_image']
);
$uploader->setAllowedExtensions(['jpg', 'jpeg', 'png']);
$uploader->setAllowRenameFiles(true);
$uploader->setFilesDispersion(false);
// get media directory
$mediaDirectory = $this->_filesystem->getDirectoryRead(DirectoryList::MEDIA);
// save the image to media directory
$result = $uploader->save($mediaDirectory->getAbsolutePath('images/'.$this->image->currentUser()->getId().'/'),'IMG_'.$this->image->currentUser()->getId());
} catch (Exception $e) {
\Zend_Debug::dump($e->getMessage());
}
}
$this->_redirect('customer/account/');
//echo "Hello";
}
}


  1. Finally create a folder named ‘Block’ and create a file ‘Imageprofile.php’ in it. Paste the following code in it.

namespace <vendor>\<module>\Block;

use Magento\Framework\App\Filesystem\DirectoryList;

class Imageprofile extends \Magento\Framework\View\Element\Template
{
protected $customerSession;
protected $_file;

public function __construct(
\Magento\Backend\Block\Template\Context $context,
\Magento\Customer\Model\Session $customerSession,
\Magento\Framework\Filesystem\Driver\File $file,
array $data = []
)
{
parent::__construct($context,$data);
$this->customerSession = $customerSession;
$this->_file = $file;
}
public function currentUser()
{
return $this->customerSession->getCustomer();//->getId();
}

public function getMediaUrl()
{
$mediaUrl = $this->_storeManager
->getStore()
->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_MEDIA);
return $mediaUrl;
}

public function checkExist()
{
$filename = 'IMG_'.$this->currentUser()->getId();
$filepath = $this->_filesystem->getDirectoryRead(DirectoryList::MEDIA)->getAbsolutePath('images/'.$this->currentUser()->getId().'/');
if ($this->_file->isExists($filepath.$filename)) {
return 1;
}else{
return 0;
}
//return $filepath.$filename;
}

}