development
Delivering the best experience in your blog requires a lot of things. We've covered most of them in our
. However, sometimes you need to tweak the default settings to suit your requirements.By default, blog templates automatically resize images to 1000. We do that to improve the loading speed since users usually upload larger (heavier) images. This value is selected as a default based on our previous experiences.
However, in case that doesn't work for you, you can easily resize the featured images on the posts list.
To resize features images on blog posts:
1. Create the following file in your Magento 2:
app/design/frontend/ThemeVendor/theme_name/Magefan_Blog/layout/blog_post_list.xml
2. Add the following code to it:
<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
<body>
<referenceBlock name="blog.posts.list">
<action
In this article, we describe the GraphQl queries for getting the necessary auto-related rule data for Progressive Web App (PWA). To implement the GraphQL resolver you need to install Magento 2 Automatic Related Products Extra.
Besides, you can easily check those queries with the ChromeiQL extension for Chrome Browser.
Related Product Rule GraphQL Request
With this query you will get all related products using rule ID (products that fall under the rule conditions).
Query:
query mfAutoRelatedProducts ($ruleId: Int $productId: Int $categoryId: Int $pageSize: Int $currentPage: Int) { mfAutoRelatedProducts( ruleId: $ruleId productId: $productId categoryId: $categoryId pageSize: $pageSize currentPage: $currentPage ){ total_count items{ products{ id name sku } category_ids{ category_id } customer_group_ids{ group_id } store_ids{ store_id } id name description status priority block_position merge_type from_one_category_only only_with_higher_price only_with_lower_price conditions_serialized
Customer support is extremely important if you want to keep your customer experience levels high. So, if you want to be able to log in to customer accounts outside your Shopify store admin (like your CRM, helpdesk, etc.), you can do this using the Shopify Login as Customer API keys.
To get the public and secret keys required for this, you need to:
1. Find the Login as Customer app in your store Shopify admin and go to the API Keys at the top right corner of the customers' list.
2. Copy your Public Key.
3. Generate new Secret Key if you need.
Now that you have your Shopify Login as Customer API keys, you can use them to extend the CRM or helpdesk you want to be able to log in to customer accounts from.
Assist customers faster, logging into their accounts without a password with a handy Login as Customer App.
Customer support is improved significantly with the Shopify Login as Customer App since you can assist customers in placing orders and fixing account-related issues timely. But that's not it.
Shopify store admin is not the only place where you can log in to your customers' accounts from. You can also log in to customer accounts from your CRM or helpdesk, etc., if they allow you to extend them and run requests to custom API that comes with our app.
You can log in to customer accounts using API in 2 different ways.
Note: the following code is written in PHP, but you can use any other programming language.
Log in As a Customer using the Customer ID
<?php
$publicKey = '****************';
$secretKey = '***********************************';
$shop = '**********'; // for example 'myshop.myshopify.com';
$customer = '**********'; //for example 123456789;
?>
<?php
$currentDate = date('Y-m-d');
$token = hash('sha256', $publicKey . $secretKey . $currentDate . $secretKey . $customer . $publicKey);
Magento 2 Preferences is used by the Magento 2 object manager to extend the default implementation. You can use preferences in Magento 2 to implement some interfaces or to rewrite/override the existing PHP classes and their methods.
In this article, you'll learn how to do it and discover useful examples that will help you along the way.
Overriding classes in Magento 2
If you want to rewrite existing class methods with Magento preference follow the steps below:
1. Create the etc/di.xml file in your extension folder:
app/code/MyCompany/ModuleName/etc/di.xml
2. Add this code to it:
<?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\Checkout\Block\Onepage\Success" type="MyCompany\ModuleName\Block\Onepage\Success" />
</config>
Use attribute "for" in the "preference" tag to define the PHP class that you want to override. Use attribute "type" to define
Menu Title [identifier]
Dashboard [Magento_Backend::dashboard]
Sales [Magento_Sales::sales] Operations [Magento_Sales::sales_operation] Orders [Magento_Sales::sales_order] Invoices [Magento_Sales::sales_invoice] Shipments [Magento_Sales::sales_shipment] Credit Memos [Magento_Sales::sales_creditmemo] Billing Agreements [Magento_Paypal::paypal_billing_agreement] Transactions [Magento_Sales::sales_transactions]
The etc/adminhtml/menu.xml file is used to control the Magento 2 admin panel menu and add new items to it, in particular.
In the previous article we described how to create your own section on the Magento 2 configuration page (Stores > Configuration).
In order to set the default values for the configuration fields, you need to create the following file in the module folder:
etc/config.xml
and add this code:
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Store:etc/config.xsd">
<default>
<section_id>
<group_id>
<field_id>default_value</field_id>
</group_id>
</section_id>
</default>
</config>
In the previous article we explained how to configure and delimitate access rights for the Magento 2 admin panel users. In this article, you will learn how to create your own access rules (Role Resources).
You need to create ACL file (ACL - Access Control List) in your module folder:
etc/acl.xml
To find the Magento 2 configuration page navigate to Magento 2 Admin Panel > Stores > Configuration.
All tabs and forms on this page are customized using this file
etc/adminhtml/system.xml
As you create your eCommerce store, there are multiple stages you go through: starting with development and gradually moving to live. Each stage has corresponding tasks to keep your website running.
Magento 2 handles this through the mode functionality. There are three Magento modes: default, development, and production. Each facilitates different store operations.
However, to use Magento 2 deploy modes effectively, you need to know what each is useful for. This is exactly what you will find out today: what Magento modes are, how they differ and how to switch between them.
What Are Magento 2 Deploy Modes?
Magento 2 deploy modes are modes that create specific operation conditions suitable for different store operation tasks. As we've mentioned, there are 3 core Magento deploy modes that make your experience better. Let's have a closer look at each of them.
1. Default mode
After installation, Magento 2 is in a default mode making it possible to use the platform without applying
Magento 2 Rocket JavaScript extension allows you to enable deferred JavaScript loading and move JS code to the bottom of the page automatically. That way you increase the website loading speed and eliminate render-blocking.
However, sometimes you don't need to move all JavaScripts to the bottom since there are some that aren't recommended to be moved there.
In this guide you'll learn how you can do that.
How to Defer Some JS Loading Automatically?
Navigate to Stores > Configuration > Magefan Extensions > Rocket Javascript and find the Ignore Deferred JavaScript With section. Then specify the strings you don't want to be moved to the bottom, each in a new line.
The extension will add data-rocketjavascript="false" attribute to them automatically so they will be ignored.
How to Defer Some JS Loading Manually?
In case you don't want to move some JavaScript to the bottom you just need to add the attribute data-rocketjavascript="false" to the <script> tag directly in your code:
Example:
Let's
Basic Linux Commands
pwd - displays information about the current location in the file system (the path of the directory (folder) you are in);
dir, ls - shows a list of files and folders in the current directory;
cd - (change the current directory) allows you to move to another folder;
Example:
cd ../ - will move to a folder on a higher level;
cd foo - will go to the child folder "foo";
cd /var - will go to the "var" folder located in the root of the file system;
touch fine_name - create a new file named "fine_name";
mkdir dir_name - create a new folder "dir_name" in the current directory;
rm file_name - delete the file "file_name";
rm -r dir_name - delete the folder "dir_name";
cp origin_name new_name - copy files and folders;
mv old_name new_name - move files and folders;
ln -s origin_name link_name - create a symbolic link;
The examples of some GraphQL queries for the Magento 2 blog module are presented in this article. With these queries, you will be able to extract the necessary blog data for Progressive Web Application (PWA). The queries are easily tested in the ChromeiQL - Chrome Browser Extension.
Note that Magento 2 Blog GraphQL addition should be installed first.
To check the full Blog GraphQL schema please see the schema.graphqls file.
Blog Index Page Posts GraphQL Request
Query:
query GetPosts ($currentPage: Int $pageSize: Int) { blogPosts (currentPage: $currentPage pageSize: $pageSize sort: ["DESC"]) { total_count items { post_id title short_filtered_content author { author_id name author_url } post_url creation_time tags { tag_id title tag_url } categories { category_id title category_url } } total_pages } }
Variables:
{
"currentPage": 1,
"pageSize": 5
}
Result:
As a result, you will receive data for the first 5 blog posts that should be displayed on the first page.
Blog Category Posts GraphQL
In order to easily test the GraphQL queries all you need to do is install the ChromeiQL extension for Chrome Browser by pressing the Add to Chrome.
Once you have added it you will see the extension icon in the right top corner of the browser window. Pressing it will move you to the new window where the testing is actually conducted.
This is the example of how we test the GraphQL query for Magefan Magento 2 Blog module:
- Set the Endpoint URL in the corresponding field.
- In the left field set the query. It is also allowed to state the query parameters if needed below the query section.
- Press the Run ( > ) button and see the result on the right.
To get a resized post image in Magento 2 Blog you can use this code in your template files:
<?php $width = 100; $height = 150; $imageHelper = $this->helper(\Magefan\Blog\Helper\Image::class); ?> <img src="<?php echo $imageHelper->init($_post->getFeaturedImg())->resize($width, $height); ?>" alt="<?php echo $block->escapeHtml($_post->getTitle()) ?>" />
If you don't want to use the height parameter then you can execute the resize function only with the first parameter, e.g.
<?php echo $imageHelper->init($_post->getFeaturedImg())->resize($width); ?>
Or if you use Magefan Blog version greater equal than v2.9.8, you can use this line (it will allow you to save the image width x height file proportion and not create a square image with white borders):
<?php echo $imageHelper->init($_post->getFeaturedImg())->resize($width, null, false); ?>
The sortOrder property for plugins in Magento 2 determines when to call them (before, after, or around a method), on condition more than one plugin is configured for the same method.
<config> <type name="{ObservedType}"> <plugin name="{pluginName}" type="{PluginClassName}" sortOrder="1" disabled="false" /> </type> </config>
Prioritizing rules are as follows:
- Before executing the original method, Magento will run before plugins from the smallest to the biggest value in sortOrder.
- The part of the plugin around code will also be executed from the smallest to the biggest value before calling the original method (callable).
- The after plugin is called from the biggest to the smallest after calling the original method.
If you need to extend Magento 2 Blog Module by Magefan and add a new custom text field to Blog Post edit page in Magento 2 Admin panel (Content > Blog > Posts) you must to create a simple custom Magento 2 Extension.
You can download a sample extension from our GitHub page or you can create it manually using the steps below:
1. Create composer.json file
app/code/Magefan/BlogCustomField/composer.json
{
"name": "magefan/module-blog-custom-field",
"description": "Implements Blog functionality on Magento 2 store",
"require": {
"magefan/module-blog" : ">=2.8.0"
},
"type": "magento2-module",
"version": "2.0.0",
"license": [
"OSL-3.0",
"AFL-3.0"
],
"autoload": {
"files": [ "registration.php" ],
"psr-4": {
"Magefan\\BlogCustomField\\": ""
}
}
}
Have you ever wondered how easy it is to check the quality of your own code, your colleagues' code, or a third-party module you want to use on a Magento project?
The Magento development team has created the Magento Extension Quality Program Coding Standard (Magento EQP), which allows you to check the code compliance with the standard, as well as identify the following shortcomings:
- SQL queries execution in the loop;
- use of dangerous functions;
- use of super-global variables;
- excessive code complexity;
- Unjustified collections loads.
Imagine you are cooking pasta and sit down to play some game. You listen with one ear to see if the "dish" does not boil away. In this case, you are an observer. When the pasta starts to boil away — the event "pasta_began_to_boil_away" is triggered, which causes you (the observer) to rush to the kitchen (perform an action, an algorithm).
In Magento 2, as in real life, there are observers and events that are implemented on the basis of the "Publish-subscribe pattern". We have already described plugins in Magento 2, which allows you to extend and change the functionality of the store. Let's see how you can do this with the help of events and observers in Magento 2.
Events
Events are triggered by Magento 2 modules when an action has occurred or has to occur. Magento has many native events and also allows you to call your own event.
Use the dispatch method of the \Magento\Framework\Event\ Manager class to call an event.
You often need to change the JavaScript code logic located in the .js file. The easiest way is to override the js file is by using a theme. You can learn how to override view files in the article about Overriding Magento 2 Storeftont Files. This is a quick but not an elegant way.
To change one or more js-file methods, use the mixins available in RequireJS.
To extend this file:
app/code/VendorName/ModuleName/view/%area%/web/js/folder1/folder2/somefile.js