Monthly Archives: February 2021
Every store works hard trying to make their customers happy. However, sometimes it happens that people want their money back. This is an inevitable step in Magento order processing.
In such cases, people request a refund which in Magento is called Credit Memo.
And in this article, you will find out what is it and how to create it. Besides, you will learn about a solution that will help you to get rid of refunded orders.
What is Credit Memo in Magento 2?
Magento 2 Credit Memo is a document that defines how much money will be returned to the customer. How you create credit memos in Magento 2 generally depends on the payment method used while placing the order.
Based on this there are 2 types of credit memos: Online and Offline.
WebP — is a modern image file format many websites use in order to reduce image size and improve website performance. But let's not forget that big-size images can slow down your application loading too.
Consider using WebP images in your application for Magento 2?
Here is how.
If you have a third-party application (e.g. application for iOS, Android, etc.) and use it for your Magento 2 store, you can display images in WebP format there, even if you only have the PNG, JPG, or other format images.
The only thing you need is our Magento 2 WebP Images extension installed on your Magento 2 store.
So, to display WebP images in third-party applications you have to define the getWebPUrl function in the aplication code. Find the example below:
function getWebUrl(imageUrl) {
/* @var string */
imageUrl = imageUrl.trim();
var baseUrl = 'https://mystore.com/';
var imageFormat = imageUrl.split('.').pop();
var mediaBaseUrl = 'https://mystore.com/pub/media/';
var staticBaseUrl =
Sometimes you need to run Magento 2 code externally, in the following cases:
- Magento 2 integration with other frameworks or platforms that are installed on the same web server,
- quick test execution of some method, for example, cron job.
In Magento 2 you can create plugins (interceptors) that allow you to extend functionality and execute your own code before, after, or around any PHP class public method.
For example there is a PHP class with public functions "setName", "getName", "save":
<?php
namespace VendorName\ModuleName\Folder;
class SomeModel
{
private $name;
public function setName($name, $storeId = 0) {
$this->name[0] = $name;
}
public function getName($storeId = 0) {
return $this->name[$storeId];
}
public function save($fields, $customData) {
/* some save logic here */
}
}
How to create plugin in Magento 2?
1. Create the etc/di.xml file in your module folder and add the following code there:
<?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="VendorName\ModuleName\Folder\SomeModel">
<plugin name="mycompany_mymodule_plugin_modulename_Folder_somemodel"
type="MyCompany\MyModule\Plugin\ModuleName\Folder\SomeModelPlugin" sortOrder="10" />
</type>
</config>
Downloadable products in Magento 2 have no physical form and require practically nothing to manage. You don't need to cover manufacturing, publishing, shipping or delivery, etc. And that is what makes downloadable products so popular.
In Magento 2, except for creating a downloadable product, you have to configure the general Downloadable Product Options.
These steps to configure Downloadable Products Options in Magento 2:
- Go to Stores > Configuration > Catalog > Catalog and find the Downloadable Product Options section.
- Choose the Order Item Status to Enable Downloads which defines if the order needs to be Invoiced or Pending for a customer to be able to download the product.
- Set the Default Maximum Number of Downloads available for 1 customer once he purchases the downloadable product.
One of the primary tasks of any storeowner is to make the shopping experience as diverse and advanced as possible. Magento 2 custom options can help with that. They help you to provide your customers with an opportunity to choose product options as to their needs without relying on the product attributes.
Such functionality allows you to categorize products, create cart price and catalog price rules. Thus, you may configure it for your own store too. And this is exactly what you'll learn today.
How to Configure Magento 2 Custom Options?
1. Navigate to Catalog > Products and choose the product you'd like to configure customizable options for or Add New Product if needed.
Note: you can't configure Magento custom options for simple products that already constitute the configurable product.
2. Scroll down to the Customizable Options section and press the Add Option button.
- Specify the Option Title and Type.
- Check if you want this option to be Required.
Note: once you check the Required
In the previous article you learned how to create new tables in Magento 2 database. In this one, you will learn more about the models used to work with the Magento 2 database which allows you to read, edit and delete the data.
Magento 2 and Magento 1 uses the Model/ResourceModel/Collection ORM (Object-relational mapping) for these purposes. To implement this concept you need to create 3 files (model, resource model, collection).
When developing an online store based on Magento 2, you might face the problem of extending the standard Magento 2 or Magento extensions functionality.
Editing Magento core files or modules is not allowed because these changes will be overwritten during the upgrade.
So, how to make changes to the storefront (frontend) template file, css, js?
WARNING! The following instructions cannot be used to override the layout files.
Override module view-files in the app/code folder.
Let's say you need to change this file:
app/code/Magefan/Blog/view/frontend/templates/post/view.phtml
Make its copy in the theme folder with the following path:
app/design/frontend/ThemeVendor/themename/Magefan_Blog/templates/post/view.phtml
Make the necessary changes to the newly created theme file.
If your own theme is missing from your Magento installation, create new theme.
If you get an error "An element with a “root” ID already exists" in your Magento 2, it is, most likely, related to a third-party extension that calls methods to re-render a page.
To fix the "An element with a “root” ID already exists" error:
1. Find a PHP file and a line that throws an exception.
2. Open CLI (terminal), navigate to the Magento root directory, and run the following commands to find the proper file:
grep vendor/ -re ' ID already exists'
grep app/ -re ' ID already exists'
As a result, you will get an output similar to this:
vendor/magento/framework/Data/Form.php: 'An element with a "' . $elementId . '" ID already exists.'
vendor/magento/framework/Data/Test/Unit/FormTest.php: $this->expectExceptionMessage('An element with a "1" ID already exists.');
vendor/magento/framework/Data/Structure.php: new \Magento\Framework\Phrase('An element with a "%1" ID already exists.', [$elementId])
3. Then open each file (except *Test.php) and put a backtrace and exit function before each
If you want to change product attribute type from dropdown to text in Magento 2, all you need to do is to run the following SQL queries (please make a Database backup before execution):
1. This query will change the attribute settings and convert it to a text attribute.
UPDATE eav_attribute SET
backend_type = "varchar",
frontend_input = "text",
source_model = ""
WHERE attribute_code = "MY_ATTRIBUTE_CODE";
2. This query will copy dropdown attribute data to the text attribute value table and replace the option IDs with their actual labels (text).
INSERT INTO catalog_product_entity_varchar
SELECT null as value_id, pei.attribute_id, pei.store_id, pei.entity_id, aov.value as value
FROM catalog_product_entity_int pei
LEFT JOIN eav_attribute_option ao ON pei.attribute_id = ao.attribute_id
LEFT JOIN eav_attribute_option_value aov ON ao.option_id = aov.option_id
WHERE
pei.value IS NOT NULL
AND pei.attribute_id = (SELECT attribute_id FROM eav_attribute WHERE attribute_code = "MY_ATTRIBUTE_CODE
Sometimes, during Magento 2 customization you need to get the store Information programmatically. You may need to get a current Store ID, Store Code, Name, Website ID, or current URL.
To retrieve this data use the singleton instance of the following class:
\Magento\Store\Model\StoreManagerInterface
For example, you can include it in your class constructor and then call:
<?php
namespace \MyCompany\ExampleModule\Model;
class Example
{
private $storeManager;
public function __construct( ... \Magento\Store\Model\StoreManagerInterface $storeManager, ... ) {
... $this->storeManager = $storeManager; ... } /**
* Examples
*/
public function execute()
{
/* Get Current Store ID */
$storeId = $this->storeManager->getStore()->getId();
/* Get Current Store Code */
$storeCode = $this->storeManager->getStore()->getCode();
/* Get Current Store Name */
$storeName = $this->storeManager->getStore()->getName();
Get URL in the .phtml template files
1. Get URL of the store homepage (http://domain.com/):
<?= $this->getUrl() ?>
2. Get URL of some page, e.g. "Contact Us" (http://domain.com/contacts/):
<?= $this->getUrl('contacts') ?>
3. Get URL of the theme static file, e.g.:
app/design/frontend/ThemeVendor/theme_name/web/css/custom.css
app/design/frontend/ThemeVendor/theme_name/Magefan_Blog/web/js/lazyload.js
app/code/Magefan_Blog/view/frontend/web/js/lazyload.js
vendor/magefan/module-blog/view/frontend/web/js/lazyload.js
use the following code:
<?= $this->getViewFileUrl('css/custom.css') ?>
<?= $this->getViewFileUrl('Magefan_Blog::js/lazyload.js') ?>