简体   繁体   English

Magento 2 如何在可配置产品上显示简单的产品库存?

[英]Magento 2 How to display simple product stock on on a configurable product?

This is not a question but a possible solution it's an answer on how to show simple product stock on a configurable product这不是一个问题,而是一个可能的解决方案,它是关于如何在可配置产品上显示简单产品库存的答案

I was search for this solution and noticed that a lot of people have asked this similar question and most of the answers were vague or the solutions were for magento 1 but that was until I stumble across this [solution][1] which was not for my exact problem but it was doing what I want just not for stock but SKUs,so I made a few adjustments and came up with the solution below for stock我正在寻找这个解决方案,注意到很多人都问过这个类似的问题,而且大多数答案都含糊不清,或者解决方案是针对 magento 1 的,但直到我偶然发现这个 [solution][1] 才不是我的确切问题,但它正在做我想做的事情,而不是库存,而是 SKU,所以我做了一些调整,并提出了以下库存解决方案

A drop-down list is the attribute used on the configurable product下拉列表是可配置产品上使用的属性

Create a plugin创建一个插件

namespace Vendor\Module\Plugin;


class Configurable
{

    public function aftergetJsonConfig(
        \Magento\ConfigurableProduct\Block\Product\View\Type\Configurable $subject,
        $result
    ) {

        $jsonResult = json_decode($result, true);
        $jsonResult['stockqtys']=[];

        foreach ($subject->getAllowProducts() as $simpleProduct) {
            $jsonResult['stockqtys'][$simpleProduct->getId()] = $this->getProductStock($simpleProduct->getId());
        }

        $result = json_encode($jsonResult);

        return $result;
    }

   public function getProductStock($productId)
    {   
        $objectManager =  \Magento\Framework\App\ObjectManager::getInstance(); 
        $stockRegistry = $objectManager->get('\Magento\CatalogInventory\Api\StockRegistryInterface');
        return $stockRegistry->getStockItem($productId)->getQty();        
         
    }
}

Create etc\di.xml创建 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  disabled="false"  name="Vendor_Module_Plugin" sortOrder="10" type="Vendor\Module\Plugin\Configurable"/>
    </type>
    
</config>

view/frontend/web/js/stockswitch.js查看/前端/web/js/stockswitch.js

define([
    'jquery',
    'mage/utils/wrapper'
], function ($, wrapper) {
    'use strict';

    return function(targetModule){

        var reloadPrice = targetModule.prototype._reloadPrice;
        var reloadPriceWrapper = wrapper.wrap(reloadPrice, function(original){

            var result = original();
            var productStock = this.options.spConfig.stockqtys[this.simpleProduct];
            
            $('div.product-info-main .stock .value').html("");

            if(productStock != '') {
                $('div.product-info-main .stock .value').html(productStock);
            }   
            
            return result;
        });

        targetModule.prototype._reloadPrice = reloadPriceWrapper;
        return targetModule;
    };
});

view/frontend/requirejs-config.js查看/前端/requirejs-config.js

var config = {
    config: {
        mixins: {
            'Magento_ConfigurableProduct/js/configurable': {
                'Vendor_Module/js/stockswitch': true
            }
        }
    }
};

Override the default.phtml template and add the lines below where you want your stock to appear Vendor/Module/Magento_Catalog/templates/product/view/type/default.phtml覆盖 default.phtml 模板并在下面添加您希望库存出现的行Vendor/Module/Magento_Catalog/templates/product/view/type/default.phtml

<div class="stock actual-qty"> <span class="value" > </span> </div> 

Please let me know on any improvements in the comments请让我知道评论中的任何改进

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM