Magento2缺货相关产品未显示在可配置产品的下拉选项中 [英] Magento2 Out of Stock Assosiated Products not showing in the dropdown options for Configurable Products
问题描述
我面临Magento2的问题,我不确定这是否是错误.我想在可配置产品"下拉列表中显示缺货相关产品".
I am facing an issue with Magento2 and I am not sure whether it's a bug or not. I want to show Out of Stock Assosiated Products in the Configurable Product Dropdown.
在管理设置中,商店"->配置"->目录"->库存"->显示缺货",该商品已设置为是",但没有运气.
In the admin settings, Stores -> Configuration -> Catalog -> Inventory -> Display out of stock products, this is already set to "Yes" but no luck.
我已经尝试了几个具有集成插件的模块,并对此进行了很多研究,但是找不到任何解决方案.
I have tried several modules with integrating plugins and did a lot of research on this but could not find any solution for this.
让我们举个例子-我有一个可配置产品,其子产品的尺寸为-S,M,L,例如L缺货.因此,当前我可以在我的可配置产品的下拉选项中看到S和M.
Lets take an example - I have a Configurable Product with Child products Size - S , M , L and for instance L is out of stock. So currently I am able to see S and M in the dropdown options for my configurable product.
我希望看到所有3个带有"Out of Stock"标记的儿童产品,例如L-Out of Stock或类似的东西.
I want to see all the 3 Child products with the mark of "Out of Stock" like L - Out of Stock or something like that.
推荐答案
对不起,您的答复很晚.我真的很忙于其他项目.
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.
这篇关于Magento2缺货相关产品未显示在可配置产品的下拉选项中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!