In AnQiCMS template, how to judge and display the 'In stock' or 'Out of stock' status based on the product inventory (`Stock`) quantity?

Calendar 👁️ 84

It is crucial to clearly communicate the inventory status of products to users in website operations, especially for sites involving product display.This not only optimizes the user experience, reduces invalid consultations, but also effectively guides users' purchase decisions.AnQiCMS as a flexible and efficient content management system, actually implements the display of "in stock" or "out of stock" status according to the product inventory quantity in the template, which is very intuitive.

The AnQiCMS template system uses a syntax similar to Django, which allows us to control the display of page content through concise tags and variables. When handling product information, it is usually usedarchiveTags related to the document to obtain product data.

Understand the product inventory field

In AnQiCMS, product data (usually considered a "document model") will include a field namedStockThe field, this field is used to store the inventory quantity of the product. Our goal is to display the inventory status based onStockthe value of the field to dynamically display the inventory status.

Display the inventory status on the product detail page

When the user browses the details page of a single product, we hope the page can directly inform whether the product is in stock. In the template of the product details page (such asproduct/detail.htmlorarchive/detail.htmlIn your template structure and model name, we can usearchiveDetailtags to obtain the details of the current product, including itsStockValue.

First, obtain the details of the current product,StockValue:

{%- archiveDetail product with name="Stock" %}

Here we willStockThe value is assigned toproductthe variable. Next, we can use the conditional judgment tag{% if %}To checkproductThe value of the variable is:

<div class="product-stock-status">
    <p>库存状态:
        {% if product > 0 %}
            <span style="color: green; font-weight: bold;">有货</span>
        {% else %}
            <span style="color: red; font-weight: bold;">缺货</span>
        {% endif %}
    </p>
</div>

If the following code is run,product(That is, the product inventory) greater than 0, the page will display a green "In stock"; otherwise, it will display a red "Out of stock". You can adjust it according to your website design style.spanThe style of the label or use the preset CSS class to beautify the display.

Display stock status on the product list page.

On the product list page (such as category pages, search result pages, or home page recommendation modules), we often need to display multiple products, and each product should have its inventory status. In this case, we will usearchiveListTag to traverse the product list.

archiveListTag will return an array of products, we usually use{% for item in products %}such a loop structure to process each product one by one. Inside the loop, each product'sStockFields can be passed throughitem.Stock.

Here is an example of displaying inventory status in a product list:

<div class="product-grid">
    {% archiveList products with moduleId="2" type="page" limit="12" %} {# 假设moduleId="2"代表产品模型 #}
        {% for item in products %}
        <div class="product-item">
            <a href="{{item.Link}}" class="product-link">
                <img src="{{item.Thumb}}" alt="{{item.Title}}" class="product-thumbnail">
                <h3 class="product-title">{{item.Title}}</h3>
            </a>
            <div class="product-meta">
                <p class="product-price">¥ {{item.Price}}</p>
                <p class="product-stock">
                    {% if item.Stock > 0 %}
                        <span style="color: green;">有货</span>
                    {% else %}
                        <span style="color: red;">缺货</span>
                    {% endif %}
                </p>
            </div>
            <button class="add-to-cart-button" {% if item.Stock <= 0 %}disabled{% endif %}>
                {% if item.Stock > 0 %}立即购买{% else %}已售罄{% endif %}
            </button>
        </div>
        {% empty %}
        <p>抱歉,目前没有找到相关商品。</p>
        {% endfor %}
    {% endarchiveList %}
</div>

In this example, in addition to displaying 'In stock' or 'Out of stock', we also throughdisabledThe "Buy Now" button has been disabled, and the button text changed to "Sold Out" when out of stock, further improving the user experience.

Advanced consideration and optimization

  • Style separation:To ensure better maintainability, it is recommended thatstyleReplace the attribute with a CSS class. For example, define.in-stock { color: green; }and.out-of-stock { color: red; }Then use it in the template<span class="in-stock">有货</span>.

  • Multi-level inventory status:If you wish to display more detailed stock status, such as "low stock", "about to sell out", and so on, you can addelifConditional judgment:

    <p>库存状态:
        {% if product > 10 %}
            <span class="in-stock">有货</span>
        {% elif product > 0 and product <= 10 %}
            <span class="low-stock">少量库存 (仅剩 {{ product }} 件)</span>
        {% else %}
            <span class="out-of-stock">缺货</span>
        {% endif %}
    </p>
    

    Here is displayed when the inventory is greater than 10, when the inventory is between 1 and 10, it displays "Low stock and shows specific quantity", and when the inventory is 0, it displays "Out of stock".

  • Combine purchase logic:In actual e-commerce scenarios, inventory status often directly affects the availability of the purchase button. As shown in the above list page example, you can use the followingStockSet the button's value dynamicallydisabledProperty or click event to prevent users from purchasing out-of-stock items.

By these flexible template tags and conditional judgments, AnQiCMS allows website operators to easily implement dynamic display of product inventory status, greatly enhancing the practicality and interactivity of content presentation.


Frequently Asked Questions (FAQ)

  1. Ask: If my product inventory field is notStockbut a custom name, how should I judge?

    • Answer: In the AnQiCMS backend, the custom field you create in the "Content Model" can be directly passed through the template by its value{{archive.自定义字段名}}or{{item.自定义字段名}}To get. For example, if your custom inventory field name isproduct_quantity, then you can use it in the template{% if archive.product_quantity > 0 %}The logic is the same. To judge
  2. Question: Can I directly display the specific quantity of inventory on the front end?

    • Answer: Absolutely. In the template's{% if %}or{% else %}block, you can directly output{{product}}(on the detail page) or{{item.Stock}}(On the list page) you can display the specific inventory number. For example, you can display it as "In stock (remaining {{product}} pieces)" to provide users with clearer information.
  3. Ask: Can I set statuses like "Presale" or "Coming soon" in addition to "In stock" and "Out of stock"?

    • Answer: This usually requires combining with custom fields to implement. You can add a custom field named "Product Status" in the product model (the field type can be selected as "Dropdown" or "Single Selection") and preset options such as "In Stock", "Out of Stock", "Pre-sale", "About to Arrive".Then in the template, except for the judgmentStockQuantity, you can also add a judgment for this 'Product Status' custom field, for example{% if archive.商品状态字段 == "预售" %}To display more rich status information. This way, even if the inventory is 0, but the status is "pre-sale", it can be displayed correctly.

Related articles

How to display different content in the AnQiCMS template based on the value of the `item.Status` field (such as approved, in review)?

In website content operation, the review status of the content is a very important link.Whether it is a comment submitted by the user, a forum post, or an article or product information released by the website administrator, it often needs to be reviewed before it can be displayed to the public.AnQiCMS provides a flexible template mechanism that allows us to easily display different content on the front-end page based on the review status of the content (such as "approved" or "in review"), thereby providing users with clearer and more accurate feedback.AnQiCMS template syntax is similar to the Django template engine

2025-11-09

How to judge if there is a thumbnail in the `archiveList` tag in AnQiCMS template through `if` to selectively display images?

In AnQiCMS template development, displaying list content is a common requirement, and how to elegantly handle the images in these lists, especially thumbnails, is directly related to the visual effects and user experience of the website.The `archiveList` tag is one of the core content call tags of AnQiCMS, which helps us flexibly obtain various document lists.However, in practice, we often encounter situations where certain documents have not set thumbnails, which may lead to broken images or layout confusion on the page. At this time

2025-11-09

In AnQiCMS template, how to judge if the list is empty and display a prompt of 'No content'?

When using AnQiCMS for website template development, you often encounter situations where you need to display list data, such as article lists, product lists, or image galleries.When we retrieve data through template tags (such as `archiveList` or `categoryList`) and use a `for` loop to iterate over the list, if the list is empty, it is usually necessary to provide the user with a friendly prompt instead of displaying a blank space.How can you elegantly judge whether a list is empty in a `for` loop and display a 'No content' prompt?

2025-11-09

In AnQiCMS template, how to decide whether to display the default image or placeholder based on the existence of a variable?

In the template development of AnQiCMS, we often encounter situations where the content may not contain images.For example, a news article may not have an illustration, or a certain product in a product list may not have uploaded the main image temporarily.In this case, if the template directly calls the image address, it may display a broken image icon or leave an abrupt blank, which undoubtedly affects the overall aesthetics and user experience of the website.To solve this problem, AnQiCMS provides flexible template tags and filters, allowing us to determine whether the image variable exists

2025-11-09

In AnQiCMS template, how to judge whether a string can be successfully converted to a numeric type and perform conditional processing?

In AnQi CMS template creation, we often encounter situations where we need to process strings entered by users or retrieved from the database.One common requirement is to determine whether a string can be successfully converted to a numeric type and to perform different conditional processing based on the result.This is crucial for data display, calculation, and even simple form validation.The AnQi CMS template engine (based on Go language's Pongo2) provides a rich set of filters (filters) and logical tags, allowing us to flexibly meet this requirement. Below

2025-11-09

What is the default return value when the `integer` and `float` filters fail to convert in the AnQiCMS template?

When building a website on Anqi CMS, we often need to flexibly handle and display data in the template.These, `integer` and `float` filters are very commonly used tools when converting values to integers or floating-point numbers.However, have you ever thought about how the system will handle when these filters receive a value that cannot be recognized as a number?In other words, what default values will these filters return if the conversion operation fails?Understanding this is crucial for us to write robust and predictable template logic.###

2025-11-09

How to determine whether to truncate text and add an ellipsis (...) in AnQiCMS templates based on content length?

In AnQiCMS website content operation, how to elegantly handle long text content to make it both beautiful and complete on the page is the key to improving user experience.Especially on the list page, card display, or introduction area, overly long text often leads to layout confusion and affects the overall visual effect.AnQiCMS' powerful template engine provides various flexible ways to solve this problem, the most commonly used being the function of text truncation and adding ellipses.The AnQiCMS template system borrows the syntax of the Django template engine

2025-11-09

What are the respective application scenarios of the `striptags` and `removetags` filters when cleaning HTML code in AnQiCMS templates?

In AnQiCMS template design, we often encounter situations where we need to clean up or simplify the HTML code in the content.This is not just for beauty, but also to ensure the correct display of content, improve page loading efficiency, and even prevent potential security risks.The Aqie CMS provides two very practical filters: `striptags` and `removetags`.Although they are all related to the removal of HTML tags, each has a clear application scenario.### `striptags`

2025-11-09