0

this is my list.phtml code and look

enter image description here

instead I want to display like

enter image description here

my code

<div class="products wrapper <?= /* @noEscape */ $viewMode ?> products-<?= /* @noEscape */ $viewMode ?>">
    <ol class="products list items product-items">
        <?php /** @var $_product \Magento\Catalog\Model\Product */ ?>
        <?php foreach ($_productCollection as $_product) :?>
            <li class="item product product-item">
                <div>
                    <style>
                        .column {
                            float: left;
                            width: 20%;
                            padding: 10px;
                        }

                        .row:after {
                            content: "";
                            display: table;
                            clear: both;
                        }
                    </style>
                    <?php
                    $productImage = $block->getImage($_product, $imageDisplayArea);
                    if ($pos != null) {
                        $position = ' style="left:' . $productImage->getWidth() . 'px;'
                            . 'top:' . $productImage->getHeight() . 'px;"';
                    }
                    ?>
                    <?php
                    $_productNameStripped = $block->stripTags($_product->getName(), null, true);
                    ?>
                    <strong class="product name product-item-name">
                        <a class="product-item-link"
                           href="<?= $block->escapeUrl($_product->getProductUrl()) ?>">
                            <?= /* @noEscape */ $_helper->productAttribute($_product, $_product->getName(), 'name') ?>
                        </a>
                    </strong>

                    <div class="row">
                        <div class="column" style="padding: 1px;border-right: 1px solid #60d0e4;">
                            <?php // Product Image?>
                            <a href="<?= $block->escapeUrl($_product->getProductUrl()) ?>"
                               class="product photo product-item-photo"
                               tabindex="-1">
                                <?= $productImage->toHtml() ?>

                            </a>
                        </div>
                        <div class="column" style="background-color:#bbb;">

                            <p>Some text..</p>
                        </div>
                        <div class="column" style="background-color:#ccc;">

                            <p>Some text..</p>
                        </div>
                        <div class="column" style="background-color:#ccc;">

                            <p>Some text..</p>
                        </div>
                        <div class="column" style="background-color:#ccc;">

                            <p>Some text..</p>
                        </div>
                    </div>
                </div>
            </li>
        <?php endforeach; ?>
    </ol>
</div>
2

1 Answer 1

0

You cannot get the product stock from the product object directly. However, I see you use a $block already to retrieve other product information, such as the product image.

Therefore, you can modify the block constructor adding a new dependency in the constructor and creating a new function called getStockStatus($product) as follows:

// add this dependency in your constructor if already existing
public function __construct(
    \Magento\CatalogInventory\Api\StockRegistryInterface $stockRegistry
) {
    $this->stockRegistry = $stockRegistry;
}

// returns product quantity given the product object
public function getStockStatus($product)
{
    return $this->stockRegistry->getStockItem($product->getId());
}

In your list.phtml, you can use

$quantity = $block->getStockStatus($product)

to get the quantity.

After that, run bin/magento setup:install.

4
  • @shaziyali Did it work?
    – Pasquale
    Dec 4, 2020 at 17:14
  • no, I didn't understand can you explain to me on Instagram as @shaziyali8
    – shaziyali
    Dec 6, 2020 at 8:03
  • Please upvote the answer.
    – Pasquale
    Dec 7, 2020 at 13:33
  • ah sorry! I don't want to get quantity, I want a button with working and change the price
    – shaziyali
    Dec 8, 2020 at 13:07

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.