获取可配置产品属性后获取简单产品属性

magento 收藏
0 39
app/code/[Vendor]/[Module]/etc/di.xml

<?xml version="1.0"?>
    <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
        <type name="Magento\ConfigurableProduct\Block\Product\View\Type\Configurable">
            <plugin name="[Vendor]_configurable_product_configurable" type="[Vendor]\[Module]\Plugin\Block\ConfigurableProduct\Product\View\Type\Configurable" sortOrder="1"/>
        
app/code/[Vendor]/[Module]/Plugin/Block/ConfigurableProduct/Product/View/Type/configurable.php


<?php
namespace [Vendor]\[Module]\Plugin\Block\ConfigurableProduct\Product\View\Type;

use Magento\Framework\Json\EncoderInterface;
use Magento\Framework\Json\DecoderInterface;

class Configurable
{
    /**
     * @var EncoderInterface
     */
    protected $jsonEncoder;

    /**
     * @var DecoderInterface
     */
    protected $jsonDecoder;

    /**
     * @var \Magento\CatalogInventory\Api\StockRegistryInterface
     */
    protected $stockRegistry;

    /**
     * Configurable constructor.
     *
     * @param EncoderInterface $jsonEncoder
     * @param DecoderInterface $jsonDecoder
     * @param \Magento\CatalogInventory\Api\StockRegistryInterface $stockRegistry
     */
    public function __construct(
        EncoderInterface $jsonEncoder,
        DecoderInterface $jsonDecoder,
        \Magento\CatalogInventory\Api\StockRegistryInterface $stockRegistry
    ) {
        $this->jsonDecoder = $jsonDecoder;
        $this->jsonEncoder = $jsonEncoder;;
        $this->stockRegistry = $stockRegistry;
    }

    public function aroundGetJsonConfig(
        \Magento\ConfigurableProduct\Block\Product\View\Type\Configurable $subject,
        \Closure $proceed
    ) {
        $config = $proceed();
        $config = $this->jsonDecoder->decode($config);
        $productsCollection = $subject->getAllowProducts();
        $stockInfo = array();
        foreach ($productsCollection as $product) {
            $productId = $product->getId();
            $stockItem = $this->stockRegistry->getStockItem($product->getId());
            if ($stockItem->getQty() <= 0 || !($stockItem->getIsInStock())) {
                $stockInfo[$productId] = array(
                    "stockLabel" => __('Out of stock'),
                    "stockQty" => intval($stockItem->getQty()),
                    "is_in_stock" => false
                );
            } else {
                $stockInfo[$productId] = array(
                    "stockLabel" => __('In Stock'),
                    "stockQty" => intval($stockItem->getQty()),
                    "is_in_stock" => true
                );
            }
        }

        $config['stockInfo'] = $stockInfo;
        return $this->jsonEncoder->encode($config);
    }
}
上面configurable添加的属性stockInfo可在Magento_ConfigurableProduct/templates/product/view/type/options/configurable.phtml中通过$block->getJsonConfig()查看或者直接页面中js的spconfig查看

    暂时没有人评论