|
Shop_Product class
Represents a product.
The class has fields and methods for accessing the product name, description, price and other parameters.
Public propertiesShow inherited properties.
Public methodsShow inherited methods.
Events
Property details¶ allow_pre_order propertypublic boolean $allow_pre_order;
Indicates whether the product inventory tracking settings allow pre-ordering. You can use this field value for detecting whether the Add to Cart button should be visible on the
product details page. Usage example:
<? if (!$product->is_out_of_stock() || $product->allow_pre_order): ?> ... Add to Cart button ... <? endif ?> ¶ category_list propertypublic Db_DataCollection $category_list;
A list of categories the product belongs to. Each element in the collection is an object of Shop_Category class.
¶ created_at propertypublic Phpr_DateTime $created_at;
Specifies a date and time when the product has been added to the database.
¶ depth propertypublic float $depth;
Specifies the product depth.
¶ description propertypublic string $description;
Specifies the product description in HTML format.
¶ disable_completely propertypublic boolean $disable_completely;
Determines whether the product and all its grouped products are disabled (overrides the enabled property). Completely disabled products are displayed in the product list in the Administration Area, but not displayed in other lists (Discounts, etc.).
¶ enabled propertypublic boolean $enabled;
Determines whether the product is enabled. See also disable_completely property.
¶ expected_availability_date propertypublic Phpr_DateTime $expected_availability_date;
Specifies the date of the product's expected availability. You can display this field on the website using the following simple code:
<? if ($product->is_out_of_stock()): ?> <p> <strong>This product is temporarily unavailable</strong> <? if ($product->expected_availability_date): ?> <br/>The expected availability date is <?= $product->displayField('expected_availability_date') ?> <? endif ?> </p> <? endif ?> ¶ extra_options propertypublic Db_DataCollection $extra_options;
A collection of the product extra options. Each element in the collection is Shop_ExtraOption object.
¶ files propertypublic Db_DataCollection $files;
A list of files associated with the product. Files can be associated only with downloadable products. Each element of the collection is an object of the Shop_ProductFile class.
¶ grouped_menu_label propertypublic string $grouped_menu_label;
Specifies a label for the grouped products option list, for example "Color". A value of this field is specified in the Attribute Name field of the Create/Edit Product form in the Administration Area, on the Grouped tab.
¶ grouped_option_desc propertypublic string $grouped_option_desc;
Specifies a description of the product in the list of grouped products, for example "Green".
¶ grouped_products propertypublic Db_DataCollection $grouped_products;
A list of grouped products associated with the product. Use this list for displaying a list of grouped products on the product page. Each element in the collection is Shop_Product object.
¶ height propertypublic float $height;
Specifies the product height.
¶ id propertypublic integer $id;
Specifies the product identifier in the database.
¶ images propertypublic Db_DataCollection $images;
A collection of the product images. Each element in the collection is an object of the Db_File class.
¶ in_stock propertypublic integer $in_stock;
Indicates how many units of the products are available in stock. See also the in_stock_grouped() method.
¶ manufacturer propertypublic Shop_Manufacturer $manufacturer;
Specifies the product manufacturer. This field can be NULL if a manufacturer is not specified for the product.
¶ master_grouped_product propertypublic Shop_Product $master_grouped_product;
Specifies a reference to the parent grouped product.
¶ meta_description propertypublic string $meta_description;
Specifies the product description for outputting in the HTML META element on the product details page.
¶ meta_keywords propertypublic string $meta_keywords;
Specifies the product keywords for outputting in the HTML META element on the product details page.
¶ name propertypublic string $name;
Specifies the product name.
¶ options propertypublic Db_DataCollection $options;
Contains a list of product options. Each element in the collection is an object of Shop_CustomAttribute class.
Please read the Displaying Product Options article for details and code examples.
¶ product_id propertypublic integer $product_id;
Specifies identifier of a parent product for a grouped product.
¶ product_type propertypublic Shop_ProductType $product_type;
Specifies the product type.
¶ rating_all propertypublic float $rating_all;
Specifies a rating based on all reviews, included non-approved.
¶ rating_all_review_num propertypublic integer $rating_all_review_num;
Specifies a number of all reviews with a rating specified. You can use this filed for displaying the "Based on 8 reviews" text near the product rating value.
¶ rating_approved propertypublic float $rating_approved;
Returns a rating based on approved reviews. This and the $rating_all fields has a value in the 0-5 range. Value 0 means that there is no
rating information available for the product. Values have increment of 0.5 i.e.: 1, 1.5, 2, 2.5, 3, 3.5, 4. 4,5, 5. There is no 0.5 value,
because the minimal rating a visitor can set is 1.
¶ rating_review_num propertypublic integer $rating_review_num;
Specifies a number of approved reviews with rating specified. You can use this filed for displaying the "Based on 8 reviews" text near the product rating value.
¶ related_products propertypublic Db_DataCollection $related_products;
A list of related products. Each element of the collection is an object of Shop_Product class. The collection contains all enabled and available related products,
but ignores product visibility filters (customer group filter, etc). If you want to obtain a list of related products with visibility
filters applied, please use the list_related_products() method.
¶ short_description propertypublic string $short_description;
Specifies the product short description as plain text.
¶ sku propertypublic string $sku;
Specifies the product SKU.
¶ tax_class propertypublic Shop_TaxClass $tax_class;
Specifies a tax class the product belongs to.
¶ url_name propertypublic string $url_name;
Specifies the product URL name. The shop:product action uses this field to load a product by an URL parameter. Usually you don't
need to access the field directly. Use the page_url() method for creating links to
product pages.
¶ visibility_catalog propertypublic boolean $visibility_catalog;
Determines whether product should be visible in the catalog. This field affects the Shop_Category::list_products() and Shop_CustomGroup::list_products() methods.
¶ visibility_search propertypublic boolean $visibility_search;
Determines whether product should be visible in search results. This field affects the find_products() method behavior.
¶ weight propertypublic float $weight;
Specifies the product weight.
¶ width propertypublic float $width;
Specifies the product width.
Method details¶ apply_availability() methodpublic Shop_Product apply_availability(boolean $group_products=true)
Applies product stock availability filter to a product list.
Call this method before calling the find() or
find_all() method. This method allows to
fetch custom product lists from the database, safely that out of stock products are not displayed.
Usage example:
<? $products = Shop_Product::create()->apply_availability(); $this->render_partial('shop:product_list', array( 'products'=>$products, 'paginate'=>false )); ?> See Also¶ apply_avaliability() methodpublic Shop_Product apply_avaliability()
This method is deprecated. Use apply_availability() method instead.
Applies product stock availability filter to a product list.
¶ apply_catalog_visibility() methodpublic Shop_Product apply_catalog_visibility()
Applied customer group filter.
Product catalog visibility can be configured on the Create/Edit Product form in the Administration Area.
Call this method before calling the find() or
find_all() method.
Usage example:
<? $products = Shop_Product::create()->apply_catalog_visibility(); $this->render_partial('shop:product_list', array( 'products'=>$products, 'paginate'=>false )); ?> ¶ apply_customer_group_visibility() methodpublic Shop_Product apply_customer_group_visibility()
Applied customer group filter.
Customer group filter can be configured on the Create/Edit Product form in the Administration Area.
Call this method before calling the find() or
find_all() method.
Usage example:
<? $products = Shop_Product::create()->apply_customer_group_visibility(); $this->render_partial('shop:product_list', array( 'products'=>$products, 'paginate'=>false )); ?> ¶ apply_filters() methodpublic Shop_Product apply_filters(boolean $group_products=true)
Applies visibility, availability and customer group filters to a product list.
Call this method before calling the find() or
find_all() method. This method allows to
fetch custom product lists from the database, safely that disabled and hidden product are not displayed.
The example below fetches all active products from the database:
<? $products = Shop_Product::create()->apply_filters(); $this->render_partial('shop:product_list', array( 'products'=>$products, 'paginate'=>false )); ?> See Also¶ apply_visibility() methodpublic Shop_Product apply_visibility()
Hides disabled products from a product list.
Call this method before calling the find() or
find_all() method. This method allows to
fetch custom product lists from the database, safely that disabled products are not displayed.
Usage example:
<? $products = Shop_Product::create()->apply_visibility(); $this->render_partial('shop:product_list', array( 'products'=>$products, 'paginate'=>false )); ?> See Also¶ find_products() methodpublic static Db_DataCollection find_products(string $query, Phpr_Pagination $pagination, integer $page=1, array $options=array())
Finds products by a search term and/or other options.
The $options array can have the following elements:
// Create the pagination object, 10 records per page $pagination = new Phpr_Pagination(10); // Load the current page index from the URL $current_page = $this->request_param(0, 1); $query = 'laptop'; $options = array(); $options['attributes'] = array('CPU'=>'2.33'); $options['options'] = array('color'=>'black'); $options['custom_groups'] = array('featured_products'); $products = Shop_Product::find_products($query, $pagination, $current_page, $options); $options['attributes'] = array('CPU'=>'!2.33'); $options['options'] = array('color'=>'!black'); See Also¶ get_attribute() methodpublic string get_attribute(string $name)
Returns a product attribute value by the attribute name.
¶ get_discounted_price() methodpublic float get_discounted_price(integer $quantity=1, integer $customer_group_id=NULL)
This method is deprecated. Use the get_sale_price() method instead
Returns the product's sale price.
¶ get_rss() methodpublic static string get_rss(string $feed_name, string $feed_description, string $default_product_url, integer $record_number=20)
Returns content of RSS feed representing a list of recently added products.
The $default_product_url parameter should contain an URL of a default product page
(see the page_url() method description).
See Also¶ get_sale_price() methodpublic float get_sale_price(integer $quantity=1, integer $customer_group_id=NULL)
Returns the product's sale price.
If there are no catalog price rules applied to the product and there
is no sale price specified for the product, the method returns the product base price.
The price can include tax if the Display catalog/cart prices including tax
option is enabled
If you need to get the base product price, without any catalog price rules applied, use the price() method. The optional parameters can be used for evaluating a price for specific quantity and customer group. If the $customer_group_id parameter is not specified, a currently logged in customer will be used. The following example outputs a product's base and sale prices: Price: <?= format_currency($product->price()) ?><br/> Sale Price: <?= format_currency($product->get_sale_price(1)) ?> See Also¶ get_sale_reduction() methodpublic float get_sale_reduction(integer $quantity, integer $customer_group_id=NULL)
Returns the difference between the product's regular price and sale price.
If there are no price rules applied to the product and no sale price or discount set, the method returns 0.
See Also¶ has_om_records() methodpublic boolean has_om_records()
Returns true if the product has Option Matrix records.
¶ image_url() methodpublic string image_url(integer $index, mixed $width, mixed $height, boolean $as_jpeg=true, array $params=array( 'mode'=>'keep_ratio'))
Returns an URL of a product image thumbnail.
Use this method for displaying product images. The $width and $height parameters are thumbnail width and height correspondingly.
You can use exact integer values, or word 'auto' for automatic image resizing. The $as_jpeg parameter allows you to generate
PNG images with transparency support. By default the parameter value is TRUE and the method generates a JPEG image. The $params
array allows to pass parameters to image processing modules (which handle the core:onProcessImage event). This method is proxiable.
The following line of code outputs a thumbnail of the first product image. The thumbnail width is 100 pixels, and thumbnail height is calculated
by LemonStand to keep the original aspect ratio.
<img src="<?= $product->image_url(0, 100, 'auto') ?>"/> See Also¶ in_stock_grouped() methodpublic integer in_stock_grouped()
Returns the total number of items in stock for the product and all its grouped products.
¶ is_discounted() methodpublic boolean is_discounted()
This method is deprecated. Use the is_on_sale() method instead.
Determines whether the product is on sale.
Products considered on sale if there are any active catalog price rules
affecting the product price, or if the product's On Sale checkbox is checked.
¶ is_low_stock() methodpublic boolean is_low_stock()
Checks whether the product has reached its low stock threshold.
For products which use Option Matrix the base product
is considered to have reached the threshold when the sum of all Option Matrix stock is equal or lower
than the low stock threshold.
¶ is_on_sale() methodpublic boolean is_on_sale()
Determines whether the product is on sale.
Products considered on sale if there are any active catalog price rules affecting the product price,
or if product's On Sale checkbox is checked.
¶ is_out_of_stock() methodpublic boolean is_out_of_stock()
Checks whether the product is out of stock.
For products which use Option Matrix the base product
is considered out of stock in case if all Option Matrix products are out of stock.
¶ list_all_reviews() methodpublic Db_DataCollection list_all_reviews()
Returns a list of all reviews, including non-approved.
The returned collection includes reviews of all grouped products.
See Also¶ list_applied_catalog_rules() methodpublic array list_applied_catalog_rules(integer $customer_group_id=NULL)
Returns a list of catalog price rules applied to the product.
Usage example:
<ul> <? foreach ($product->list_applied_catalog_rules() as $rule): ?> <li> <?= h($rule->name) ?> <?= h($rule->description) ?> </li> <? endforeach ?> </ul> ¶ list_discounted() methodpublic static Shop_Product list_discounted(array $options=array())
This method is deprecated. Use list_on_sale() method instead.
Returns a list of products on sale.
¶ list_extra_option_groups() methodpublic array list_extra_option_groups()
Returns a list of product extra options grouped by matching group names.
The method returns a nested array. Indexes in the array represent group names. Values represent extra option objects
belonging to the group. Result example: array('Group name 1'=>array(Shop_ExtraOption, Shop_ExtraOption), 'Group name 2'=>array(Shop_ExtraOption)).
See Also¶ list_grouped_products() methodpublic array list_grouped_products()
Returns the full list of grouped products, including this product.
The method ignores products stock availability and any visibility filters.
¶ list_on_sale() methodpublic static Shop_Product list_on_sale(array $options=array())
Returns a list of products on sale.
Products on sale include products with any catalog price rules applied
or with a sale price or discount specified directly on the product configuration page.
The method returns an instance of the Shop_Product class. To obtain a collection of products call the
find_all() method of the returned object.
The $options parameter allows to specify the sorting mode with the sorting element. This element should contain an array
of column names. The supported fields you can sort the products by are:
$products = Shop_Product::list_on_sale(array('sorting'=>array('price desc')))->limit(10)->find_all(); $products = Shop_Product::list_on_sale(array('group_products'=>false))->limit(10)->find_all(); See Also¶ list_related_products() methodpublic Shop_Product list_related_products()
Returns a list of enabled, visible and available related products.
The method returns the configured Shop_Product class. Call the find_all() method of
the return object in order to obtain a collection of related products.
Usage example:
<? $related_products = $product->list_related_products()->find_all(); if ($related_products->count): ?> <h3>Related products</h3> <? $this->render_partial('shop:product_list', array('products'=>$related_products)); ?> <? endif ?> See Also¶ list_reviews() methodpublic Db_DataCollection list_reviews()
Returns a list of approved reviews.
The returned collection includes reviews of all grouped products.
See Also¶ list_uploaded_files() methodpublic Db_DataCollection list_uploaded_files(string $session_key=NULL)
Returns a list of files uploaded by a customer.
This method is applicable only on the Product Details page. Please see
Supporting file uploads on the product page article for details.
¶ max_price() methodpublic float max_price(boolean $include_grouped_products=false, integer $customer_group_id=NULL)
Returns maximum product's sale price.
The method returns the maximum price for a specific or current customer group, optionally taking into account
grouped products and always taking into account Option Matrix records. Pass TRUE to the $include_grouped_products parameter
to include grouped product prices into the calculations. Pass a specific customer group identifier to the $customer_group_id parameter
if you want to get the maximum price for a specific customer group.
¶ min_price() methodpublic float min_price(boolean $include_grouped_products=false, integer $customer_group_id=NULL)
Returns minimum product's sale price.
The method returns the minimum price for a specific or current customer group, optionally taking into account
grouped products and always taking into account Option Matrix records. Pass TRUE to the $include_grouped_products parameter
to include grouped product prices into the calculations. Pass a specific customer group identifier to the $customer_group_id parameter
if you want to get the maximum price for a specific customer group.
¶ om() methodpublic mixed om(string $property_name, mixed $options=NULL)
Returns Option Matrix product property.
Specify the property name in the $property_name parameter.
It supports all product class properties. If the method cannot find the specified property in Option Matrix product
or if Option Matrix property value is empty, the method automatically falls back to the base product
property value. The method supports a list of special property names (which exist as methods in the Shop_Product class):
$posted_options = Shop_ProductHelper::get_default_options($product); $in_stock = $product->om('in_stock', $posted_options); ¶ options_as_string() methodpublic void options_as_string()
Returns a list of product options as a string.
The returned value has the following format: Color: green, Size: large.
This is applicable only for Option Matrix products and only when displaying the search results.
In all other cases the method returns NULL.
¶ order_by_price() methodpublic Shop_Product order_by_price(string $direction='asc')
Simplifies ordering products by their current price.
The following code example outputs all products sorted by price in reverse order:
<? $products = Shop_Product::create()->apply_filters()->order_by_price('desc'); $this->render_partial('shop:product_list', array( 'products'=>$products, 'paginate'=>false )); ?> ¶ page_url() methodpublic string page_url(string $base_url)
Returns a product page URL, based on the URL passed in the parameter.
Use this method to create links to products. If there is no custom page assigned to a product, the method just
adds the product URL name to the base URL. So, if you passed the product string to the method,
it would return strings like /product/red_mug or /product/apple_keyboard. For products which have
a custom page assigned and no URL Name assigned, the method returns the custom page URL. For products with both a
custom page and URL Name specified, the method returns the URL of the custom page plus the value of the URL
Name parameter: /custom_page/url_name. See the
Displaying a list of products article for the usage example.
¶ price() methodpublic float price(integer $quantity=1, integer $customer_group_id=NULL)
Returns the product's base price.
The price depends on price tiers and does not depend on catalog price rules.
If you need to get a price calculated with catalog price rules applied, use the get_sale_price() method.
The price can include tax if the Display catalog/cart prices including tax
option is enabled
The optional parameters can be used for evaluating a base price for specific quantity and customer group. If the $customer_group_id parameter is not specified, a currently logged in customer will be used. The following example outputs a product's base and sale prices: Price: <?= format_currency($product->price()) ?><br/> Sale Price: <?= format_currency($product->get_sale_price(1)) ?> See Also¶ volume() methodpublic float volume()
Returns the product volume.
|