I want to extend default "Only x left" message. Can you tell me which file controls it?

At the moment message is visible only when stock is less than qty threshold. I want if stock is more than qty threshold, to show message "More than qty threshold" and if sock is 0 or below 0(I have activated backorder) to show "Orderable with delivery" message.


Actually you asked 2 questions. To override the template file and override the block function.

The first one is easy.The original file is on vendor/magento/module-catalog-inventory/view/frontend/templates/stockqty/default.phtml. To override this file, you can copy this file and paste it on your active theme. So that app/design/frontend/Your_Vendor/Your_Theme/Magento_CatalogInventory/templates/stockqty/default.phtml. You can start editing it now.

The second one is a bit tricky, but still OK. To override the original threshold function isMsgVisible, you can just modify it a bit.

  1. Create a new module. Let's say, MyVendor_CatalogInventory. Create a new file app/code/MyVendor/CatalogInventory/etc/module.xml and add the following content:

    <?xml version="1.0" encoding="UTF-8"?>
    <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
        <module name="MyVendor_CatalogInventory" setup_version="0.0.1"/>
  2. Create new file app/code/MyVendor/CatalogInventory/etc/di.xml and add the following content:

    <?xml version="1.0" encoding="UTF-8"?>
    <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
        <type name="\Magento\CatalogInventory\Block\Stockqty\AbstractStockqty">
            <plugin name="MyVendor_CatalogInventory::aroundIsMsgVisible" type="MyVendor\CatalogInventory\Block\Stockqty" sortOrder="10"/>
  3. Create new file app/code/MyVendor/CatalogInventory/Block/Stockqty.php with the following content:

    namespace MyVendor\CatalogInventory\Block\;
    class Stockqty{
        public function aroundIsMsgVisible()
            //Start your logic here
  4. Enable this module

    cd <your magento root>
    php bin/magento module:enable MyVendor_CatalogInventory

Ref: http://devdocs.magento.com/guides/v2.1/extension-dev-guide/plugins.html

  • Thanks mate. I didn't check it, but that should be the way. – Agent Smith Nov 29 '17 at 22:28
  • Stockqty.php returns error "parse error: syntax error, unexpected ; expecting identifier t string on line 3 – Agent Smith Nov 30 '17 at 17:20
  • Something with namespace declaration, can you help with that – Agent Smith Nov 30 '17 at 17:21
  • You can ask another question :) – PY Yick Dec 1 '17 at 2:33

Will it be possible modifying only default.phtml file to have:

If StockQty > 0 && StockQty < QtyThreshold --- StockQty pcs 

If StockQty > QtyThreshold ---. More than QtyThreshold

If StockQty <=0 ---. With delivery: $stockdelivery (we will have such attribute with drop-down)

And show nothing if In Stock is set to Out of stock.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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