Note: dans un but d’anonymisation, le véritable libellé du vendor a été remplacé par MyVendor
.
Les fichiers obligatoires pour la création d’un nouveau module dans Magento 2 (ou Adobe Commerce)
app/code/MyVendor/KlarnaOnSiteMessaging/registration.php
1 2 3 4 5 6 |
<?php \Magento\Framework\Component\ComponentRegistrar::register( \Magento\Framework\Component\ComponentRegistrar::MODULE, 'MyVendor_KlarnaOnsitemessaging', __DIR__ ); |
app/code/MyVendor/KlarnaOnSiteMessaging/etc/module.xml
1 2 3 4 5 6 7 8 9 |
<?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="MyVendor_KlarnaOnsitemessaging" > <sequence> <module name="Klarna_Onsitemessaging" /> </sequence> </module> </config> |
app/code/MyVendor/KlarnaOnSiteMessaging/etc/di.xml
1 2 3 4 5 |
<?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="Klarna\Onsitemessaging\Block\Product" type="MyVendor\KlarnaOnsitemessaging\Block\Product"/> </config> |
Etendre un Block dans Magento 2
Le Block (fichier) initial
1 |
Le Block étendu
app/code/MyVendor/KlarnaOnSiteMessaging/Block/Product.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
<?php /** * This file is part of the Klarna Onsitemessaging module * * (c) Klarna Bank AB (publ) * * For the full copyright and license information, please view the NOTICE * and LICENSE files that were distributed with this source code. */ declare(strict_types=1); namespace MyVendor\KlarnaOnsitemessaging\Block; use Magento\Catalog\Helper\Data; use Magento\Framework\Locale\Resolver; use Magento\Framework\View\Element\Template; use Magento\Framework\View\Element\Template\Context; use Magento\Store\Model\ScopeInterface; /** * @api */ class Product extends \Klarna\Onsitemessaging\Block\Product { /** @var \Magento\Framework\Registry $registry */ protected $registry; /** * @param Context $context * @param \Magento\Framework\Registry $registry $registry * @param Resolver $locale * @param Data $productHelper * @param array $data * @codeCoverageIgnore */ public function __construct( Context $context, \Magento\Framework\Registry $registry, Resolver $locale, Data $productHelper, array $data = [] ) { parent::__construct( $context, $locale, $productHelper, $data ); $this->registry = $registry; } public function getProduct() { return $this->registry->registry('current_product'); } /** * Check to see: * - if display on product is enabled or not * - whether the product type or stock allows to purchase the product (isSalable = true && isInStock = true) * - if product final price < 10000 * * @return bool */ public function conditionalShowOnProduct(): bool { $product = $this->getProduct(); $finalPrice = $product->getFinalPrice(); $finalPriceThreshold = 10000; //TODO $isAvailable = $product->isAvailable(); return $this->showOnProduct() && $isAvailable && $finalPrice < $finalPriceThreshold; } } |
1 |
1 |
1 |
1 |
1 |
1 |