OpenCart – Simple Tricks That Go a Long Way

This is an article that will list certain tricks you can perform with the Opencart Framework that will go a long way. This will especially be valuable for those that are scripting custom modules to enhance a default Opencart installation. These examples of course will be using PHP Scripting Language.

Checking for Customer Logged & Getting Customer ID

On any page, at any time, you can grab the customer ID of the customer that is logged in. However, the customer must be logged in in order to do this. All you have to do is check like this:

if ($this->customer->isLogged()) {
                        $cid = $this->customer->getId();
       }

Or you can check if NOT logged in

if (!$this->customer->isLogged()) {
                        $cid = $this->customer->getId();
       }

Assuming the customer is logged in the variable $cid now holds the customer ID

Checking if the cart has contents

On any page at any time you can check to see if there are any contents in the shopping cart like this:

if ($this->cart->hasProducts()){
            //actions
        }

Or you can check if NO items in cart

if (!$this->cart->hasProducts()){
            //actions
        }

Looking at the cart contents, if any

If there are cart contents then you can look at them like this:

if ($this->cart->hasProducts()){

$products = $this->cart->getProducts();
                
        foreach ($products as $product) {

//grab products

}

}

Check to see if the cart items actually have items in stock

if($this->cart->hasStock()){

//actions

}

Or check if NOT stock

if(!$this->cart->hasStock()){

//actions

}

Looking for quality OpenCart Web Hosting? Look no further than Arvixe Web Hosting!

Tags: , , , , , | Posted under OpenCart | RSS 2.0

Author Spotlight

Joe Stenhouse

Joe Stenhouse

I am a web application developer that specializes in PHP, JAVASCRIPT, MYSQL, HTML, and CSS. We manifest exciting potentials within the world wide web through means of innovation.

Leave a Reply

Your email address will not be published. Required fields are marked *


4 × = 4

You may use these HTML tags and attributes: <a href="" title="" rel=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>