Note: dans un but d’anonymisation, le véritable libellé du vendor a été remplacé par MyVendor
.
Ressource en ligne: Créer un bloc sous magento 2.
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
vendor/klarna/module-onsitemessaging/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 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 |
<?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 Klarna\Onsitemessaging\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 Template { /** * @var Data */ private $productHelper; /** * @var Resolver */ private $locale; /** * @param Context $context * @param Resolver $locale * @param Data $productHelper * @param array $data * @codeCoverageIgnore */ public function __construct(Context $context, Resolver $locale, Data $productHelper, array $data = []) { parent::__construct($context, $data); $this->locale = $locale; $this->productHelper = $productHelper; } /** * Check to see if display on product is enabled or not * * @return bool */ public function showOnProduct(): bool { return $this->isSetFlag('klarna/osm/enabled') && $this->isSetFlag('klarna/osm/product_enabled'); } /** * Get the locale according to ISO_3166-1 standard * * @return string */ public function getLocale(): string { return str_replace('_', '-', $this->locale->getLocale()); } /** * Get placement id * * @return string */ public function getPlacementId(): string { $placementId = $this->getValue('klarna/osm/product_placement_select'); if ($placementId && $placementId === 'other') { $placementId = $this->getValue('klarna/osm/product_placement_other'); } return $placementId; } /** * Get theme (default or dark) * * @return string */ public function getTheme(): string { return $this->getValue('klarna/osm/theme'); } /** * Get the amount of the purchase formated as an integer `round(amount * 100)` * * @return int */ public function getPurchaseAmount(): int { $product = $this->productHelper->getProduct(); $price = $product->getFinalPrice($product->getQty()); return (int)round($price * 100); } /** * Wrapper around `$this->_scopeConfig->isSetFlag` that ensures store scope is checked * * @param string $path * @return bool */ private function isSetFlag(string $path): bool { return $this->_scopeConfig->isSetFlag($path, ScopeInterface::SCOPE_STORE, $this->_storeManager->getStore()); } /** * Wrapper around `$this->_scopeConfig->getValue` that ensures store scope is checked * * @param string $path * @return mixed */ private function getValue(string $path) { return $this->_scopeConfig->getValue($path, ScopeInterface::SCOPE_STORE, $this->_storeManager->getStore()); } } |
Le Block étendu
app/code/MyVendor/KlarnaOnsitemessaging/Block/Product.php
Raison de l’extension: dans la fiche produit, nous avons besoin de cacher la bannière Klarna pour les produits > 10 000 euros ainsi que les produits not salable ou qui ont un stock = 0.
Nous allons pour ce faire créer une fonction publique getProduct()
qui va nous permettre de récupérer tout un tas d’informations sur le produit affiché et notamment celles que nous allons exploiter dans la fonction publique conditionalShowOnProduct()
qui teste que toutes les conditions sont remplies pour afficher la bannière.
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 78 79 80 81 82 83 84 85 86 87 88 89 |
<?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; /** * @api */ class Product extends \Klarna\Onsitemessaging\Block\Product { const CHECKOUT_PRICES_ABOVE = 'checkout/prices_above'; /** * @var \Magento\Framework\Registry */ protected \Magento\Framework\Registry $registry; /** * @var \Magento\Framework\App\Config\ScopeConfigInterface */ protected \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig; /** * Product constructor. * * @param \Magento\Framework\View\Element\Template\Context $context * @param \Magento\Framework\Locale\Resolver $locale * @param \Magento\Catalog\Helper\Data $productHelper * @param \Magento\Framework\Registry $registry * @param array $data */ public function __construct( \Magento\Framework\View\Element\Template\Context $context, \Magento\Framework\Locale\Resolver $locale, \Magento\Catalog\Helper\Data $productHelper, \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig, \Magento\Framework\Registry $registry, array $data = [] ) { parent::__construct( $context, $locale, $productHelper, $data ); $this->registry = $registry; } /** * @return \Magento\Catalog\Model\Product */ public function getProduct(): \Magento\Catalog\Model\Product { 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 = (int) $this->_scopeConfig->getValue( self::CHECKOUT_PRICES_ABOVE, \Magento\Store\Model\ScopeInterface::SCOPE_STORE ); $isAvailable = $product->isAvailable(); return $this->showOnProduct() && $isAvailable && $finalPrice <= $finalPriceThreshold; } } |
app/code/MyVendor/KlarnaOnsitemessaging/etc/config.xml
Dans la class Product
que nous venons d’étendre (cf. extension de /Block/Product.php), nous utilisons une config const CHECKOUT_PRICES_ABOVE = 'checkout/prices_above';
que nous définissons dans ce fichier (plutôt qu’en dur).
1 2 3 4 5 6 7 8 9 10 11 |
<?xml version="1.0"?> <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Store:etc/config.xsd"> <websites> <de> <checkout> <prices_above>10000</prices_above> </checkout> </de> </websites> </config> |
app/design/frontend/MyTheme/default/Klarna_Onsitemessaging/templates/html/placement/product.phtml
On interroge la fonction conditionalShowOnProduct()
présente dans notre Block étendu pour afficher ou non la bannière Klarna.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
<?php /** @var MyVendor\KlarnaOnsitemessaging\Block\Product $block */ /** @var Magento\Framework\Escaper $escaper */ ?> <?php if ($block->conditionalShowOnProduct()): ?> <!-- Placement v2 --> <div id="klarna-placement"> <klarna-placement data-key="<?= $escaper->escapeHtml($block->getPlacementId()) ?>" data-locale="<?= $escaper->escapeHtml($block->getLocale()) ?>" <?php if ($block->getPurchaseAmount()): ?> data-purchase-amount="<?= $escaper->escapeHtml($block->getPurchaseAmount()) ?>" <?php endif ?> data-theme="<?= $escaper->escapeHtml($block->getTheme()) ?>" ></klarna-placement> </div> <!-- end Placement --> <?php endif ?> |