Replace stock level with custom text

Created on 21 December 2022, over 2 years ago
Updated 18 April 2023, about 2 years ago

With D7 and Commerce + Commerce Stock using views it was possible to rewrite the stock value with custom text ie

Stock 20+ = Plenty of stock
Stock 1-20 = Only a few left
Stock 0 = Out of stock

I know out of the box that i can customise in stock and out of stock but any ideas how i can achieve the 'only a few left' scenario?

Thanks,

✨ Feature request
Status

Active

Version

1.1

Component

Code

Created by

πŸ‡ΈπŸ‡ͺSweden cruze72

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • πŸ‡ΈπŸ‡ͺSweden cruze72

    I've tried the following code in a views template file like this:

    
    views-view-field--my_view--default--field-stock.html.twig
    
    {% if field_stock %}
      {% set stock = field_stock.0.value %}
      {% if stock >= 10 %}
        <span style="color: green">In stock</span>
      {% elseif stock > 0 and stock < 10 %}
        <span style="color: orange">Limited stock</span>
      {% else %}
        <span style="color: red">Out of stock</span>
      {% endif %}
    {% endif %}
    
    

    but still can't get the view to show the text instead of stock level ....

  • πŸ‡ΈπŸ‡ͺSweden cruze72

    I've even tried injecting the above code directly as a custom views field rewrite. i can get a message printed but it shows 'out of stock' for all rows even when there is stock. I'm guessing that the issue lies with the field_stock.

Production build 0.71.5 2024