Jump to content

Only list products in stock


Recommended Posts

Hi ! Im trying to make a hook that only lists products in stock in certain category but im sure im missing something. Did any one tried to set up something similar.

Code to my hook:

 

use WHMCS\Database\Capsule;

add_hook('ShoppingCartProductList', 1, function($vars) {
    $categoryId = 15;

    $products = Capsule::table('tblproducts')
        ->where('category', $categoryId)
        ->get();

    $productsInStock = [];
    foreach ($products as $product) {
        if ($product->stockcontrol && $product->qty > 0) {
            $productsInStock[] = $product;
        }
    }

    $vars['productList'] = $productsInStock;

    return $vars;
});

 

Link to comment
Share on other sites

You can edit your orderforms template products.tpl file 

search for the below:

{foreach $products as $key => $product}

and add after it and make sure to close the {if} statement and include an else statement so it displays the other products not in this category

{if $product.stockControlEnabled === true && $product.qty > 0 && $gid === 15}
Edited by File_Explorer
typo
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use & Guidelines and understand your posts will initially be pre-moderated