且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

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

更新时间:2023-11-30 12:42:22

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

sorry for the late reply. I was really busy with different projects.

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

So for this, i used 1 module and did customized that based on my requirements. I am providing you all the files and folders so anyone who needs this, can upload the files.

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

Created a module - Jworks/ConfigurableProduct so the folder structure will be /app/code/Jworks/ConfigurableProduct

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

Create a file app/code/Jworks/ConfigurableProduct/registration.php and enter below code -

<?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并输入以下代码-

Create a file app/code/Jworks/ConfigurableProduct/composer.json and enter below code -

{
  "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并输入以下代码-

Create a file app/code/Jworks/ConfigurableProduct/etc/module.xml and enter below code -

<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并输入以下代码-

Create a file app/code/Jworks/ConfigurableProduct/etc/di.xml and enter below code -

<?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并输入以下代码-

Create a file app/code/Jworks/ConfigurableProduct/etc/frontend/di.xml and enter below code -

<?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并输入以下代码-

Create a file app/code/Jworks/ConfigurableProduct/Plugin/Model/AttributeOptionProvider.php and enter below code -

<?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并输入以下代码-

Create a file app/code/Jworks/ConfigurableProduct/Plugin/Model/ResourceModel/Attribute/InStockOptionSelectBuilder.php and enter below code -

<?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并输入以下代码-

Create a file app/code/Jworks/ConfigurableProduct/Model/Rewrite/ConfigurableAttributeData.php and enter below code -

<?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并输入以下代码-

Create a file app/code/Jworks/ConfigurableProduct/Model/ConfigurableProduct/Block/Product/View/Type/Configurable/Plugin.php and enter below code -

<?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扩展程序的命令.

And after all these, you can run all the commands for installing the Magento2 extension.

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

I hope anybody can get help with this.