Magento2缺货相关产品未显示在可配置产品的下拉选项中

时间:2018-08-07 12:33:04

标签: magento magento2 stock configurable-product inventory-management

我面临Magento2的问题,我不确定这是否是错误。我想在“可配置产品”下拉列表中显示“缺货相关产品”。

在管理设置中,“商店”->“配置”->“目录”->“库存”->“显示缺货”,该商品已设置为“是”,但没有运气。

我尝试了几个带有集成插件的模块,并对此进行了很多研究,但找不到任何解决方案。

让我们举个例子-我有一个可配置产品,其子产品的尺寸为-S,M,L,例如L缺货。因此,目前我可以在我的可配置产品的下拉选项中看到S和M。

我想看到所有3个带有“ Out of Stock”标记的儿童产品,例如L-Out of Stock或类似的东西。

2 个答案:

答案 0 :(得分:1)

对不起,您的答复很晚。我真的很忙于其他项目。

为此,我使用了1个模块,并根据我的要求进行了定制。我为您提供了所有文件和文件夹,因此需要此文件的任何人都可以上传文件。

创建了一个模块-Jworks / ConfigurableProduct,因此文件夹结构将为/ app / code / Jworks / ConfigurableProduct

创建一个文件app / code / Jworks / ConfigurableProduct / registration.php并输入以下代码-

<?php
/**
 *
 * @category Jworks
 * @package ConfigurableProduct
 * @author Jitheesh V O <jitheesh@digitalboutique.co.uk>
 * @copyright Copyright (c) 2018 Digital Boutique (http://www.digitalboutique.co.uk/)
 */
\Magento\Framework\Component\ComponentRegistrar::register(
    \Magento\Framework\Component\ComponentRegistrar::MODULE,
    'Jworks_ConfigurableProduct',
    __DIR__
);

创建文件app / code / Jworks / ConfigurableProduct / composer.json并输入以下代码-

{
  "name": "jworks/module-configurableproduct",
  "description": "Jworks configurableproduct updates",
  "require": {
    "magento/module-catalog": "101.0.*"
  },
  "type": "magento2-module",
  "version": "1.0.0",
  "autoload": {
    "files": [
      "registration.php"
    ],
    "psr-4": {
      "Jworks\\ConfigurableProduct\\": ""
    }
  }
}

创建一个文件app / code / Jworks / ConfigurableProduct / etc / module.xml并输入以下代码-

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
    <module name="Jworks_ConfigurableProduct" setup_version="0.0.1">
        <sequence>
            <module name="Magento_ConfigurableProduct" />
        </sequence>
    </module>
</config>

创建文件app / code / Jworks / ConfigurableProduct / 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">
 <preference for="Magento\ConfigurableProduct\Model\ConfigurableAttributeData" type="Jworks\ConfigurableProduct\Model\Rewrite\ConfigurableAttributeData" />
</config>

创建一个文件app / code / Jworks / ConfigurableProduct / 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">
    <type name="Magento\ConfigurableProduct\Model\ResourceModel\Attribute\OptionSelectBuilderInterface">
        <plugin name="Magento_ConfigurableProduct_Plugin_Model_ResourceModel_Attribute_InStockOptionSelectBuilder" type="Jworks\ConfigurableProduct\Plugin\Model\ResourceModel\Attribute\InStockOptionSelectBuilder"/>
    </type>

    <type name="Magento\ConfigurableProduct\Model\AttributeOptionProvider">
        <plugin name="Magento_ConfigurableProduct_Plugin_Model_AttributeOptionProvider" type="Jworks\ConfigurableProduct\Plugin\Model\AttributeOptionProvider"/>
    </type>

    <type name="Magento\ConfigurableProduct\Block\Product\View\Type\Configurable">
    <plugin name="changeAllowProductsBehaviour" type="Jworks\ConfigurableProduct\Model\ConfigurableProduct\Block\Product\View\Type\Configurable\Plugin" sortOrder="10" />
    </type>
</config>

创建一个文件app / code / Jworks / ConfigurableProduct / Plugin / Model / AttributeOptionProvider.php并输入以下代码-

<?php


namespace Jworks\ConfigurableProduct\Plugin\Model;
class AttributeOptionProvider
{

    public function afterGetAttributeOptions(\Magento\ConfigurableProduct\Model\AttributeOptionProvider $subject, array $result)
    {
        $optiondata=array();
        foreach ($result as $option) {  

            if(isset($option['stock_status']) && $option['stock_status']==0){
                $option['option_title']  = $option['option_title'].__(' - uitverkocht');
            }
            $optiondata[]=$option;
        }
        return $optiondata;
    }
}

创建文件app / code / Jworks / ConfigurableProduct / Plugin / Model / ResourceModel / Attribute / InStockOptionSelectBuilder.php并输入以下代码-

<?php

namespace Jworks\ConfigurableProduct\Plugin\Model\ResourceModel\Attribute;

use Magento\CatalogInventory\Model\ResourceModel\Stock\Status;
use Magento\ConfigurableProduct\Model\ResourceModel\Attribute\OptionSelectBuilderInterface;
use Magento\Framework\DB\Select;

/**
 * Plugin for OptionSelectBuilderInterface to add stock status filter.
 */
class InStockOptionSelectBuilder
{
    /**
     * CatalogInventory Stock Status Resource Model.
     *
     * @var Status
     */
    private $stockStatusResource;

    /**
     * @param Status $stockStatusResource
     */
    public function __construct(Status $stockStatusResource)
    {
        $this->stockStatusResource = $stockStatusResource;
    }

    /**
     * Add stock status filter to select.
     *
     * @param OptionSelectBuilderInterface $subject
     * @param Select $select
     * @return Select
     *
     * @SuppressWarnings(PHPMD.UnusedFormalParameter)
     */
    public function afterGetSelect(OptionSelectBuilderInterface $subject, Select $select)
    {
        $select->joinInner(
            ['stock' => $this->stockStatusResource->getMainTable()],
            'stock.product_id = entity.entity_id',
            ['stock.stock_status']
        );

        return $select;
    }
}

创建文件app / code / Jworks / ConfigurableProduct / Model / Rewrite / ConfigurableAttributeData.php并输入以下代码-

<?php
/**
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */

namespace Magento\ConfigurableProduct\Model;

use Magento\Catalog\Model\Product;
use Magento\ConfigurableProduct\Model\Product\Type\Configurable\Attribute;

/**
 * Class ConfigurableAttributeData
 * @api
 * @since 100.0.2
 */
class ConfigurableAttributeData
{
    /**
     * Get product attributes
     *
     * @param Product $product
     * @param array $options
     * @return array
     */
    public function getAttributesData(Product $product, array $options = [])
    {
        $defaultValues = [];
        $attributes = [];
        foreach ($product->getTypeInstance()->getConfigurableAttributes($product) as $attribute) {
            $attributeOptionsData = $this->getAttributeOptionsData($attribute, $options);
            if ($attributeOptionsData) {
                $productAttribute = $attribute->getProductAttribute();
                $attributeId = $productAttribute->getId();
                $attributes[$attributeId] = [
                    'id' => $attributeId,
                    'code' => $productAttribute->getAttributeCode(),
                    'label' => $productAttribute->getStoreLabel($product->getStoreId()),
                    'options' => $attributeOptionsData,
                    'position' => $attribute->getPosition(),
                ];
                $defaultValues[$attributeId] = $this->getAttributeConfigValue($attributeId, $product);
            }
        }
        return [
            'attributes' => $attributes,
            'defaultValues' => $defaultValues,
        ];
    }

    /**
     * @param Attribute $attribute
     * @param array $config
     * @return array
     */
    protected function getAttributeOptionsData($attribute, $config)
    {
        $attributeOptionsData = [];
        foreach ($attribute->getOptions() as $attributeOption) {
            $optionId = $attributeOption['value_index'];
            $attributeOptionsData[] = [
                'id' => $optionId,
                'label' => $attributeOption['label'],
                //'test' => $config[$attribute->getAttributeId()][$optionId],
                'products' => isset($config[$attribute->getAttributeId()][$optionId])
                    ? $config[$attribute->getAttributeId()][$optionId]
                    : [],
            ];
        }
        return $attributeOptionsData;
    }

    /**
     * @param int $attributeId
     * @param Product $product
     * @return mixed|null
     */
    protected function getAttributeConfigValue($attributeId, $product)
    {
        return $product->hasPreconfiguredValues()
            ? $product->getPreconfiguredValues()->getData('super_attribute/' . $attributeId)
            : null;
    }
}

创建一个文件app / code / Jworks / ConfigurableProduct / Model / ConfigurableProduct / Block / Product / View / Type / Configurable / Plugin.php并输入以下代码-

<?php
namespace Jworks\ConfigurableProduct\Model\ConfigurableProduct\Block\Product\View\Type\Configurable;

class Plugin
{
    /**
     * getAllowProducts
     *
     * @param \Magento\ConfigurableProduct\Block\Product\View\Type\Configurable $subject
     *
     * @return array
     */
    public function beforeGetAllowProducts($subject)
    {
        if (!$subject->hasData('allow_products')) {
            $products = [];
            $allProducts = $subject->getProduct()->getTypeInstance()->getUsedProducts($subject->getProduct(), null);
            foreach ($allProducts as $product) {
                    $products[] = $product;
            }
            $subject->setData('allow_products', $products);
        }

        return [];
    }

}

然后,您可以运行所有用于安装Magento2扩展程序的命令。

我希望任何人都能得到帮助。

答案 1 :(得分:0)

打开文件vendor \ magento \ module-catalog \ Helper \ Product.php

更改

protected $_skipSaleableCheck = false;

protected $_skipSaleableCheck = true;