Show "Out of stock" message on product listing pages

Ask the community for help and support.
lecarlb
VIP Member
VIP Member
Posts: 304
Joined: Mon Oct 26, 2020 5:26 pm
Has thanked: 42 times
Been thanked: 9 times

Re: Show "Out of stock" message on product listing pages

Post by lecarlb »

shopify wrote: Sat Jul 10, 2021 11:20 pm Sold Out.jpgI've done this using if ($listing['in_stock'] == 0 {
//Show add to cart button
} else {
//Show sold out button
}

But I like the look of that @lecarlb sold out button. I wonder how he did that button.
It's the older version of @zipurman Product Manager
User avatar
Kofod95
VIP Member
VIP Member
Posts: 605
Joined: Sat Feb 06, 2021 7:38 pm
Has thanked: 80 times
Been thanked: 141 times

Re: Show "Out of stock" message on product listing pages

Post by Kofod95 »

Using something similar to what Burt suggested, we have this:
Screenshot_20210711-191634.png
We are not measuring stock, as we have a physical store that is too small for POS-systems, but when we sell the last item of any sort, we change the product status to '2' ('0' is the red cross, '1' is the green check, '2' is something we have added to signify 'still visible, but out of stock'.). We have added a class to the product cards so they include the status, allowing us to target the sold out products with css.

//Daniel
You do not have the required permissions to view the files attached to this post.
I'm not smart, but sometimes even a blind chicken can find a corn.
Here are a lot of corns: Phoenix user guide
burt
Lead Developer
Lead Developer
Posts: 2423
Joined: Tue Oct 29, 2019 9:37 am
Has thanked: 49 times
Been thanked: 137 times

Re: Show "Out of stock" message on product listing pages

Post by burt »

Per the two posts above, you could drop a copy of this file:

/templates/default/includes/components/product_card.php

into the following location:

/templates/override/includes/components/product_card.php (note that if you use a different template to override, you should drop it into what template you are using)

Now find:

Code: Select all

    if (!$product->get('has_attributes')) {
      echo PHP_EOL, tep_draw_button(
        IS_PRODUCT_BUTTON_BUY,
        '',
        tep_href_link(basename($GLOBALS['PHP_SELF']), tep_get_all_get_params(['action', 'products_id']) . 'action=buy_now&products_id=' . (int)$product->get('id')),
        null,
        ['params' => 'data-has-attributes="0" data-in-stock="' . (int)$product->get('in_stock') . '" data-product-id="' . (int)$product->get('id') . '"'],
        'btn-light btn-product-listing btn-buy');
    }
Change to:

Code: Select all

    if ($product->get('in_stock') > 0) {  // is stock greater than zero? YES:
      if (!$product->get('has_attributes')) { 
         echo PHP_EOL, tep_draw_button(
        IS_PRODUCT_BUTTON_BUY,
        '',
        tep_href_link(basename($GLOBALS['PHP_SELF']), tep_get_all_get_params(['action', 'products_id']) . 'action=buy_now&products_id=' . (int)$product->get('id')),
        null,
        ['params' => 'data-has-attributes="0" data-in-stock="' . (int)$product->get('in_stock') . '" data-product-id="' . (int)$product->get('id') . '"'],
        'btn-light btn-product-listing btn-buy');
      }
    }
    else { // NO:
      echo tep_draw_button('<i class="fas fa-battery-empty"></i>', '', $product->get('link'), null, ['params' => 'title="OOS"'], 'btn-danger btn-product-listing btn-view');
    }
This is the stunning beauty of Matts templating system. You can do similar in the other places that show product lists (eg new roducts for month etc), but note that some files are in the older style of coding (but still templatable and over-rideable), it is just that the code will look slightly different.
Gamechanger Addon: Queued Emails, try before you buy.
shopify
Posts: 54
Joined: Tue Mar 30, 2021 3:36 pm
Has thanked: 23 times
Been thanked: 3 times

Re: Show "Out of stock" message on product listing pages

Post by shopify »

burt wrote: Tue Jul 13, 2021 10:55 am Per the two posts above, you could drop a copy of this file:

/templates/default/includes/components/product_card.php

into the following location:

/templates/override/includes/components/product_card.php (note that if you use a different template to override, you should drop it into what template you are using)

Now find:

Code: Select all

    if (!$product->get('has_attributes')) {
      echo PHP_EOL, tep_draw_button(
        IS_PRODUCT_BUTTON_BUY,
        '',
        tep_href_link(basename($GLOBALS['PHP_SELF']), tep_get_all_get_params(['action', 'products_id']) . 'action=buy_now&products_id=' . (int)$product->get('id')),
        null,
        ['params' => 'data-has-attributes="0" data-in-stock="' . (int)$product->get('in_stock') . '" data-product-id="' . (int)$product->get('id') . '"'],
        'btn-light btn-product-listing btn-buy');
    }
Change to:

Code: Select all

    if ($product->get('in_stock') > 0) {  // is stock greater than zero? YES:
      if (!$product->get('has_attributes')) { 
         echo PHP_EOL, tep_draw_button(
        IS_PRODUCT_BUTTON_BUY,
        '',
        tep_href_link(basename($GLOBALS['PHP_SELF']), tep_get_all_get_params(['action', 'products_id']) . 'action=buy_now&products_id=' . (int)$product->get('id')),
        null,
        ['params' => 'data-has-attributes="0" data-in-stock="' . (int)$product->get('in_stock') . '" data-product-id="' . (int)$product->get('id') . '"'],
        'btn-light btn-product-listing btn-buy');
      }
    }
    else { // NO:
      echo tep_draw_button('<i class="fas fa-battery-empty"></i>', '', $product->get('link'), null, ['params' => 'title="OOS"'], 'btn-danger btn-product-listing btn-view');
    }
I haven't a /templates/override/includes/components/ folder so I modified the file /templates/default/includes/components/product_card.php file but nothing changes. I still have the add to cart button even when I change stock to zero.
shopify
Posts: 54
Joined: Tue Mar 30, 2021 3:36 pm
Has thanked: 23 times
Been thanked: 3 times

Re: Show "Out of stock" message on product listing pages

Post by shopify »

By the way what file displays new products for the month?
User avatar
zipurman
PhoenixCart Developer
PhoenixCart Developer
Posts: 470
Joined: Tue Oct 13, 2020 5:20 pm
Has thanked: 84 times
Been thanked: 147 times

Re: Show "Out of stock" message on product listing pages

Post by zipurman »

shopify wrote: Sat Jul 24, 2021 11:50 am By the way what file displays new products for the month?
Modules - Content - New Products (index)
zipurman
aka Preston Lord
-----------
Happy to help where I can ;)

https://phoenixaddons.com
https://www.youtube.com/zipurman/ ** PHOENIX HOW-TO VIDEOS **
Image
User avatar
Kofod95
VIP Member
VIP Member
Posts: 605
Joined: Sat Feb 06, 2021 7:38 pm
Has thanked: 80 times
Been thanked: 141 times

Re: Show "Out of stock" message on product listing pages

Post by Kofod95 »

shopify wrote: Sat Jul 24, 2021 11:30 am I haven't a /templates/override/includes/components/ folder so I modified the file /templates/default/includes/components/product_card.php file but nothing changes. I still have the add to cart button even when I change stock to zero.
You should create the directory and copy the file into it, as that will preserve your changes throughout updates in the future - the point about templates is that it allows you to override core-layout instead of changing it directly.

However, on my 1.0.8.0 the product_card.php doesn't do anything (at least as far as I could tell), meaning the relevant change should be in product_listing.php instead (templates/override/includes/components/product_listing.php - just copy the one from the default template, and create the directories in the override template)

//Daniel
I'm not smart, but sometimes even a blind chicken can find a corn.
Here are a lot of corns: Phoenix user guide
User avatar
Kofod95
VIP Member
VIP Member
Posts: 605
Joined: Sat Feb 06, 2021 7:38 pm
Has thanked: 80 times
Been thanked: 141 times

Re: Show "Out of stock" message on product listing pages

Post by Kofod95 »

shopify wrote: Sat Jul 24, 2021 11:50 am By the way what file displays new products for the month?
The file is cm_i_card_products.php (includes/modules/content/index)

//Daniel
I'm not smart, but sometimes even a blind chicken can find a corn.
Here are a lot of corns: Phoenix user guide
shopify
Posts: 54
Joined: Tue Mar 30, 2021 3:36 pm
Has thanked: 23 times
Been thanked: 3 times

Re: Show "Out of stock" message on product listing pages

Post by shopify »

Kofod95 wrote: Sat Jul 24, 2021 6:00 pm
shopify wrote: Sat Jul 24, 2021 11:50 am By the way what file displays new products for the month?
The file is cm_i_card_products.php (includes/modules/content/index)

//Daniel
This files does not have anything in it that display products for the month on the index.php page. I need to modify the file that displays these products like the product_listing.php file to show item is out of stock. The index page is first place customers see products and if product is not in stock, then I'd like to display the out of stock button here too.
shopify
Posts: 54
Joined: Tue Mar 30, 2021 3:36 pm
Has thanked: 23 times
Been thanked: 3 times

Re: Show "Out of stock" message on product listing pages

Post by shopify »

Kofod95 wrote: Sat Jul 24, 2021 5:58 pm
shopify wrote: Sat Jul 24, 2021 11:30 am I haven't a /templates/override/includes/components/ folder so I modified the file /templates/default/includes/components/product_card.php file but nothing changes. I still have the add to cart button even when I change stock to zero.
You should create the directory and copy the file into it, as that will preserve your changes throughout updates in the future - the point about templates is that it allows you to override core-layout instead of changing it directly.

However, on my 1.0.8.0 the product_card.php doesn't do anything (at least as far as I could tell), meaning the relevant change should be in product_listing.php instead (templates/override/includes/components/product_listing.php - just copy the one from the default template, and create the directories in the override template)

//Daniel
I created the folder as dvised and changes are picked up here. Next thing is applying to the index.php page.
Post Reply