Skip to content

Instantly share code, notes, and snippets.

@javierrojas10
Forked from rseon/cheatsheet.md
Created July 6, 2021 05:09
Show Gist options
  • Save javierrojas10/d8a5a3fb1853636f3a65514622898f2d to your computer and use it in GitHub Desktop.
Save javierrojas10/d8a5a3fb1853636f3a65514622898f2d to your computer and use it in GitHub Desktop.
Prestashop cheatsheet (>= 1.5)

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

Updated with 1.7 code.

Classes

Attribute

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

Configuration

  • Configuration::deleteByName(string $key) : bool
    Delete a configuration key in database
  • Configuration::get(string $key, ?int $idLang = null, ?int $idShopGroup = null, ?int $idShop = null, bool $default = false) : string
    Get a single configuration value
  • Configuration::set(string $key, mixed $values, ?int $idShopGroup = null, ?int $idShop = null) : void
    Set TEMPORARY a single configuration value
  • Configuration::updateValue(string $key, mixed $values, bool $html = false, ?int $idShopGroup = null, ?int $idShop = null) : bool
    Update configuration key and value into database (automatically insert if key does not exist)

Combination

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

Customer

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

Database

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

Image

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

Language

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

Link

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

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

Module

  • Module::getInstanceByName(string $module_name) : Module
    Return an instance of the specified module
  • Module->registerHook(string $hook_name) : bool
    Connect module to a hook
  • Module->unregisterHook(string|int $hook_id) : bool
    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)

  • ObjectModel->add() : bool
    Adds current object to the database
  • ObjectModel->associateTo(int|array $id_shops) : bool
    This function associate an item to its context
  • ObjectModel->delete() : bool
    Deletes current object from database
  • ObjectModel->duplicateObject() : ObjectModel
    Takes current object ID, gets its values from database, saves them in a new row and loads newly saved values as a new object
  • ObjectModel->save() : bool
    Saves current object to database (add or update)
  • ObjectModel->update() : bool
    Updates the current object in the database
  • ObjectModel->validateFields(bool $die = true, bool $error_return = false) : bool
    Checks if object field values are valid before database interaction
  • ObjectModel->validateFieldsLang(bool $die = true, bool $error_return = false) : bool
    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) : void
    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)
  • 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 = [], ?string $available_date = null) : bool
    Add a product attribute
  • Product->checkDefaultAttributes() : bool
    Check if there is no default attribute and create it if not
  • Product->deleteDefaultAttributes() : bool
    Delete all default attributes for product
  • Product->deleteImages() : bool
    Delete product images from database
  • Product->getAttributeCombinations(int $id_lang) : array
    Get all available product attributes combinations
  • Product::getDefaultAttribute(int $id_product) : int
    Get the default attribute for a product (id_product_attribute)
  • 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) : float
    Returns product price
  • Product::getProducts(int $id_lang, int $start, int $limit, string $order_by, string $order_way, int $id_category = false, bool $only_active = false, Context $context = null) : array
    Get all available products
  • 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 = null, array $id_shop_list = []) : array
    Update a product attribute
  • Product::updateDefaultAttribute(int id_product) : int

Shop

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

Stock

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

Tools

  • Tools::getIsset(string $key) : bool
  • Tools::getRemoteAddr() : string
    Get the server variable REMOTE_ADDR, or the first ip of HTTP_X_FORWARDED_FOR (when using proxy)
  • Tools::getServerName() : string
    Get the server variable SERVER_NAME
  • Tools::getAllValues() : mixed
    Get all values from POST / GET
  • Tools::getValue(string $key, mixed $default_value = false) : mixed
    Get a value from POST / GET. If unavailable, take a default value
  • Tools::isSubmit(string $submit) : bool
    Check if submit has been posted
  • Tools::str2url(string $str) : string
    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