Case history: how to create a plugin that adds the classical label on product image in the different layouts concerning products (product page, category page, related products, upsell, cross-sell).
Aims:
1. To create two labels with different shape and position,
2. To have a divided graphic management of these labels,
3. To avoid linking labels to single products, but working on categories
They focused on some issues that highlight the new mechanisms in Magento 2, compared to the usual procedures in Magento 1:
– Structure of folds and plugin files, different from Magento 1,
– Plugin registration, different from Magento 1,
– Use of dependency injection in M2,
– Core rewriting through ‘plugin’ in M2,
– Use of ‘RequireJS’ to load javascript in M2,
– Adding an ACL, different from Magento 1,
– Adding setting parameters and source models, similar to M1.
They shared technical choices, the problems we faced, the status of the documentation available online and the interaction with Magento community.
3. Giorgio Bignozzi
Technical Manager | Marketing Informatico
https://www.marketinginformatico.it
@GiorgioBignozzi
Michele Fantetti
Magento Developer at Maggioli Editore
http://www.fantetti.net
@WaP_oNe
Who we are
4. Abstract:
Our aim was to create an extension that adds the classical label to the product image in the
different layouts concerning products (product page, category page, related products,
upsell products, cross-sell products, widget, search results).
Goals:
• To create two labels (“Discount” and “New”) with different shapes and positions
• To have a separate graphic management of these labels (custom label or image). If you
choose a custom label, you can set two labels with a configurable text, text colour and
background colour
• To display the label on sale products or products associated to certain categories
(“Discount”)
• To display the label on products associated to certain categories (“New”)
• To enable/disable automatically (with programmable date and time) one or both the
stickers.
Our project: Stickers
10. • ISSUE #1: how to add the label (HTML code) without modifying layout files in
multiple pages and making the plugin independent
• SOLUTION #1: when rendering product attributes, upload HTML code that will
be completed lately via script JS.
• ISSUE #2: how and where to insert this HTML code
• SOLUTION #2: thanks to Magento 2 PLUGIN, beforeGetProductPrice() and
afterGetProductPrice().
Development: problems and solutions (1)
11. • ISSUE #3: how to insert extra code to this HTML code
• SOLUTION #3: requireJS
• ISSUE #4: enable/disable some configurable fields depending on which fields
you have valued before
• SOLUTION #4: upload a JS in the layout of the configuration page of the plugin
Development: problems and solutions (2)
12. File System: M1 VS M2
Differences in file
system organization
in M1 e M2
(Vanilla installation)
23. Issue: to create a hierarchy when enabling/disabling fields
Solution: to upload a template in the layout of the configuration page
(adminhtml_system_config_edit.xml):
<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" layout="admin-2columns-left"
xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
<body>
<referenceContainer name="footer">
<block class="MainfStickersBlockConfig" template="Mainf_Stickers::js.phtml"/>
</referenceContainer>
</body>
</page>
In js.phtml template insert the JS code:
<script type="text/javascript">
require(
['jquery'],
function($) {
$(function() {
..
});
}
);
</script>
Development: module configuration & customization
24. • crontab.xml file creation (new!):
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Cron:etc/crontab.xsd">
<group id="default">
<job name="mainf_stickers_activation" instance="MainfStickersModelObserver”
method="checkStickersActivation">
<schedule>*/5 * * * *</schedule>
</job>
</group>
</config>
• Observer.php file creation:
control of the status of the sticker (enabled/disabled)
comparison of date
enabling/disabling the sticker
flush of date field
Development: Observer
28. • Structural improvements for the development of extensions:
all files for each extension in the same folder;
substitution of controllers/ folder with Controller/ ;
use of XML Style Definition (XSD) within xml file;
split of config.xml in multiple xml files, each with a precise task (module.xml,
routes.xml, crontab.xml etc.) ;
one file for each action;
bin/magento
• Lots of bugs still open (1695 in GitHub, our is #8370), community not ready
as it is for M1 and a lot of doubts.
Conclusions
29. You can download the extension “Stickers” here:
https://github.com/WaPoNe/module-stickers
Or you can buy the extension “Stickers Pro” here:
http://www.magentostoremanager.it/module-stickers-
pro.html
Discount coupon for you: MMIT17
Reference