shopProduct
The purpose of this class is to work with a single product. It allows obtaining information about a product, modifying and saving a product to the database without accessing individual model classes.
Array access
When working with the class, you can handle its instance as an array. On the first access data are read from the database, and subsequent attempts to read data are satisfied with the use of cache.
Array access works both for reading and for writing. When data are written to an object, they are saved in an internal cache storage. Data are saved to the database via the call of the save()
method.
Example
$product = new shopProduct(9999); echo $product['name']; $product['name'] = 'Super product'; $product->save();
Accessible fields
// Fields of the 'shop_product' table that are accessible for reading and writing. int $product['id'] string $product['name'] string $product['summary'] string $product['meta_title'] string $product['meta_keywords'] string $product['meta_description'] string $product['description'] int $product['contact_id'] string $product['create_datetime'] string $product['edit_datetime'] int $product['status'] int $product['type_id'] int $product['image_id'] string $product['image_filename'] string $product['video_url'] int $product['sku_id'] string $product['url'] string $product['currency'] int $product['tax_id'] int $product['count_denominator'] float $product['order_multiplicity_factor'] int $product['stock_unit_id'] int $product['base_unit_id'] float $product['stock_base_ratio'] float $product['order_count_min'] float $product['order_count_step'] float $product['base_price'] float $product['min_base_price'] float $product['max_base_price'] int $product['cross_selling'] int $product['upselling'] int $product['category_id'] string $product['badge'] int $product['sku_type'] float $product['base_price_selectable'] float $product['compare_price_selectable'] float $product['purchase_price_selectable'] // Fields of the 'shop_product' table that are accessible only for reading because their values are calculated automatically. float $product['price'] float $product['compare_price'] float $product['min_price'] float $product['max_price'] string $product['ext'] float $product['rating'] int $product['rating_count'] float|null $product['count'] float|null $product['total_sales'] int $product['sku_count'] // Class instance fields accessible for reading and writing. // See more details in the descriptions of class methods getData() and setData(). array $product['params'] // shopProductParamsModel array $product['skus'] // shopProductSkusModel array $product['sets'] // shopProductSetsModel array $product['tags'] // shopProductTagsModel array $product['features'] // shopProductFeaturesModel array $product['features_selectable'] // shopProductFeaturesSelectableModel array $product['categories'] // shopProductCategoriesModel array $product['og'] // shopProductOgModel // Class instance fields accessible only for reading. // See more details in the descriptions of corresponding class methods. array $product['type'] // getType() string $product['currency_html'] // getCurrencyHtml() waContact $product['contact'] // getContact() array $product['images'] // getImages() array $product['pages'] // getPages() array $product['next_forecast'] // getNextForecast() array $product['reviews'] // getReviews() array $product['sku_features'] // getSkuFeatures()
Public methods
-
__construct
Class constructor.
-
isFrontend
Returns the value of the
$is_frontend
parameter passed to the class constructor. -
getId
Returns the id field’s value.
-
getImages
Returns the list of product images.
-
getPages
Returns the list of product subpages.
-
getContact
Returns an instance of the
waContact
class with the information about the user who created the product. -
getCurrencyHtml
Returns product currency’s HTML code.
-
save
Saves changes made to product properties to the database.
-
getData
Returns data from the internal class cache without accessing the database.
-
setData
Writes a value of a product property to the internal class cache.
-
getType
Returns information about the product’s type.
-
upSelling
Returns the list of products set up as upselling items for the specified product.
-
crossSelling
Returns the list of products set up as cross-selling items for the specified product.
-
getNextForecast
Returns information about product sales for the past 90 days and an estimated sales forecast.
-
getReviews
Returns the list of customer reviews about the product.
-
getSkuFeatures
Returns information about feature values saved in product variants’ properties.
-
checkRights
Returns information about authorized user’s access rights to the product editing.
-
duplicate
Creates a product copy.
-
getProductUrl
Returns the URL of the product page in the storefront.
-
getCanonicalCategory
Returns main product category’s data.
-
getCategoryUrl
Returns the main URL part of the main product category.
-
getBreadcrumbs
Returns array of data used to generate breadcrumbs navigation links on the product’s page in the storefront.
-
getVideo
Returns product video’s data.
-
getSkus
Returns product variants’ data.
-
getFeatures
Returns product features’ data.
public function __construct ($data = array(), $is_frontend = false)
Class constructor
Parameters
-
$data
Either a product ID, or an array of product data with key names matching the names of the shop_product table.
-
$is_frontend
Flag denoting the class instance use mode in frontend controllers. In the frontend mode, all prices returned by this class’ methods if rounding is enabled for the currency selected by an authorized user. Field values are accessible only for reading in the frontend mode.
Example
$product = new shopProduct(9999); echo $product['name'];
public function isFrontend()
Returns the value of the $is_frontend
parameter passed to the class constructor.
Example
$product = new shopProduct(9999, true); if ($product->isFrontend()) { echo 'The product class instance is in the frontend mode!'; }
public function getId()
Returns the id field’s value. Equivalent to reading the id field value.
Example
$product = new shopProduct(9999); echo $product->getId(); //9999 echo $product['id']; //9999
public function getImages ($sizes = array(), $absolute = false)
Returns the list of product images. Array keys are image IDs, each array value is a sub-array containing an individual image’s data.
Parameters
-
$sizes
Dimensions of image thumbnails, whose URL must be contained in the returned array, specified in one of the following formats:
- Associative array with items specified like key => size. To the sub-array of each image’s data are added items with keys named like 'url_...', for each key instead of the ellipsis, whose values are URLs of image thumbnails with the dimensions specified in the corresponding size values as described below:
- ...x... (e.g., '96x96'): a rectangle with the specified dimensions, reduced with the original dimension ratio to one of the specified dimension values and with the remaining part, not fitting the resulting image, cut off,
- ...x0 (e.g., '96x0'): a rectangle reduced with the original dimension ratio to the specified width,
- 0x... (e.g., '0x96'): a rectangle reduced with the original dimension ratio to the specified height,
- ... (e.g., '96'): a rectangle reduced with the original dimension ratio by its maximum dimension.
- Simple array of special strings denoting image dimensions: 'big' ('970'), 'default' ('750x0'), 'default_fit' ('750'), 'thumb' ('200x0'), 'crop' ('96x96'), 'crop_small' ('48x48'), or only one of these values without the use of an array.
- Array of integers denoting generation of image thumbnails, with the original dimension ratio, reduced by their maximum dimension, or only 1 integer without the use of an array.
- Associative array with items specified like key => size. To the sub-array of each image’s data are added items with keys named like 'url_...', for each key instead of the ellipsis, whose values are URLs of image thumbnails with the dimensions specified in the corresponding size values as described below:
-
$absolute
Flag denoting the necessity to return absolute image thumbnail URLs.
Example
$product = new shopProduct(9999); $images = $product->getImages([ 'main' => '700x0', 'aux' => '200', ]); wa_dump($images);
Result
[ 1 => [ 'id' => '1', 'product_id' => '9999', 'upload_datetime' => '2000-01-01 00:00:00', 'edit_datetime' => NULL, 'description' => '', 'sort' => '0', 'width' => '1500', 'height' => '2000', 'size' => '1000000', 'filename' => '', 'original_filename' => '01.png', 'ext' => 'png', 'badge_type' => NULL, 'badge_code' => NULL, 'edit_datetime_ts' => NULL, 'url_main' => '/wa-data/public/shop/products/99/99/9999/images/1/1.700x0.png', 'url_aux' => '/wa-data/public/shop/products/99/99/9999/images/1/1.200.png', ], ..., ]
Example
$product = new shopProduct(9999); $images = $product->getImages('big'); wa_dump($images);
Result
[ 1 => [ 'id' => '1', 'product_id' => '9999', 'upload_datetime' => '2000-01-01 00:00:00', 'edit_datetime' => NULL, 'description' => '', 'sort' => '0', 'width' => '1500', 'height' => '2000', 'size' => '1000000', 'filename' => '', 'original_filename' => '01.png', 'ext' => 'png', 'badge_type' => NULL, 'badge_code' => NULL, 'edit_datetime_ts' => NULL, 'url_big' => '/wa-data/public/shop/products/99/99/9999/images/1/1.970.png', ], ..., ]
Example
$product = new shopProduct(9999); $images = $product->getImages('big'); wa_dump($images);
Result
[ 1 => [ 'id' => '1', 'product_id' => '9999', 'upload_datetime' => '2000-01-01 00:00:00', 'edit_datetime' => NULL, 'description' => '', 'sort' => '0', 'width' => '1500', 'height' => '2000', 'size' => '1000000', 'filename' => '', 'original_filename' => '01.png', 'ext' => 'png', 'badge_type' => NULL, 'badge_code' => NULL, 'edit_datetime_ts' => NULL, 'url_big' => '/wa-data/public/shop/products/99/99/9999/images/1/1.970.png', ], ..., ]
Example
$product = new shopProduct(9999); $images = $product->getImages(500); wa_dump($images);
Result
[ 1 => [ 'id' => '1', 'product_id' => '9999', 'upload_datetime' => '2000-01-01 00:00:00', 'edit_datetime' => NULL, 'description' => '', 'sort' => '0', 'width' => '1500', 'height' => '2000', 'size' => '1000000', 'filename' => '', 'original_filename' => '01.png', 'ext' => 'png', 'badge_type' => NULL, 'badge_code' => NULL, 'edit_datetime_ts' => NULL, 'url_500' => '/wa-data/public/shop/products/99/99/9999/images/1/1.500.png', ], ..., ]
public function getPages()
Returns the list of product subpages. Equivalent to reading the pages field. Each item of the returned array is a sub-array containing the values of the shop_product_pages table’s fields.
Example
$product = new shopProduct(9999); $pages = $product->getPages(); wa_dump($pages);
Result
[ 1 => [ 'id' => '1', 'product_id' => '9999', 'name' => 'About', 'title' => 'About the product', 'url' => 'info', 'content' => '<p>...</p>', 'create_datetime' => '2000-01-01 00:00:00', 'update_datetime' => '2000-01-01 00:00:00', 'create_contact_id' => '1', 'sort' => '0', 'status' => '1', 'keywords' => '', 'description' => '', ], ..., ]
public function getContact()
Returns an instance of the waContact
class with the information about the user who created the product. Equivalent to reading the contact field.
Example
$product = new shopProduct(9999); echo $product->getContact()->getName(); // 'John Smith'
public function getCurrencyHtml()
Returns product currency’s HTML code. Equivalent to reading the currency_html field.
Example
$product = new shopProduct(9999); echo $product->getCurrencyHtml(); // '<span class="ruble">₽</span>'
public function save ($data = [])
Saves changes made to product properties, either using the array access technique or also passed in the $data
parameter, to the database.
Parameters
-
$data
Array with the key => value structure containing values for the shop_product table. The use of this method is equivalent to writing each value to the corresponding class instance field with a subsequent call of this method without a parameter.
Example
$product = new shopProduct(); // either pass all values as an array parameter to the save() method $product->save([ 'name' => 'Super product', 'url' => 'superproduct', ]); // or first write each value to the corresponding class instance field and then call the save() method without a parameter $product['name'] = 'Super product'; $product['url'] = 'superproduct'; $product->save();
public function getData ($name = null)
Returns data from the internal class cache without accessing the database. If there are no data in the cache yet then the method returns null
. If the cache contains data then the use of this method is equivalent to reading the corresponding class instance field.
Parameters
-
$name
Class instance field whose value must be returned. If no field name is specified then the method returns the entire cache contents.
Example
$product = new shopProduct(); $product['name'] = 'Super product'; $product['url'] = 'superproduct'; echo $product->getData('name'); //'Super product' echo $product->getData('url'); //'superproduct'
public function setData ($name, $value)
Writes a value of a product property to the internal class cache. Equivalent to writing a field value to an class instance using the array access technique.
Example
$product = new shopProduct(); $product->setData('name', 'Super product'); // the same using the array access $product['name'] = 'Super product';
public function getType()
Returns information about the product’s type as an array of data retrieved from the shop_type table. Equivalent to reading the type field.
Example
$product = new shopProduct(9999); $type = $product->getType(); wa_dump($type);
Result
[ 'id' => '1', 'sort' => '0', 'name' => 'Ordinary product', 'icon' => 'box', 'cross_selling' => 'alsobought', 'upselling' => '0', 'count' => '3149', 'stock_unit_fixed' => '2', 'stock_unit_id' => '0', 'base_unit_fixed' => '2', 'base_unit_id' => NULL, 'stock_base_ratio_fixed' => '2', 'stock_base_ratio' => '1.00000000', 'count_denominator_fixed' => '2', 'count_denominator' => NULL, 'order_multiplicity_factor_fixed' => '2', 'order_multiplicity_factor' => NULL, 'order_count_min_fixed' => '2', 'order_count_min' => NULL, 'order_count_step_fixed' => '2', 'order_count_step' => NULL, ]
public function upSelling ($limit = 5, $available_only = false)
Returns the list of products set up as upselling items for the specified product.
Parameters
-
$limit
Maximum number of returned recommended products.
-
$available_only
Flag denoting the necessity to return only the recommended products that are available in stock and are marked as available for ordering.
Example
$product = new shopProduct(9999); $upselling_products = $product->upSelling(3); wa_dump($upselling_products);
Result
[ 26521 => [ id => '26521' name => 'Boeing 747' summary => '...' ... ], 26522 => [ id => '26522' name => 'Boeing 767' summary => '...' ... ], ..., ]
public function crossSelling ($limit = 5, $available_only = false, $exclude = array())
Returns the list of products set up as cross-selling items for the specified product.
Parameters
-
$limit
Maximum number of returned recommended products.
-
$available_only
Flag denoting the necessity to return only the recommended products that are available in stock and are marked as available for ordering.
-
$exclude
Array of IDs of the products which must be excluded from the returned result.
Example
$product = new shopProduct(9999); $cross_selling_products = $product->crossSelling(3); wa_dump($cross_selling_products);
Result
[ 26468 => [ id => '26468' name => '...' summary => '...' ... ], 26469 => [ id => '26469' name => '...' summary => '...' ... ], ..., ]
public function getNextForecast()
Returns information about product sales for the past 90 days and an estimated sales forecast. Equivalent to reading the next_forecast field.
Example
$product = new shopProduct(9999); $forecast = $product->getNextForecast(); wa_dump($forecast);
Result
[ sales => 19980 // sales amount expressed in the main store currency sold => 2 // average monthly sold product quantity according to statistics for the past 90 days sold_rounded => 2 // average monthly sold product quantity rounded to an integer sold_rounded_1 => 2 // average monthly sold product quantity rounded to 1 decimal digit sold_rounded_1_str => '2.0' // average monthly sold product quantity rounded and formatted as a string profit => 19980 // sales profit expressed in the main store currency days => 150 // number of days when the products’ stock must run out, according to the forecast date => 1464444704 // UNIX timestamp of the day when the products’ stock must run out, according to the forecast count => 10 // products’ stock quantity ]
public function getReviews()
Returns the list of customer reviews about the product. Equivalent to reading the reviews field.
Example
$product = new shopProduct(9999); $reviews = $product->getReviews(); wa_dump($reviews);
Result
[ 1 => [ 'id' => '1', 'left_key' => '1', 'right_key' => '2', 'depth' => '0', 'parent_id' => '0', 'product_id' => '9999', 'review_id' => '0', 'datetime' => '2000-00-00 13:00:00', 'status' => 'approved', 'title' => 'Super!', 'text' => 'Ordering this item for a hundredth time now. Absolutely the best product in the whole world.', 'rate' => '5.00', 'contact_id' => '99', 'name' => NULL, 'email' => NULL, 'images_count' => '0', 'site' => NULL, 'auth_provider' => 'user', 'ip' => '2130706433', 'images' => [], 'datetime_ts' => 1668508693, 'author' => [ 'name' => 'John Smith', 'email' => NULL, 'site' => NULL, 'id' => '1', 'is_user' => '1', 'is_company' => '0', 'photo' => '0', 'photo_url_50' => '/wa-content/img/userpic50@2x.jpg', 'photo_url_20' => '/wa-content/img/userpic20@2x.jpg', ], ], ]
public function getSkuFeatures()
Returns information about feature values saved in product variants’ properties. Equivalent to reading the sku_features field.
Example
$product = new shopProduct(9999); $sku_features = $product->getSkuFeatures(); wa_dump($sku_features);
Result
[ 3182 => [ 'size' => 'L', 'weight' => shopDimensionValue object { shopDimensionValuevalue => '120' shopDimensionValueunit => 'g' shopDimensionValuetype => 'weight' shopDimensionValueunits => NULL shopDimensionValuevalue_base_unit => '0.12' shopDimensionValuebase_code => NULL shopDimensionValueformat => false id => '1' feature_id => '1' sort => '0' }, ], 3183 => [ 'size' => 'XL', 'weight' => shopDimensionValue object { shopDimensionValuevalue => '150' shopDimensionValueunit => 'g' shopDimensionValuetype => 'weight' shopDimensionValueunits => NULL shopDimensionValuevalue_base_unit => '0.12' shopDimensionValuebase_code => NULL shopDimensionValueformat => false id => '2' feature_id => '1' sort => '2' }, ], ]
public function checkRights()
Returns information about authorized user’s access rights to the product editing.
Example
$product = new shopProduct(9999); if ($product->checkRights()) { // the authorized user has access rights to edit this product }
public function duplicate()
Creates a product copy and returns a class instance for the created copy.
Example
$product = new shopProduct(9999); $new_product = $product->duplicate();
public function getProductUrl ($storefront = null, $set_canonical = false, $absolute = true)
Returns the URL of the product page in the storefront.
Parameters
-
$storefront
Information about the storefront for which the product page URL must be returned. Allowed values:
true
: To return the product page URL for an automatically detected storefront, i.e. the current storefront for the frontend and the first storefront in the list for the backend.- String containing a storefront domain name and path; e.g.,
'mydomain.com/shop/'
.
-
$set_canonical
Flag denoting the necessity to return the canonical product page URL containing the product’s main category page URL (for the Natural and Flat URL types).
-
$absolute
Flag denoting the necessity to return the absolute product page URL.
Example
$product = new shopProduct(9999); echo $product->getProductUrl(true, true, true); // http://mydomain.com/shop/sale/superproduct/ echo $product->getProductUrl(true, true, false); // /shop/sale/superproduct/ echo $product->getProductUrl(true, false, true); // http://mydomain.com/shop/superproduct/
public function getCanonicalCategory ($route = null)
Returns main product category’s data if it is available in the specified storefront.
Parameters
-
$route
Array of parameters of the storefront for which the availability of the main product category must be verified. If not specified then the main category availability in the storefront is not performed.
Example
$product = new shopProduct(9999); $canonical_category = $product->getCanonicalCategory($route);
public function getCategoryUrl ($route = null)
Returns the main URL part of the main product category if it is available in the specified storefront.
Parameters
-
$route
Array of parameters of the storefront for which the availability of the main product category must be verified. If not specified then the main category availability in the storefront is not performed.
Example
$product = new shopProduct(9999); $canonical_category_url_part = $product->getCategoryUrl($route); // 'sale/accessories'
public function getBreadcrumbs ($product_link = false, $route = null)
Returns array of data used to generate breadcrumbs navigation links on the product’s page in the storefront.
Parameters
-
$product_link
Flag denoting the necessity to add the product’s data to the returned breadcrumbs data array.
-
$route
Array of parameters of the storefront which must be taken into account to generate breadcrumbs link URLs. If no value is specified then current storefront’s parameters are used.
Example
$product = new shopProduct(9999); $breadcrumbs_data = $product->getBreadcrumbs(true);
Result
[ 13 => [ 'url' => '/shop/accessories', 'name' => 'Accessories', ], 16 => [ 'url' => '/shop/accessories/cases/', 'name' => 'Cases', ], 0 => [ 'url' => NULL, 'name' => 'Tablet PC case', ], ]
public function getVideo ($sizes = array(), $absolute = false)
Returns product video’s data.
Parameters
-
$sizes
String value denoting the video clip thumbnail image dimensions, or an array of such values. Allowed dimensions values are explained in the description of the getImages() method. The URLs of image thumbnails with the specified dimensions are added to the returned data array.
-
$absolute
Flag denoting the necessity to return absolute image thumbnail URLs instead of relative ones.
Example
$product = new shopProduct(9999); wa_dump($product->getVideo(['96x96', '100', '200x0'], true));
Result
[ 'product_id' => '9999', 'orig_url' => 'http://youtu.be/...', 'url' => '//www.youtube.com/embed/...', 'width' => 560, 'height' => 315, 'images' => [ 'http://mydomain.com/wa-data/public/shop/products/99/99/9999/video/96x96.jpg', 'http://mydomain.com/wa-data/public/shop/products/99/99/9999/video/100.jpg', 'http://mydomain.com/wa-data/public/shop/products/99/99/9999/video/200x0.jpg', ], ]
public function getSkus()
Returns product variants’ data: values of the shop_product_skus table and the quantities available in all setup stocks.
Example
$product = new shopProduct(9999); wa_dump($product->getSkus());
Result
[ 398 => [ 'id' => '398', 'product_id' => '9999', 'sku' => '...', 'sort' => '1', 'name' => 'white', 'image_id' => '0', 'price' => 99.0, 'primary_price' => 0.0, 'purchase_price' => 0.0, 'compare_price' => 0.0, 'count' => '12.000', 'available' => '1', 'stock_base_ratio' => NULL, 'order_count_min' => NULL, 'order_count_step' => NULL, 'status' => '1', 'dimension_id' => NULL, 'file_name' => '', 'file_size' => '0', 'file_description' => NULL, 'virtual' => '0', 'stock' => [ 2 => '0.000', 1 => '12.000', ], ], ]
public function getFeatures ($status = null)
Returns product features’ data.
Parameters
-
$status
Allowed visibility of the returned product features in the storefront:
'public'
(return only visible features),'all'
(return all features),null
(in the storefront, return only visible feature; otherwise return all features).
Example
$product = new shopProduct(9999); wa_dump($product->getFeatures());
Result
[ 'accessory_type' => 'case', 'country_of_production' => 'China', 'suitable_for' => [ 264 => 'tablets', ], 'weight' => shopDimensionValue object { shopDimensionValuevalue => '0.1' shopDimensionValueunit => 'kg' shopDimensionValuetype => 'weight' shopDimensionValueunits => NULL shopDimensionValuevalue_base_unit => '0.1' shopDimensionValuebase_code => NULL shopDimensionValueformat => false id => '200' feature_id => '95' sort => '135' }, ]