Een module maken
Een module is een structuurelement van Commerce - modules vormen de ruggengraat van het systeem. U begint typisch een aanpassing door een module te bouwen.
Voor wie is deze video?
- Achtergrondontwikkelaars
Stappen om een module toe te voegen
- Maak de modulemap.
- Maak het
etc/module.xml-bestand. - Maak het
registration.php-bestand. - Voer
bin/magento setup:upgradeuit om de module te registreren en te installeren. - Controleer of de module werkt.
Het bestand 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="Training_Sales">
<sequence>
<module name="Magento_Sales"/>
</sequence>
</module>
</config>
Het bestand registration.php
<?php
use Magento\Framework\Component\ComponentRegistrar;
ComponentRegistrar::register(
ComponentRegistrar::MODULE,
'Training_Sales',
__DIR__);
Een plug-in toevoegen
Vervolgens voegt u functionaliteit toe aan de basismodule. U gebruikt plug-ins als essentiële hulpmiddelen bij de ontwikkeling van Adobe Commerce. In deze video en zelfstudie ziet u hoe u een plug-in kunt maken.
Te onthouden zaken voor plug-ins
- U declareert alle plug-ins in
di.xml. - U geeft elke plug-in een unieke naam.
- U kunt desgewenst de kenmerken
disabledensortOrderinstellen. - U stelt het bereik van de insteekmodule in door te kiezen welke map het
di.xml-bestand bevat. - U stelt stoppen vóór, na, of rond de vraag van de doelmethode in werking.
- Vermijd insteekmodules van het type
around. Ze verleiden je, maar ze geven vaak de verkeerde keuze en veroorzaken prestatieproblemen.
Codevoorbeelden van insteekmodules
De zelfstudie gebruikt de volgende klassen XML en PHP om een plug-in toe te voegen aan uw eerste module.
app/code/Training/Sales/etc/adminhtml/di.xml
<?xml version="1.0" ?>
<!--
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
-->
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<!-- A Plugin that executes when the admin user places an order -->
<type name="Magento\Sales\Model\Order">
<plugin name="admin-training-sales-add-logging" type="Training\Sales\Plugin\AdminAddLoggingAfterOrderPlacePlugin" disabled="false" sortOrder="0"/>
</type>
</config>
app/code/Training/Sales/etc/frontend/di.xml
<?xml version="1.0" ?>
<!--
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
-->
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<!-- A plugin that executes when a customer uses the LoginPost controller from the Luma frontend -->
<type name="Magento\Customer\Controller\Account\LoginPost">
<plugin name="training-customer-loginpost-plugin"
type="Training\Sales\Plugin\CustomerLoginPostPlugin" sortOrder="20"/>
</type>
</config>
app/code/Training/Sales/etc/webapi_rest/di.xml
<?xml version="1.0" ?>
<!--
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
-->
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<!-- A plugin that executes when the REST API is used OR when the Luma frontend places an order -->
<type name="Magento\Sales\Model\Order">
<plugin name="rest-training-sales-add-logging" type="Training\Sales\Plugin\RestAddLoggingAfterOrderPlacePlugin"/>
</type>
</config>
app/code/Training/Sales/Plugin/AdminAddLoggingAfterOrderPlacePlugin.php
<?php
declare(strict_types=1);
namespace Training\Sales\Plugin;
use Magento\Sales\Model\Order;
use Psr\Log\LoggerInterface;
/**
*
*/
class AdminAddLoggingAfterOrderPlacePlugin
{
/**
* @var LoggerInterface
*/
private LoggerInterface $logger;
/**
* @param LoggerInterface $logger
*/
public function __construct(
LoggerInterface $logger
) {
$this->logger = $logger;
}
/**
* Add log after an order is placed
*
* @param Order $subject
* @param Order $result
* @return Order
*/
public function afterPlace(Order $subject, Order $result): Order
{
// Log this admin area order
$this->logger->notice('An ADMIN User placed an Order - $' . $subject->getBaseTotalDue());
return $result;
}
}
app/code/Training/Sales/Plugin/CustomerLoginPostPlugin.php
<?php
declare(strict_types=1);
namespace Training\Sales\Plugin;
use Psr\Log\LoggerInterface;
use Magento\Framework\App\RequestInterface;
/**
* Introduces Context information for ActionInterface of Customer Action
*/
class CustomerLoginPostPlugin
{
/**
* @var LoggerInterface
*/
private LoggerInterface $logger;
/**
* @var RequestInterface
*/
private RequestInterface $request;
/**
* @param LoggerInterface $logger
* @param RequestInterface $request
*/
public function __construct(LoggerInterface $logger, RequestInterface $request)
{
$this->logger = $logger;
$this->request = $request;
}
/**
* Simple example of a before Plugin on a public method in a Controller
*/
public function beforeExecute()
{
$login = $this->request->getParam('login');
$username = $login['username'];
$this->logger->notice( "Login Post controller was used for " . $username );
}
}
app/code/Training/Sales/Plugin/RestAddLoggingAfterOrderPlacePlugin.php
<?php
declare(strict_types=1);
namespace Training\Sales\Plugin;
use Magento\Sales\Model\Order;
use Psr\Log\LoggerInterface;
/**
*
*/
class RestAddLoggingAfterOrderPlacePlugin
{
/**
* @var LoggerInterface
*/
private LoggerInterface $logger;
/**
* @param LoggerInterface $logger
*/
public function __construct(
LoggerInterface $logger
) {
$this->logger = $logger;
}
/**
* Add log after an order is placed
*
* @param Order $subject
* @param Order $result
* @return Order
*/
public function afterPlace(Order $subject, Order $result): Order
{
// Log this customer driven order
$this->logger->notice('A Customer placed an Order for $' . $subject->getBaseTotalDue());
return $result;
}
}
Nuttige bronnen
recommendation-more-help
3a5f7e19-f383-4af8-8983-d01154c1402f