|
Shop_Category class
Represents a product category.
The class has fields and methods for accessing the category name and description
as well as a list of the category products. The category class is proxiable.
This class is extended with Db_Act_As_Tree class which implements tree functionality. It allows categories to be accessed as a hierarchy with methods get_parent(), get_parents(), list_root_children() and list_children(). By default list_root_children() and list_children() methods sort items by name. To sort the category lists accordingly the order you set in the Administration Area, pass the front_end_sort_order value to the methods: $root_categories = Shop_Category::create()->list_root_children('front_end_sort_order'); $subcategories = $category->list_children('front_end_sort_order'); Public propertiesShow inherited properties.
Public methodsShow inherited methods.
Events
Property details¶ code propertypublic string $code;
The category API code. You can use this field for finding a specific category in your API calls. Proxiable.
Example:
$category = Shop_Category::create()->find_by_code('computers'); ¶ description propertypublic string $description;
Specifies the category description in HTML format. Proxiable.
¶ id propertypublic integer $id;
Specifies the category database identifier.
¶ images propertypublic Db_DataCollection $images;
A collection of images assigned to the category. Each element in the collection is an object of the Db_File class. You can use this property directly to output
category images, or use the image_url() method. Not proxiable.
¶ name propertypublic string $name;
Specifies the category name. Proxiable.
¶ parent propertypublic Shop_Category $parent;
A reference to a parent category. This field is NULL for the root-level categories. Proxiable.
¶ short_description propertypublic string $short_description;
Specifies the short description of the category as a plain text. To output the short description use h() function to escape HTML-sensitive characters. Proxiable.
¶ url_name propertypublic string $url_name;
Specifies the category URL name. The shop:category action uses this field to load a category by an URL. Usually you don't need
to access the field directly. Use the page_url() method for creating links to category pages.
Method details¶ as_options() methodpublic static string as_options(integer $current_id=NULL)
Returns an HTML string containing a set of OPTION elements corresponding to all product categories.
Nested categories are indented. The method does not take into account category visibility and always return all categories.
You can use this method for creating a drop-down category selector. The optional parameter allows you to specify a category
identifier to be selected by default. This method is not proxiable. Example:
<select name="category_id"> <?= Shop_Category::as_options() ?> </select> ¶ get_url_name() methodpublic string get_url_name()
Returns the category URL name.
If nested category URLs and Prepend parent category URL options are enabled on System/Settings/eCommerce Settings page,
the method returns category URL name with the parent category URLs prepended. This method is proxiable.
¶ image_url() methodpublic string image_url(integer $index=0, mixed $width='auto', mixed $height='auto', boolean $as_jpeg=true, array $params=array( 'mode'=>'keep_ratio'))
Returns an URL of a category image thumbnail.
Use this method for displaying category 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 category image. The thumbnail width is 100 pixels, and thumbnail height is calculated
by LemonStand to keep the original aspect ratio.
<img src="<?= $category->image_url(0, 100, 'auto') ?>"/> See Also¶ is_current() methodpublic boolean is_current(string $current_category_url_name, boolean $look_in_subcategories=false)
A helper function which returns TRUE in case if the category URL name matches a value passed to the first parameter.
If the second parameter is TRUE, the function also examines all subcategories.
It allows to mark a category as current in the category list, if the category itself is not current, but one of its subcategories is current.
This method is proxiable.
¶ list_manufacturers() methodpublic Db_DataCollection list_manufacturers()
Returns a list of manufacturers of products belonging to the category.
This method is not proxiable. The method returns an instance of the Db_DataCollection class.
Each element in the collection is an object of Shop_Manufacturer class. You can output a list of
a category manufacturers with the following code:
<ul> <? $manufacturers = $category->list_manufacturers(); foreach ($manufacturers as $manufacturer): ?> <li><?= h($manufacturer->name) ?></li> <? endforeach ?> </ul> ¶ list_products() methodpublic Shop_Product list_products(array $options=array())
Returns a list of the category products.
This method is not proxiable. The result of this function is an object of the Shop_Product class.
To obtain a collection of all category products call the find_all() method of the returned object.
$full_product_list = $category->list_products()->find_all(); You can pass an array of options to the method. The supported options are sorting, manufacturer_url_name and apply_top_products. By default the product list is sorted by product name. You can sort products by another field. Also, you can sort the product list by multiple fields. $product_list = $category->list_products(array( 'sorting'=>array('price', 'name') ));
$product_list = $category->list_products(array( 'sorting'=>array('price desc') )); Note that if there are top products defined for the category, they are always returned in the beginning of the list, and their order is independent on the sorting parameters you specify in the method call. You can disable the top products feature and display them on their normal positions by passing FALSE value to the apply_top_products option. $product_list = $category->list_products(array( 'sorting'=>array('price desc'), 'apply_top_products'=>false )); $product_list = $category->list_products(array( 'manufacturer_url_name'=>'lemonstand_ecommerce_inc' )); $product_num = $category->list_products()->requestRowCount(); ¶ page_url() methodpublic string page_url(string $base_url)
Returns the category page URL, based on the URL passed in the parameter.
Use this method to create links to categories. If there is no custom page assigned to a category,
this method just adds the category URL name to the base URL specified in the parameter. So, if you passed the category string
to the method, it would return strings like /category/computers or /category/monitors. For categories which have a
custom page assigned and no URL Name assigned, the method returns the custom page URL. For categories with both a custom page and URL Name
specified, the method returns an URL of the custom page plus the value of the URL Name parameter: /custom_page/url_name .
This method is proxiable. Usage example:
<a href="<?= $category->page_url('/category') ?>"><?= $category->name ?></a> See Also |