这是我用来打印警告的两个文件:
extra.html(https://i.sstatic.net/IYo7wvWk.png) data.html(https://i.sstatic.net/TMabfFrJ.png)
这就是我得到的,但它是静态的,我希望它是有条件的: 在此输入图片描述
必须在某些 SKU 和类别之间过滤此警告,以首先检查兼容性。 如果没有兼容性,就应该扔掉它。
我自己制作了一个插件,但它不会影响我添加到购物车的文本的行为。
我的插件:
<?php
namespace Webkul\MiniCart\Plugin\Checkout\CustomerData;
use Magento\Checkout\Model\Cart;
use Magento\Framework\Message\ManagerInterface;
use Magento\Framework\Session\SessionManagerInterface;
class MiniCartPlug {
protected $messageManager;
protected $sessionManager;
protected $compatibilities = [
'951-0080' => ['compatible_accessories' => ['930-0026', '930-0027'], 'brand' => 'Glock', 'models' => ['17', '19', '43']],
'951-0082' => ['compatible_accessories' => ['930-0024', '930-0025'], 'brand' => 'Taurus', 'models' => ['G2C', 'G3C', 'G3']],
'951-0087' => ['compatible_accessories' => ['930-0026', '930-0027'], 'brand' => 'Sig Sauer', 'models' => ['P365 Macro']],
'951-0079' => ['compatible_accessories' => ['930-0026', '930-0027'], 'brand' => 'Sig Sauer', 'models' => ['P365 Macro']],
'951-0091' => ['compatible_accessories' => ['930-0038', '931-0038'], 'brand' => 'Springfield', 'models' => ['Hellcat Pro']],
'951-0072' => ['compatible_accessories' => ['930-0024', '930-0025'], 'brand' => 'Sig Sauer', 'models' => ['P320']],
'951-0090' => ['compatible_accessories' => ['930-0038', '931-0038'], 'brand' => 'Springfield', 'models' => ['Hellcat Pro']],
'951-0077' => ['compatible_accessories' => ['930-0024', '930-0025'], 'brand' => 'Sig Sauer', 'models' => ['P365 Macro']],
'951-0074' => ['compatible_accessories' => ['930-0026', '930-0027'], 'brand' => 'Sig Sauer', 'models' => ['P320']],
'951-0068' => ['compatible_accessories' => ['930-0024', '930-0025'], 'brand' => 'Springfield', 'models' => ['Hellcat']],
'951-0059' => ['compatible_accessories' => ['930-0026', '930-0027'], 'brand' => 'Sig Sauer', 'models' => ['P320']],
'951-0065' => ['compatible_accessories' => ['930-0024', '930-0025'], 'brand' => 'Sig Sauer', 'models' => ['P365 Macro']],
'951-0067' => ['compatible_accessories' => ['930-0024', '930-0025'], 'brand' => 'Taurus', 'models' => ['G2C', 'G3C', 'G3']],
'951-0066' => ['compatible_accessories' => ['930-0024', '930-0025'], 'brand' => 'Springfield', 'models' => ['Hellcat Pro']],
'951-0069' => ['compatible_accessories' => ['930-0026', '930-0027'], 'brand' => 'Glock', 'models' => ['17', '19', '43']],
'951-0053' => ['compatible_accessories' => ['930-0024', '930-0025'], 'brand' => 'Springfield', 'models' => ['Hellcat']],
'951-0063' => ['compatible_accessories' => ['930-0026', '930-0027'], 'brand' => 'Taurus', 'models' => ['G2C', 'G3C', 'G3']],
'951-0054' => ['compatible_accessories' => ['930-0024', '930-0025'], 'brand' => 'Taurus', 'models' => ['G2C', 'G3C', 'G3']],
'951-0055' => ['compatible_accessories' => ['930-0024', '930-0025'], 'brand' => 'Springfield', 'models' => ['Hellcat Pro']],
'951-0057' => ['compatible_accessories' => ['930-0026', '930-0027'], 'brand' => 'Glock', 'models' => ['17', '19', '43']],
'951-0058' => ['compatible_accessories' => ['930-0026', '930-0027'], 'brand' => 'Springfield', 'models' => ['Hellcat Pro']],
'952-0003' => ['compatible_accessories' => ['930-0001', '930-0006', '930-0007'], 'brand' => 'Glock', 'models' => ['19', '19X', '45', '23', '29', '32', '38']],
'952-0007' => ['compatible_accessories' => ['930-0001', '930-0006', '930-0007'], 'brand' => 'Palmetto State Armory', 'models' => [' PS9 Dagger']],
'951-0081' => ['compatible_accessories' => ['912-0026'], 'brand' => 'Taurus', 'models' => ['G3', 'G3c', 'G3X', 'G2c', 'G2 PT111']],
'951-0004' => ['compatible_accessories' => ['912-0026'], 'brand' => 'Taurus', 'models' => ['G3', 'G3c', 'G3X', 'G2c', 'G2 PT111']],
'951-0011' => ['compatible_accessories' => ['912-0029', '912-0079'], 'brand' => 'Springfield', 'models' => ['Hellcat/Hellcat OSP (NOT RDP)']],
'951-0003' => ['compatible_accessories' => ['912-0026'], 'brand' => 'Taurus', 'models' => ['G3', 'G3c', 'G3X', 'G2c', 'G2 PT111']],
'951-0093' => ['compatible_accessories' => ['912-0030', '912-0078'], 'brand' => 'Sig Sauer', 'models' => ['P365 XL']],
'951-0092' => ['compatible_accessories' => ['912-0030', '912-0078'], 'brand' => 'Sig Sauer', 'models' => ['P365 XL']],
'951-0071' => ['compatible_accessories' => ['912-0045'], 'brand' => 'Ruger', 'models' => ['MAX9']],
'951-0070' => ['compatible_accessories' => ['912-0049', '912-0050'], 'brand' => 'Palmetto State Armory', 'models' => ['']],
'951-0061' => ['compatible_accessories' => ['912-0049', '912-0050'], 'brand' => 'Palmetto State Armory', 'models' => ['']],
'951-0021' => ['compatible_accessories' => ['912-0045'], 'brand' => 'Ruger', 'models' => ['MAX9']],
'951-0031' => ['compatible_accessories' => ['912-0077'], 'brand' => 'Springfield', 'models' => ['Hellcat PRO']],
'951-0015' => ['compatible_accessories' => ['912-0043'], 'brand' => 'Taurus', 'models' => ['GX4']],
'951-0019' => ['compatible_accessories' => ['912-0030', '912-0078'], 'brand' => 'Sig Sauer', 'models' => ['P365']],
'951-0028' => ['compatible_accessories' => ['912-0030', '912-0078'], 'brand' => 'Sig Sauer', 'models' => ['P365']],
'951-0086' => ['compatible_accessories' => ['912-0003'], 'brand' => 'Taurus', 'models' => ['G3', 'G3c', 'G3X', 'G2c', 'G2 PT111']],
'951-0027' => ['compatible_accessories' => ['912-0030', '912-0078'], 'brand' => 'Sig Sauer', 'models' => ['P365']],
'951-0010' => ['compatible_accessories' => ['912-0071'], 'brand' => 'Ruger', 'models' => ['LCP MAX']],
'951-0013' => ['compatible_accessories' => ['912-0071'], 'brand' => 'Ruger', 'models' => ['LCP MAX']],
'951-0022' => ['compatible_accessories' => ['912-0045'], 'brand' => 'Ruger', 'models' => ['MAX9']],
'951-0012' => ['compatible_accessories' => ['912-0030', '912-0078'], 'brand' => 'Sig Sauer', 'models' => ['P365']],
'951-0009' => ['compatible_accessories' => ['912-0071'], 'brand' => 'Ruger', 'models' => ['LCP MAX']],
'951-0014' => ['compatible_accessories' => ['912-0071'], 'brand' => 'Ruger', 'models' => ['LCP MAX']],
'951-0026' => ['compatible_accessories' => ['912-0030', '912-0078'], 'brand' => 'Sig Sauer', 'models' => ['P365']],
'951-0084' => ['compatible_accessories' => ['912-0003'], 'brand' => 'Taurus', 'models' => ['G3', 'G3c', 'G3X', 'G2c', 'G2 PT111']],
'951-0020' => ['compatible_accessories' => ['912-0071'], 'brand' => 'Ruger', 'models' => ['LCP MAX']],
'951-0033' => ['compatible_accessories' => ['912-0077'], 'brand' => 'Springfield', 'models' => ['Hellcat PRO']],
'951-0037' => ['compatible_accessories' => ['912-0045'], 'brand' => 'Ruger', 'models' => ['MAX9']],
'951-0040' => ['compatible_accessories' => ['912-0071'], 'brand' => 'Ruger', 'models' => ['LCP MAX']],
'951-0073' => ['compatible_accessories' => ['912-0045'], 'brand' => 'Ruger', 'models' => ['MAX9']],
'951-0041' => ['compatible_accessories' => ['912-0077'], 'brand' => 'Springfield', 'models' => ['Hellcat PRO']],
'951-0051' => ['compatible_accessories' => ['912-0030', '912-0078'], 'brand' => 'Sig Sauer', 'models' => ['P365 XL']],
'951-0064' => ['compatible_accessories' => ['912-0030', '912-0078'], 'brand' => 'Sig Sauer', 'models' => ['P365 XL']],
'951-0075' => ['compatible_accessories' => ['912-0045'], 'brand' => 'Ruger', 'models' => ['MAX9']],
'951-0076' => ['compatible_accessories' => ['912-0030', '912-0078'], 'brand' => 'Sig Sauer', 'models' => ['P365 XL']],
'951-0088' => ['compatible_accessories' => ['912-0071'], 'brand' => 'Ruger', 'models' => ['LCP MAX']],
'951-0083' => ['compatible_accessories' => ['930-0015'], 'brand' => '', 'models' => ['']]
];
// IDs for the categories "Lasers & Lights" and "Holsters"
const LASERS_LIGHTS_CATEGORY_ID = 3; // ID real de la categoría "Lasers & Lights"
const HOLSTERS_CATEGORY_ID = 2; // ID real de la categoría "Holsters"
public function __construct(
ManagerInterface $messageManager,
SessionManagerInterface $sessionManager
) {
$this->messageManager = $messageManager;
$this->sessionManager = $sessionManager;
}
public function beforeAddProduct(Cart $subject, $productInfo, $requestInfo = null)
{
// Get products that are already in the cart
$items = $subject->getQuote()->getAllItems();
$productToAddSku = $productInfo->getSku();
$categoryToAdd = $this->getProductCategory($productInfo);
// Only proceed if the product belongs to one of the categories we are monitoring
if ($categoryToAdd == self::LASERS_LIGHTS_CATEGORY_ID || $categoryToAdd == self::HOLSTERS_CATEGORY_ID) {
foreach ($items as $item) {
$skuInCart = $item->getSku();
$categoryInCart = $this->getProductCategory($item->getProduct());
// Check if the categories are different and if they are incompatible according to our array
if ($categoryToAdd !== $categoryInCart && !$this->isCompatible($skuInCart, $productToAddSku)) {
// Guardamos el mensaje de advertencia en la sesión
$warningMessage = __("El producto %1 no es compatible con el producto %2 en tu carrito.", $productToAddSku, $skuInCart);
$this->sessionManager->setCompatibilityWarning($warningMessage);
}
}
}
// IMPORTANT: Make sure to return the arguments
return [$productInfo, $requestInfo];
}
protected function isCompatible($sku1, $sku2)
{
// Check if product 1 has compatible accessories in the compatibility array
if (isset($this->compatibilities[$sku1]) && in_array($sku2, $this->compatibilities[$sku1]['compatible_accessories'])) {
return true;
}
return false;
}
protected function getProductCategory($product)
{
// Get the first category of the product
$categoryIds = $product->getCategoryIds();
if (!empty($categoryIds)) {
return $categoryIds[0];
}
return null;
}
}
是的,绝对可以使用插件根据某些条件在 Magento 2 中显示 HTML 警告消息。您可以通过利用自定义插件(也称为拦截器)来修改 Magento 类或方法的输出或行为来实现此目的。