Skip to content

Catalog Search Product Status Stock

I am having grouped product on my projects
When I go the category from the category bar
it shows the correct stock status.
but When I go with the catalog search it always shows the out of stock for the all products .

Here is my template
I checked when I checked the hasstock in the PLP it gives me the 1 but when I Checked in the catalog search page it returns me the empty.

For some purposes I have disabled the all inventory related modules in the Magento.

Can anyone give me a quick solution

    <?php
/**
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */
use MagentoFrameworkAppActionAction;
// @codingStandardsIgnoreFile

?>


<?php
/**
 * Product list template
 *
 * @var $block MagentoCatalogBlockProductListProduct
 */
?>
<?php
$_productCollection = $block->getLoadedProductCollection();
$_helper = $this->helper('MagentoCatalogHelperOutput');
$objectManager = MagentoFrameworkAppObjectManager::getInstance();
$customerSession = $objectManager->create('MagentoCustomerModelSession');
//$StockState = $objectManager->get('MagentoInventorySalesAdminUiModelGetSalableQuantityDataBySku');



?>
<style>
    .toolbar-html-container-top .filter-product__pagination, .toolbar-bottom > div > div > h2, .toolbar-bottom .sorter-options, .toolbar-bottom .hover-effect{
        display: none;
    }
    .filter-product__pagination { border: 0;}
</style>
<?php if (!$_productCollection->count()): ?>
    <div class="message info empty"><div><?= /* @escapeNotVerified */ __('We can't find products matching the selection.') ?></div></div>
<?php else: ?>
    <div class="container-catalog-products-common">
        <?php
        if ($block->getMode() == 'grid') {
            $viewMode = 'grid';
            $image = 'category_page_grid';
            $showDescription = false;
            $templateType = MagentoCatalogBlockProductReviewRendererInterface::SHORT_VIEW;
            $col_grid = ' category_page_grid_3';
        } else {
            $viewMode = 'list';
            $image = 'category_page_list';
            $showDescription = true;
            $templateType = MagentoCatalogBlockProductReviewRendererInterface::FULL_VIEW;
            $col_grid = '';
        }
        /**
         * Position for actions regarding image size changing in vde if needed
         */
        $pos = $block->getPositioned();
        ?>
        <div class="toolbar-html-container-top check-view-mode-toolbar-<?php echo $viewMode;?>" data-mode="<?php echo $viewMode;?>">
            <?= $block->getToolbarHtml() ?>
        </div>
        <?= $block->getAdditionalHtml() ?>
        <div class="products wrapper filter-product <?= /* @escapeNotVerified */ $viewMode ?> products-<?= /* @escapeNotVerified */ $viewMode ?>">
            <?php $iterator = 1; ?>
            <div class="row">
                <?php /*<ol class="container-products-switch products list items product-items <?php echo $col_grid;?>" data-view-mode="4"> */ ?>
                <?php /** @var $_product MagentoCatalogModelProduct */ ?>
                <?php foreach ($_productCollection as $_product): ?>
                    <?= /* @escapeNotVerified */ ($iterator++ == 1) ? '<div class="item product-item col-lg-4 col-sm-6">' : '</div><div class="item product-item col-lg-4 col-sm-6">' ?>
                    <div class="product-item-info" data-container="product-<?= /* @escapeNotVerified */ $viewMode ?>">
                        <div class="item-inner">
                            <?php
                            $productImage = $block->getImage($_product, $image);
                            if ($pos != null) {
                                $position = ' style="left:' . $productImage->getWidth() . 'px;'
                                    . 'top:' . $productImage->getHeight() . 'px;"';
                            }
                            ?>
                            <?php // Product Image ?>
                            <div class="product-photo product-card">
                                <a href="<?= /* @escapeNotVerified */ $_product->getProductUrl() ?>" class="product-item-photo product-card__img" tabindex="-1">
                                    <?php $productImageThumb = $block->getImage($_product, 'category_page_grid-1');?>
                                    <!--<span class="image0 image-switch">
                                            <?php //echo $productImage->toHtml(); ?>
                                        </span>-->
                                    <span class="image12 image-switch">
                                            <?php echo $productImageThumb->toHtml(); ?>
                                        </span>
                                </a>
                                <?php /* if ($block->getMode() == 'grid') { ?>
                                <div class="addto-links-icons visible-onhover">
                                    <div class="action-inner">
                                        <ul class="add-to-links">
                                            <?php if ($this->helper('RokanthemesQuickViewHelperData')->isEnable()){ ?>
                                            <li class="quickview-product">
                                                <a href="javascript:void(0)" data-role="quickview-button" data-id="<?php echo $_product->getId(); ?>" data-href="<?php echo $block->getUrl('quickview/product/quickview/id/' . $_product->getId()) ?>" title="<?php echo $block->escapeHtml(__('Quick view')); ?>" class="ajax diamond tooltip-hover" data-placement="top" data-original-title="<?php echo ('Quick view') ?>">
                                                    <i class="icon-eye1"></i>
                                                </a>
                                            </li>
                                            <?php } ?>

                                            <li>
                                                <?php
                                                $compareHelper = $this->helper('MagentoCatalogHelperProductCompare');
                                                ?>
                                                <a href="#"
                                                   class="action tocompare"
                                                   title="<?php echo $block->escapeHtml(__('Add to Compare')); ?>"
                                                   aria-label="<?php echo $block->escapeHtml(__('Add to Compare')); ?>"
                                                   data-post='<?php /* @escapeNotVerified */ /*echo $compareHelper->getPostDataParams($_product); ?>'
                                                   role="button">
                                                    <i class="icon-stack"></i>
                                                </a>
                                            </li>

                                            <li>
                                                <?php if ($this->helper('MagentoWishlistHelperData')->isAllow()): ?>
                                                    <a href="#" class="action towishlist" title="<?php echo $block->escapeHtml(__('Add to Wish List')); ?>" aria-label="<?php echo $block->escapeHtml(__('Add to Wish List')); ?>" data-post='<?php /* @escapeNotVerified */ /*echo $block->getAddToWishlistParams($_product); ?>' data-action="add-to-wishlist" role="button">
                                                        <i class="icon-heart"></i>
                                                    </a>
                                                <?php endif; ?>
                                            </li>

                                            <li>
                                                <div class="actions-add-cart">
                                                    <div class="product actions product-item-actions action-inner"<?= strpos($pos, $viewMode . '-actions') ? $position : '' ?>>
                                                        <div class="actions-primary"<?= strpos($pos, $viewMode . '-primary') ? $position : '' ?>>
                                                            <?php if ($_product->isSaleable()): ?>
                                                                <?php $postParams = $block->getAddToCartPostParams($_product); ?>
                                                                <form data-role="tocart-form" data-product-sku="<?= $block->escapeHtml($_product->getSku()) ?>" action="<?= /* @NoEscape */ /*$postParams['action'] ?>" method="post">
                                                                    <input type="hidden" name="product" value="<?= /* @escapeNotVerified */ /*$postParams['data']['product'] ?>">
                                                                    <input type="hidden" name="<?= /* @escapeNotVerified */ /*Action::PARAM_NAME_URL_ENCODED ?>" value="<?= /* @escapeNotVerified */ /*$postParams['data'][Action::PARAM_NAME_URL_ENCODED] ?>">
                                                                    <?= $block->getBlockHtml('formkey') ?>
                                                                    <button type="submit"
                                                                            title="<?= $block->escapeHtml(__('Add to Cart')) ?>"
                                                                            class="action tocart primary">
                                                                        <i class="icon-shopping-bag"></i>
                                                                    </button>
                                                                </form>
                                                            <?php  else: ?>
                                                                <?php //if ($_product->isAvailable()): ?>
                                                                    <!--<div class="stock available"><span><? //= /* @escapeNotVerified */ /* __('In stock') ?></span></div>
                                                                <?php //else: ?>
                                                                    <div class="stock unavailable"><span><? //= /* @escapeNotVerified */ /* __('Out of stock') ?></span></div>-->
                                                                <?php //endif; ?>
                                                            <?php endif; ?>
                                                        </div>
                                                    </div>
                                                </div>
                                            </li>

                                        </ul>
                                    </div>
                                </div>
                                <?php } */ ?>
                                <div class="product-label">
                                    <?php if ( $_product->getFinalPrice() < $_product->getPrice() ): ?>
                                        <span class="onsale">
                                            <span class="sale-bg"></span>
                                            <span class="sale-text"><?php echo __('Sale') ?></span>
                                        </span>
                                    <?php endif; ?>
                                    <?php $todayDate = time();
                                    $timeStartNew = strtotime($_product->getData('news_from_date') ?? '');
                                    $timeEndNew = strtotime($_product->getData('news_to_date') ?? ''); ?>
                                    <?php if ( $timeEndNew >= $todayDate && $timeStartNew <= $todayDate): ?>
                                        <span class="newlabel">
                                            <span><?php echo __('New') ?></span>
                                        </span>
                                    <?php endif; ?>
                                </div>
                                <div class="product-info product-card__info">
                                    <div class="details product-item-details">
                                        <div class="product-brand">
                                            <p>
                                                <?php
                                                $productBrand = $_product->getAttributeText("product_brand");
                                                if (is_array($productBrand)) {
                                                    // Handle the array, for example, by joining its elements into a string
                                                    $productBrand = implode(", ", $productBrand);
                                                }
                                                echo $productBrand;
                                                ?>
                                                <?php //print_r($_product->getAttributeText("product_brand")); //$_product->getAssociatedProducts()?>
                                            </p>
                                            <?php if($customerSession->isLoggedIn()): ?>
                                                <div class="stock-check">
                                                    <?php  $hasInStock = $block->hasInStockAssociatedProduct($_product->getId()); ?>
                                                    <?php print_r($hasInStock)?>;
                                                    <?php if ($hasInStock): ?>
                                                        <p class="stock available in-stock"><?= /* @escapeNotVerified */ __('In stock') ?></p>
                                                    <?php else: ?>
                                                        <p class="stock unavailable out-stock"><?= /* @escapeNotVerified */ __('Out of stock') ?></p>
                                                    <?php endif; ?>
                                                </div>
                                            <?php endif; ?>
                                        </div>
                                        <?php
                                        $_productNameStripped = $block->stripTags($_product->getName(), null, true);
                                        ?>
                                        <?= $block->getProductDetailsHtml($_product) ?>
                                        <strong class="name product-item-name">
                                            <a class="product-item-link"
                                               href="<?= /* @escapeNotVerified */ $_product->getProductUrl() ?>">
                                                <?= /* @escapeNotVerified */ $_helper->productAttribute($_product, $_product->getName(), 'name') ?>
                                            </a>
                                        </strong>

                                        <?php
                                        $productColor = $_product->getAttributeText('color');
                                        ?>

                                        <?php if ($productColor): ?>
                                            <div class="product-color">
                                                Color: <?= $productColor ?>
                                            </div>
                                        <?php endif; ?>




                                        <?= $block->getReviewsSummaryHtml($_product, $templateType) ?>

                                        <?= /* @escapeNotVerified */ $block->getProductPrice($_product) ?>

                                        <?php if ($showDescription):?>
                                            <div class="product description product-item-description">
                                                <?= /* @escapeNotVerified */ $_helper->productAttribute($_product, $_product->getShortDescription(), 'short_description') ?>
                                                <a href="<?= /* @escapeNotVerified */ $_product->getProductUrl() ?>" title="<?= /* @escapeNotVerified */ $_productNameStripped ?>"
                                                   class="action more"><?= /* @escapeNotVerified */ __('Learn More') ?></a>
                                            </div>
                                        <?php endif; ?>
                                        <?php if ($block->getMode() == 'list') { ?>
                                            <div class="addto-links-icons visible-onhover">
                                                <div class="action-inner product-item-actions">
                                                    <div class="actions-primary"<?= strpos($pos, $viewMode . '-primary') ? $position : '' ?>>
                                                        <?php if ($_product->isSaleable()): ?>
                                                            <?php $postParams = $block->getAddToCartPostParams($_product); ?>
                                                            <form data-role="tocart-form" data-product-sku="<?= $block->escapeHtml($_product->getSku()) ?>" action="<?= /* @NoEscape */ $postParams['action'] ?>" method="post">
                                                                <input type="hidden" name="product" value="<?= /* @escapeNotVerified */ $postParams['data']['product'] ?>">
                                                                <input type="hidden" name="<?= /* @escapeNotVerified */ Action::PARAM_NAME_URL_ENCODED ?>" value="<?= /* @escapeNotVerified */ $postParams['data'][Action::PARAM_NAME_URL_ENCODED] ?>">
                                                                <?= $block->getBlockHtml('formkey') ?>
                                                                <button type="submit"
                                                                        title="<?= $block->escapeHtml(__('Add to Cart')) ?>"
                                                                        class="action tocart primary">
                                                                    <span>+ <?= /* @escapeNotVerified */ __('Add to Cart') ?></span>
                                                                </button>
                                                            </form>
                                                        <?php else: ?>
                                                            <?php if ($_product->isAvailable()): ?>
                                                                <div class="stock available"><span><?= /* @escapeNotVerified */ __('In stock') ?></span></div>
                                                            <?php else: ?>
                                                                <div class="stock unavailable"><span><?= /* @escapeNotVerified */ __('Out of stock') ?></span></div>
                                                            <?php endif; ?>
                                                        <?php endif; ?>
                                                    </div>
                                                    <div data-role="add-to-links" class="actions-secondary"<?php echo strpos($pos, $viewMode . '-secondary') ? $position : ''; ?>>
                                                        <?php if ($this->helper('MagentoWishlistHelperData')->isAllow()): ?>
                                                            <a href="#"
                                                               class="action towishlist"
                                                               title="<?php echo $block->escapeHtml(__('Add to Wish List')); ?>"
                                                               aria-label="<?php echo $block->escapeHtml(__('Add to Wish List')); ?>"
                                                               data-post='<?php /* @escapeNotVerified */ echo $block->getAddToWishlistParams($_product); ?>'
                                                               data-action="add-to-wishlist"
                                                               role="button">
                                                                <i class="icon-heart"></i></i>
                                                            </a>
                                                        <?php endif; ?>
                                                        <?php
                                                        $compareHelper = $this->helper('MagentoCatalogHelperProductCompare');
                                                        ?>
                                                        <a href="#"
                                                           class="action tocompare"
                                                           title="<?php echo $block->escapeHtml(__('Add to Compare')); ?>"
                                                           aria-label="<?php echo $block->escapeHtml(__('Add to Compare')); ?>"
                                                           data-post='<?php /* @escapeNotVerified */ echo $compareHelper->getPostDataParams($_product); ?>'
                                                           role="button">
                                                            <i class="icon-stack"></i>
                                                        </a>
                                                    </div>
                                                </div>
                                            </div>
                                        <?php } ?>
                                    </div>
                                </div>
                            </div>


                        </div>
                    </div>
                    <?= ($iterator == count($_productCollection)+1) ? '</div>' : '' ?>
                <?php endforeach; ?>
                <?php /* </ol> */ ?>
            </div>
            <div class="toolbar-bottom">
                <?= $block->getToolbarHtml() ?>
            </div>
        </div>

        <?php if (!$block->isRedirectToCartEnabled()) : ?>
            <script type="text/x-magento-init">
                {
                    "[data-role=tocart-form], .form.map.checkout": {
                        "catalogAddToCart": {
                            "product_sku": "<?= /* @NoEscape */ $_product->getSku() ?>"
                    }
                }
            }
            </script>
        <?php endif; ?>
    </div>
<?php endif; ?>

<!---->
<!--<script>-->
<!--    require(['jquery'], function($) {-->
<!--        $(document).ready(function() {-->
<!--            $('.plus-icon').click(function() {-->
<!--                // Toggle the visibility of the block-->
<!--                $('.sidebar').toggle('slide', { direction: 'left' });-->
<!--            });-->
<!--        });-->
<!--    });-->
<!--</script>-->