【问题标题】:Magento 2 How to display simple product stock on on a configurable product?Magento 2 如何在可配置产品上显示简单的产品库存?
【发布时间】:2020-05-27 19:55:12
【问题描述】:

这不是一个问题,而是一个可能的解决方案 这是关于如何在可配置产品上显示简单产品库存的答案

我正在寻找这个解决方案,并注意到很多人都问过这个类似的问题,而且大多数答案都很模糊,或者解决方案是针对 magento 1 的,但直到我偶然发现这个 [解决方案][1]不是因为我的确切问题,但它正在做我想要的不是库存而是 SKU,所以我做了一些调整并提出了以下库存解决方案

【问题讨论】:

    标签: php magento plugins magento2 stock


    【解决方案1】:

    下拉列表是可配置产品上使用的属性

    创建插件

    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();        
             
        }
    }
    

    创建 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>
    

    查看/前端/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;
        };
    });
    

    查看/前端/requirejs-config.js

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

    覆盖 default.phtml 模板并在下面添加您希望您的股票出现的行 供应商/模块/Magento_Catalog/templates/product/view/type/default.phtml

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

    请让我知道 cmets 的任何改进

    【讨论】:

      猜你喜欢
      • 2013-01-29
      • 1970-01-01
      • 2016-01-24
      • 2021-12-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多