Skip to content

Instantly share code, notes, and snippets.

@johnulist
Forked from rseon/cheatsheet.md
Created November 20, 2020 10:26
Show Gist options
  • Save johnulist/039eb96c70440f44409747fcfb6e89c4 to your computer and use it in GitHub Desktop.
Save johnulist/039eb96c70440f44409747fcfb6e89c4 to your computer and use it in GitHub Desktop.
Prestashop cheatsheet (>= 1.5)

The most common. For me. Mostly from comments in source code.

Classes

Attribute

  • bool Attribute->cleanPositions(int $id_attribute_group) Reorder attribute position in group $id_attribute_group. Call it after deleting an attribute from a group.
  • int Attribute::getHigherPosition(int $id_attribute_group) Get the higher attribute position from a group attribute
  • bool AttributeGroup::cleanPositions() Reorder group attribute position. Call it after deleting a group attribute.

Configuration

  • bool string Configuration::deleteByName(string $key) Delete a configuration key in database
  • string Configuration::get(string $key) Get a single configuration value
  • void Configuration::set(string $key, string $values) Set TEMPORARY a single configuration value
  • bool Configuration::updateValue(string $key, string $values) Update configuration key and value into database (automatically insert if key does not exist)

Combination

  • int Combination::getIdByReference(int $id_product, string $reference) For a given product_attribute reference, returns the corresponding id
  • bool Combination::isFeatureActive() Check if combination is feature active in Prestashop

Customer

Retrieve logged user : $this->context->cookie->id_customer, $this->context->cookie->email

Database

  • bool Db::getInstance()->delete(string $table [, string $where ]) Delete in $table (without prefix) with $where condition
  • bool Db::getInstance()->execute(string $query) Execute the $query (INSERT, UPDATE, DELETE)
  • array Db::getInstance()->executeS(string $query) Execute the $query (SELECT) and return the result as array
  • array Db::getInstance()->getRow(string $query) Returns an associative array containing the first row of the $query
  • string Db::getInstance()->getValue(string $query) Returns a value from the first row, first column of a SELECT $query
  • bool Db::getInstance()->insert(string $table, array $data) Insert $data in $table (without prefix)
  • int Db::getInstance()->Insert_ID() Get the ID generated from the previous INSERT operation
  • int Db::getInstance()->numRows() Get number of rows for last result
  • bool Db::getInstance()->update(string $table, array $data [, string $where ]) Update $data in $table (without prefix) with $where condition

Image

  • int Image::getHighestPosition(int $id_product) Return highest position of images for a product
  • array Image::getImages(int $id_lang, int $id_product [, int $id_product_attribute = null ]) Return available images for a product
  • int Image::getImagesTotal(int $id_product) Return number of images for a product
  • bool Image::hasImages(int $id_lang, int $id_product [, int $id_product_attribute = null ]) Check if a product has an image available

Language

  • array Language::getLanguages([ bool $active = true ]) Returns available languages

Link

Retrieve global from PHP : $this->context->link and from Smarty : {$link}.

  • string Link->getAdminLink(string $controller [, bool $with_token = true ]) Use controller name to create a link
  • string Link->getCategoryLink(Category $category [, string $alias = null [, int $id_lang = null [, string $selected_filters = null [, ... ]]]]) Create a link to a category
  • string Link->getCMSCategoryLink(CMSCategory $cms_category, [ string $alias = null [, ... ]]) Create a link to a CMS category
  • string Link->getCMSLink(CMS $cms, [ string $alias = null [, ... ]]) Create a link to a CMS page
  • string Link->getImageLink(string $name, string $ids [, string $type = null ]) Returns a link to a product image for display
  • string Link->getManufacturerLink(Manufacturer $manufacturer [, string $alias = null [, ... ]]) Create a link to a manufacturer
  • string Link->getModuleLink(string $module [, string $controller = 'default' [, array $params = array() [, ... ]]]) Create a link to a module
  • string Link->getPageLink(string $controller [, ... ]) Create a simple link
  • string Link->getProductLink(Product $product [, string $alias = null [, ... ]]) Create a link to a product
  • string Link->getSupplierLink(Supplier $supplier [, string $alias = null [, ... ]]) Create a link to a supplier

Module

  • Module Module::getInstanceByName(string $module_name) Return an instance of the specified module
  • bool Module->registerHook(string $hook_name) Connect module to a hook
  • bool Module->unregisterHook(string|int $hook_id) Unregister module from hook (by name or id)

Object model

All Prestashop classes inherits of ObjectModel class so then can call these methods (Product, Combination, Shop... all classes in /classes)

  • bool ObjectModel->add() Adds current object to the database
  • bool ObjectModel->associateTo(int|array $id_shops) This function associate an item to its context
  • bool ObjectModel->delete() Deletes current object from database
  • ObjectModel ObjectModel->duplicateObject() Takes current object ID, gets its values from database, saves them in a new row and loads newly saved values as a new object
  • bool ObjectModel->save() Saves current object to database (add or update)
  • bool ObjectModel->update() Updates the current object in the database
  • bool ObjectModel->validateFields([ bool $die = true [, bool $error_return = false ]]) Checks if object field values are valid before database interaction
  • bool ObjectModel->validateFieldsLang([ bool $die = true [, bool $error_return = false ]]) Checks if multilingual object field values are valid before database interaction

OrderHistory

  • OrderHistory->changeIdOrderState(int $new_order_state, int|object $id_order [, bool $use_existing_payment = false ]) Sets the new state of the given order

Product

  • Product::__construct([ int $id_product = null [, bool $full = false [, int $id_lang = null [, int $id_shop = null ]]]])
  • bool Product->addCombinationEntity(float $wholesale_price, float $price, float $weight, float $unit_impact, float $ecotax, float $quantity, array $id_images, string $reference, int $id_supplier, string $ean13, int $default [, string $location = null [, string $upc = null [, int $minimal_quantity = 1 [, array $id_shop_list = array() [, string $available_date = null ]]]]]) Add a product attribute
  • bool Product->checkDefaultAttributes() Check if there is no default attribute and create it if not
  • bool Product->deleteDefaultAttributes() Delete all default attributes for product
  • bool Product->deleteImages() Delete product images from database
  • array Product->getAttributeCombinations(int $id_lang) Get all available product attributes combinations
  • int Product::getDefaultAttribute(int $id_product) Get the default attribute for a product (id_product_attribute)
  • float Product::getPriceStatic(int $id_product [, bool $usetax = true [, int $id_product_attribute = null [, int $decimals = 6 [, int $divisor = null [, bool $only_reduc = false [, bool $usereduc = true [, int $quantity = 1 [, bool $force_associated_tax = false [, int $id_customer = null [, int $id_cart = null [, int $id_address = null ]]]]]]]]]]]) Returns product price
  • array Product->updateAttribute(int $id_product_attribute, float $wholesale_price, float $price, float $weight, float $unit, float $ecotax, array $id_images, string $reference, string $ean13, int $default [, string $location = null [, string $upc = null [, int $minimal_quantity = 1 [, string $available_date = null [, bool $update_all_fields [, array $id_shop_list = array() ]]]]]]) Update a product attribute
  • int Product::updateDefaultAttribute(int id_product)

Shop

  • array Shop::getContextListShopID() Get a list of ID concerned by the shop context (E.g. if context is shop group, get list of children shop ID)
  • int ShopGroup::getIdByName(string $name) Return a group shop ID from group shop name

Stock

  • void StockAvailable::setQuantity(int $id_product, int $id_product_attribute, int $quantity [, int $id_shop = null ]) For a given id_product and id_product_attribute sets the quantity available

Tools

  • bool Tools::getIsset(string $key)
  • string Tools::getRemoteAddr() Get the server variable REMOTE_ADDR, or the first ip of HTTP_X_FORWARDED_FOR (when using proxy)
  • string Tools::getServerName() Get the server variable SERVER_NAME
  • mixed Tools::getAllValues() Get all values from POST / GET
  • mixed Tools::getValue(string $key [, mixed $default_value = false ]) Get a value from POST / GET. If unavailable, take a default value
  • bool Tools::isSubmit(string $submit) Check if submit has been posted
  • string Tools::str2url(string $str) Return a friendly url made from the provided string
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment