The product module

class magento.models.product.Product(data, client)[source]View on GitHub

Bases: Model

Wrapper for the products endpoint

STATUS_ENABLED = 1
STATUS_DISABLED = 2
VISIBILITY_NOT_VISIBLE = 1
VISIBILITY_CATALOG = 2
VISIBILITY_BOTH = 4
DOCUMENTATION: str = 'https://adobe-commerce.redoc.ly/2.3.7-admin/tag/products/'

Link to the Official Magento 2 API documentation for the endpoint wrapped by the Model

IDENTIFIER: str = 'sku'

The API response field that the endpoint’s uid comes from

__init__(data, client)[source]View on GitHub

Initialize a Product object using an API response from the products endpoint

Parameters
  • data (dict) – the API response from the products endpoint

  • client (Client) – an initialized Client object

property excluded_keys

API response keys that shouldn’t be set as object attributes by set_attrs()

Returns

list of API response keys that shouldn’t be set as attributes

update_stock(qty)[source]View on GitHub

Updates the stock quantity

Parameters

qty (int) – the new stock quantity

Return type

bool

update_status(status)[source]View on GitHub

Update the product status

Parameters

status (int) – either 1 (for STATUS_ENABLED) or 2 (for STATUS_DISABLED)

Return type

bool

update_price(price)[source]View on GitHub

Update the product price

Parameters

price (Union[int, float]) – the new price

Return type

bool

update_special_price(price)[source]View on GitHub

Update the product special price

Parameters

price (Union[float, int]) – the new special price

Return type

bool

update_name(name, scope=None)[source]View on GitHub

Update the product name

Parameters
  • name (str) – the new name to use

  • scope (Optional[str]) – the scope to send the request on; will use the Client.scope if not provided

Return type

bool

update_description(description, scope=None)[source]View on GitHub

Update the product description

Parameters
  • description (str) – the new HTML description to use

  • scope (Optional[str]) – the scope to send the request on; will use the Client.scope if not provided

Return type

bool

update_metadata(metadata, scope=None)[source]View on GitHub

Update the product metadata

Parameters
  • metadata (dict) – the new meta_title, meta_keyword and/or meta_description to use

  • scope (Optional[str]) – the scope to send the request on; will use the Client.scope if not provided

Return type

bool

update_attributes(attribute_data, scope=None)[source]View on GitHub

Update top level product attributes with scoping taken into account

Note

Product attributes can have a Global, Store View or Website scope

Global Attributes

Values are updated on all store views and the admin

Website Attributes

Values are updated on all store views

Store View Attributes

Values are updated on the store view specified in the request scope

A second request will be made to update Store View and Website attributes on the admin, depending on how many Store views you have:

  • 1 View: admin values are updated for all attributes, regardless of scope

  • 2+ Views: admin values are updated only for website_product_attributes

Parameters
  • attribute_data (dict) – a dictionary of product attributes to update

  • scope (Optional[str]) – the scope to send the request on; will use the Client.scope if not provided

Return type

bool

update_custom_attributes(attribute_data, scope=None)[source]View on GitHub

Update custom attributes with scoping taken into account

See update_attributes() for details

Important

This method only supports updating custom attributes

Parameters
  • attribute_data (dict) – a dictionary of custom attributes to update

  • scope (Optional[str]) – the scope to send the request on; will use the Client.scope if not provided

Return type

bool

get_orders()[source]View on GitHub

Searches for orders that contain the product

If the product is configurable, returns orders containing any of its child products

Returns

orders that contain the product, as an individual or list of Order objects

Return type

Optional[Order | List[Order]]

get_order_items()[source]View on GitHub

Searches for order items that contain the product

If the product is configurable, returns order items containing any of its child products

Returns

order items that contain the product, as an individual or list of OrderItem objects

Return type

Optional[OrderItem | List[OrderItem]]

get_invoices()[source]View on GitHub

Searches for invoices that contain the product

If the product is configurable, returns invoices containing any of its child products

Returns

invoices that contain the product, as an individual or list of Invoice objects

Return type

Optional[Invoice | List[Invoice]]

delete()[source]View on GitHub

Deletes the product

Hint

If you delete a product by accident, the Product object’s data attribute will still contain the raw data, which can be used to recover it.

Alternatively, don’t delete it by accident.

Return type

bool

get_children(refresh=False, scope=None)[source]View on GitHub

Retrieve the child simple products of a configurable product

Parameters
  • refresh (bool) – if True, calls refresh() on the child products to retrieve full data

  • scope (Optional[str]) – the scope to refresh the children on (when refresh=True)

Return type

List[Product]

property children: List[Product]

If the Product is a configurable product, returns a list of its child products

property categories: Optional[Category | List[Category]]

Categories the product is in, returned as a list of Category objects

The product’s media gallery entries, returned as a list of MediaEntry objects

property thumbnail: MediaEntry

The MediaEntry corresponding to the product’s thumbnail

Link of the product’s thumbnail image

get_media_by_id(entry_id)[source]View on GitHub

Access a MediaEntry of the product by id

Parameters

entry_id (int) – the id of the media gallery entry

Return type

MediaEntry

property encoded_sku: str

URL-encoded SKU, which is used in request endpoints

property option_skus: List[str]

The full SKUs for the product’s customizable options, if they exist

Hint

When a product with customizable options is ordered, these SKUs are used by the API when retrieving and searching for Order and OrderItem data

property stock: int

Current stock quantity

property stock_item: dict

Stock data from the StockItem Interface

property stock_item_id: int

Item id of the StockItem, used to update_stock()

property description: str

Product description (as HTML)

property special_price: float

The current special (sale) price

class magento.models.product.MediaEntry(product, entry)[source]View on GitHub

Bases: Model

Wraps a media gallery entry of a Product

MEDIA_TYPES = ['base', 'small', 'thumbnail', 'swatch']
DOCUMENTATION: str = 'https://adobe-commerce.redoc.ly/2.3.7-admin/tag/productsskumediaentryId'

Link to the Official Magento 2 API documentation for the endpoint wrapped by the Model

IDENTIFIER: str = 'id'

The API response field that the endpoint’s uid comes from

__init__(product, entry)[source]View on GitHub

Initialize a MediaEntry object for a Product

Parameters
  • product (Product) – the Product that the gallery entry is associated with

  • entry (dict) – the json response data to use as the source data

query_endpoint()[source]View on GitHub

No search endpoint exists for media gallery entries

property excluded_keys: List[str]

API response keys that shouldn’t be set as object attributes by set_attrs()

Returns

list of API response keys that shouldn’t be set as attributes

property is_enabled
property is_thumbnail

Permalink to the image

disable(scope=None)[source]View on GitHub

Disables the MediaEntry on the given scope

Parameters

scope (Optional[str]) – the scope to send the request on; will use the Client.scope if not provided

Return type

bool

enable(scope=None)[source]View on GitHub

Enables the MediaEntry on the given scope

Parameters

scope (Optional[str]) – the scope to send the request on; will use the Client.scope if not provided

Return type

bool

add_media_type(media_type, scope=None)[source]View on GitHub

Add a media type to the MediaEntry on the given scope

Caution

If the media type is already assigned to a different entry, it will be removed

Parameters
Return type

bool

remove_media_type(media_type, scope=None)[source]View on GitHub

Remove a media type from the MediaEntry on the given scope

Parameters
Return type

bool

set_media_types(types, scope=None)[source]View on GitHub

Set media types for the MediaEntry on the given scope

Parameters
Return type

bool

set_position(position, scope=None)[source]View on GitHub

Set the position of the MediaEntry on the given scope

Parameters
  • position (int) – the position to change to

  • scope (Optional[str]) – the scope to send the request on; will use the Client.scope if not provided

Return type

bool

set_alt_text(text, scope=None)[source]View on GitHub

Set the alt text (label) of the MediaEntry on the given scope

Parameters
  • text (str) – the alt text to use

  • scope (Optional[str]) – the scope to send the request on; will use the Client.scope if not provided

Return type

bool

update(scope=None)[source]View on GitHub

Uses the data dict to update the media entry

Note

Some updates alter the data of other entries; if the update is successful, the associated Product will be refreshed on the same scope to keep the data consistent

Tip

If there’s only 1 store view, the admin will also be updated

Parameters

scope (Optional[str]) – the scope to send the request on; will use the Client.scope if not provided

Return type

bool

class magento.models.product.ProductAttribute(data, client)[source]View on GitHub

Bases: Model

Wrapper for the products/attributes endpoint

DOCUMENTATION: str = 'https://adobe-commerce.redoc.ly/2.3.7-admin/tag/productsattributes/'

Link to the Official Magento 2 API documentation for the endpoint wrapped by the Model

IDENTIFIER: str = 'attribute_code'

The API response field that the endpoint’s uid comes from

__init__(data, client)[source]View on GitHub

Initialize a ProductAttribute object using an API response from the products/attributes endpoint

Parameters
  • data (dict) – the API response from the products/attributes endpoint

  • client (Client) – an initialized Client object

property excluded_keys: List[str]

API response keys that shouldn’t be set as object attributes by set_attrs()

Returns

list of API response keys that shouldn’t be set as attributes

property options