Saltar al contenido

Magento 2.2 Cómo mostrar fuera de stock en producto configurable

Basta ya de investigar por todo internet ya que estás al sitio indicado, poseemos la respuesta que buscas pero sin complicarte.

Solución:

aplicación / código / Ravindra / Personalizado / etc / module.xml



    
    

aplicación / código / Ravindra / Custom / registration.php

aplicación / código / Ravindra / Personalizado / etc / di.xml





    

    

aplicación / código / Ravindra / Personalizado / Bloque / Producto / Vista / Tipo / Configurable.php

swatchHelper = $swatchHelper;
        $this->swatchMediaHelper = $swatchMediaHelper;
         parent::__construct(
            $context,
            $arrayUtils,
            $jsonEncoder,
            $helper,
            $catalogProduct,
            $currentCustomer,
            $priceCurrency,
            $configurableAttributeData,
            $swatchHelper,
            $swatchMediaHelper,
            $data
        );
    

    public function getJsonConfig() 
        $store = $this->getCurrentStore();
        $currentProduct = $this->getProduct();

        $regularPrice = $currentProduct->getPriceInfo()->getPrice('regular_price');
        $finalPrice = $currentProduct->getPriceInfo()->getPrice('final_price');

        $options = $this->helper->getOptions($currentProduct, $this->getAllowProducts());
        $attributesData = $this->configurableAttributeData->getAttributesData($currentProduct, $options);

        $config = [
            'attributes' => $attributesData['attributes'],
            'template' => str_replace('%s', '<%- data.price %>', $store->getCurrentCurrency()->getOutputFormat()),
            'optionPrices' => $this->getOptionPrices(),
            'optionStock' => $this->getOptionStocks(),
            'prices' => [
                'oldPrice' => [
                    'amount' => $this->_registerJsPrice($regularPrice->getAmount()->getValue()),
                ],
                'basePrice' => [
                    'amount' => $this->_registerJsPrice(
                        $finalPrice->getAmount()->getBaseAmount()
                    ),
                ],
                'finalPrice' => [
                    'amount' => $this->_registerJsPrice($finalPrice->getAmount()->getValue()),

                ],
            ],

            'productId' => $currentProduct->getId(),
            'chooseText' => __('Choose an Option...'),
            'images' => isset($options['images']) ? $options['images'] : [],
            'index' => isset($options['index']) ? $options['index'] : [],
        ];

        if ($currentProduct->hasPreconfiguredValues() && !empty($attributesData['defaultValues'])) 
            $config['defaultValues'] = $attributesData['defaultValues'];
        

        $config = array_merge($config, $this->_getAdditionalConfig());

        return $this->jsonEncoder->encode($config);
    


    /*-----------------------custom code----------------------*/

    protected function getOptionStocks() 
        $stocks = [];
        $objectManager = MagentoFrameworkAppObjectManager::getInstance();       
        $stockObject=$objectManager->get('MagentoCatalogInventoryApiStockRegistryInterface');

        foreach ($this->getAllowProducts() as $product)           
            $productStockObj = $stockObject->getStockItem($product->getId());
            $isInStock = $productStockObj['is_in_stock'];
            $stocks[$product->getId()] = ['stockStatus' => $isInStock];
        
        return $stocks;
    


aplicación / código / Ravindra / Custom / Block / ConfigurableProduct.php

_storeManager = $context->getStoreManager();
    


aplicación / código / Ravindra / Personalizado / Modelo / Reescribir / ConfigurableAttributeData.php

eavConfig = $eavConfig;
    

    public function getAttributeOptionsData($attribute, $config) 
        $attributeOptionsData = [];

        $attributeId = $attribute->getAttributeId();

        $objectManager = MagentoFrameworkAppObjectManager::getInstance();
        $eavModel = $objectManager->create('MagentoCatalogModelResourceModelEavAttribute');
        $attr = $eavModel->load($attributeId);
        $attributeCode=$eavModel->getAttributeCode();


        $attribute = $this->eavConfig->getAttribute('catalog_product', $attributeCode);
        $options = $attribute->getSource()->getAllOptions();


        foreach ($options as $attributeOption) 
            $optionId = $attributeOption['value'];
            $attributeOptionsData[] = [
                'id' => $optionId,
                'label' => $attributeOption['label'],
                'products' => isset($config[$attribute->getAttributeId()][$optionId])
                    ? $config[$attribute->getAttributeId()][$optionId]
                    : [],
            ];
        
        return $attributeOptionsData;
    


app / code / Ravindra / Custom / view / frontend / layout / catalog_product_view.xml




            
        
        

app / code / Ravindra / Custom / view / frontend / layout / catalog_product_view_type_configurable.xml




    
       
            
                Ravindra_Custom::configurable-available.phtml
                       
               
    

aplicación / código / Ravindra / Custom / view / frontend / templates / configurable-available.phtml

   
getProduct() ?>
getLayout()->createBlock('RavindraCustomBlockConfigurableProduct'); ?>

displayProductStockStatus()): ?>
    isAvailable()): ?>
        

app / code / Ravindra / Custom / view / frontend / web / css / configurableproduct.css

.email-stockalert-input
    float:left;
    width: 80%;

.submit-stockalert-input 
    width: 20%;
    float: right;

.email-stockalert-input input, .submit-stockalert-input input
    height: 40px;

.submit-stockalert-input input
    float: left;
    width: 100%;
    background: #1979c3;
    border: 1px solid #1979c3;
    color: #ffffff;
    cursor: pointer;
    display: inline-block;
    font-family: 'Open Sans', 'Helvetica Neue', Helvetica, Arial, sans-serif;
    font-weight: 600;
    font-size: 14px;
    text-transform: capitalize;

.stockalert-submit-btn:hover
    background: #333;
    border: 1px solid #333;

.ravi-stockalert, .ravi-stockalert-bundle, .ravi-stockalert-group
    /*text-align: center !important;*/
    margin: 10px 0px;
    border: 1px #ccc solid;
    padding: 10px;

.product-info-stock-sku
    display:block !important;

.product-info-price
    /*width: 55% !important;*/

.product-info-main .product-info-price .price-box
    width: 100%;
    float: left;

.product-info-main .product-info-stock-sku
    float: left;
    min-width: 100%;
    padding-left: 0px;

.stock.unavailable
    text-align:left;
    color:#F00;
    font-size: 16px;
    font-weight: 400 !important;

.sku .value, .sku
    text-align:left;

.ravi-stockalert-form 
    margin: 5px 0;
    width: 100%;



.page-product-grouped .product-info-price
    width:100%;

.table.grouped .col.item 
   width: 13%;

.grouped .email-stockalert-input
    width: 69%;

.grouped .submit-stockalert-input
    width: 31%

.grouped .submit-stockalert-input input
    font-size: 12px;

.grouped .table.grouped .col.qty
    padding: 11px 0px;

.item span, .item strong
    color: #1979C3;

.grouped .product-item-name 
    color: #000 !important;
    font-size: 16px;

.rav-subscriber-title 
    text-align: left;

.guest_users_message
    text-align:left;

aplicación / código / Ravindra / Custom / view / frontend / requirejs-config.js

var config = 
    map: 
        '*': 

            'Magento_Swatches/js/swatch-renderer':'Ravindra_Custom/js/swatch-renderer', 
            configurable:'Ravindra_Custom/js/configurable',

        
    
;

Te mostramos las comentarios y valoraciones de los lectores

¡Haz clic para puntuar esta entrada!
(Votos: 2 Promedio: 3.5)



Utiliza Nuestro Buscador

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *