Skip to end of metadata
Go to start of metadata

Table of contents

Developer tips and tricks

How to check whether a user is logged to PrestaShop or not

You can easily perform this check by using the isLoggedBack() method provided in the Employee class (/classes/Employee.php).

This example assumes your file is located in the administration folder:

The isLoggedBack() method was located in the Cookie class in PrestaShop 1.4.x, and was moved to Employee class in v1.5.x.

How to find the speed bottleneck on a site

Try to enable the profiling feature, it will highlight which part of the code is slowing down your server.

  1. Put your store in maintenance mode.
  2. In your PrestaShop v1.5.x store, edit the /config/defines.inc.php file.
  3. On line 44, replace define('_PS_DEBUG_PROFILING_', false); by define('_PS_DEBUG_PROFILING_', true);
  4. Go to your store homepage and reload the page.

You should now be able to see how much time is consuming each function and SQL query.

How to retrieve the carrier's id

In PrestaShop 1.4, you could use {id_carrier}:

The same result can be achieved in PrestaShop 1.5 with {$cart->id_carrier}:

How to retrieve the cart's id

You have several ways to retrieve the Cart ID from the current visitor, the easiest one is to use the Context.

Step 1

Open the /modules/blockcart/ajax-cart.js file and look for this line:

Below this line, add this:

Step 2

Create a file named ajax.php in /modules/blockcart/ with the following code inside:

You might want to consider the fact that by retrieving this value in JavaScript, it will be publicly available to the visitor. Depending on how secure your code and payment modules are, this could be an issue.

How to import products from a XML file

With PrestaShop you can easily import your data using one of these options:

  1. By using the "CSV Import" feature in your Back-office
  2. By using the PrestaShop Web-service (custom development required)
  3. By using the existing classes (custom development required)

Below is a quick code snippet that is working with a specific XML document (stored as a heredoc string). It will create or update all the products, and take into account their price, availability, name, description, weight, etc.

Please note that:

  • This code MUST be adapted to your own XML needs!
  • This code will is not designed to work with combinations (color, sizes, etc.)
  • This code does not create categories for you, and is adding all the products to the "Home" category
  • No labels