1

If a customer inserted a quantity that is larger than the quantity of selected store echo text below the input type [qty] that says "The requested qty is larger than the quantity on this selected store".

The below code get the total quantity

  public function getStockStatus()
    {
        $productId = $this->getCurrentProduct()->getId();
        /** @var StockItemInterface $stockItem */
        $stockItem = $this->stockRegistry->getStockItem($productId);
        $isInStock = $stockItem ? $stockItem->getIsInStock() : false;
        return $isInStock;
    }

I have Attributes [USA &NZD] that have same name as my store views and below code get the values on those attributes and show alert if the attribute have value of 0.

how to apply similar logic but if the customer insert larger qty and click add to cart button a text message show on screen.

logic : if (StoreAttributes[USA or NZD] < QTY inserted){ echo popup } else{ continue;}

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

// phpcs:disable Magento2.Templates.ThisInTemplate.FoundThis

/**
 * Product additional attributes template
 *
 * @var $block \Magento\Catalog\Block\Product\View\Attributes
 */
?>
<?php
    $_helper = $this->helper(Magento\Catalog\Helper\Output::class);
    $_product = $block->getProduct();
    $ObjectManager = \Magento\Framework\App\ObjectManager::getInstance();
    $storeManager = $ObjectManager->get('\Magento\Store\Model\StoreManagerInterface');
?>
<div style="border: 1px solid #ddd; padding: 10px 10px 10px 10px; width: fit-content;">
    <p Style="color: green; font-size: 14px; font-weight: bold;" >In stock</p>
    <p Style="color: #e49300; font-size: 14px; font-weight: bold;" >10 or fewer</p>
    <p Style="color: red; font-size: 14px; font-weight: bold;" >Out of stock</p>
</div>
<?php if ($_additional = $block->getAdditionalData()) :?>
    <div class="additional-attributes-wrapper table-wrapper">
        <table class="data table additional-attributes" id="product-attribute-specs-table">
            <caption class="table-caption"><?= $block->escapeHtml(__('More Information')) ?></caption>
            <tbody>
            <?php foreach ($_additional as $_data) :
                $stockHolding = $_data['value'];
                $storeName = $_data['label'];
                $selectedStore = $storeManager->getStore()->getName();
                
                $color = "black";
                if ($stockHolding <= 0)
                    $color = "red";
                else if (($stockHolding >= 1) && ($stockHolding <= 10))
                    $color = "#e49300";
                else if ($stockHolding > 10)
                    $color = "green";?>
                <tr>
                    <th <?php echo "style=\"color: $color\"";?> class="col label" scope="row"><?= $block->escapeHtml($_data['label']) ?></th>
                    <!-- <td class="col data" data-th="<?= $block->escapeHtmlAttr($_data['label']) ?>"><?= /* @noEscape */ $_helper->productAttribute($_product, $_data['value'], $_data['code']) ?></td> -->
                </tr>
            <?php endforeach; ?>
            <?php foreach ($_additional as $data) :
            $storeCode = $data['value'];
            $storeName = $data['label'];
            $selectedStore = $storeManager->getStore()->getName();

            if($storeName =='USA' && $storeCode == 0 && $selectedStore == 'USA')
            echo '<script>alert( "They is no stock under USA but you can add product to cart. The delivery can take up to 14 working days")</script>';
            elseif($storeName =='NZD' && $storeCode == 0 && $selectedStore == 'NZD')
            echo '<script>alert( "They is no stock under NZD but you can add product to cart. The delivery can take up to 14 working days")</script>';
    ?>
    <?php endforeach; ?>
            </tbody>
        </table>
    </div>
<?php endif;?>

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.