Special Page Block

I have a page which shows products based on a specified category. I utilize the Products (product showcase) block to accomplish this.

I would like to take this a step further. I'd like to only show products assigned to a specified user group. Is this possible? It can either be simple where you enter the user group ID or select from a list of user groups.

If it is possible, how would I accomplish it? I'm not an expert in custom configuration, so please provide some detail.

If it is not directly feasible, is there a way to add an entirely new block to build on?

Or do you have to implement this via an add-on?

Thank you!

But if you assign products to specific user group, customers without this group will not see the block, right? Or you want to show products even they are not available for the customers?

But if you assign products to specific user group, customers without this group will not see the block, right? Or you want to show products even they are not available for the customers?

That is true, but we are not using it as a traditional customer based cart. For us, we will need to utilize the user group product assignments.

You should be able to create a function (I.e. products_by_group((array)$group_ids, (array)$categories=array()); ) where this would return a list of products from the specified categories (or all if empty) that are available only for one or more of the group_ids in $group_ids. This function would return the list of product_ids that would be applied to your template as the 'filling_type'. You would reference that filling_type in the block_manager schema.

You should be able to create a function (I.e. products_by_group((array)$group_ids, (array)$categories=array()); ) where this would return a list of products from the specified categories (or all if empty) that are available only for one or more of the group_ids in $group_ids. This function would return the list of product_ids that would be applied to your template as the 'filling_type'. You would reference that filling_type in the block_manager schema.

Thank you for your advice. I understood everything you said up until "that would be applied to your template as the 'filling_type'. You would reference that filling_type in the block_manager schema." As I referred to, I am not an expert at creating custom, ground up add-ons, blocks, etc for CS-Cart. I was hoping for way to copy the existing product showcase block, modify it to include an attribute for which user group to use and filter the products based on it.

Unfortaunately it's a bit too hard to describe the pieces versus just doing the work!

There's probably a way to include a selector or radio group of 'groups' in the standard 'products' block but I'm unsure how to add that selector without my having to dig through more code. Ecom may comment on that.

The 'product showcase' (products.tpl) block is a wrapper around the list_templates/products_list.tpl template.

To me, the easiest way for you to accomplish this (it is NOT the best way but it is the easiest) is to copy and modify the current products block to filter items by group. This will filter by the specified array of group_ids and if there are no matches it will return the original list.

You should copy the design/themes/[your theme]/templates/blocks/products/products.tpl file to a new name and create a language variable by the same name. I.e. products_by_gid' with a value of "Products filtered by group id". Name the template design/themes/[your_theme]/blocks/products/products_by_gid.tpl

Replace the line that says

{** block-description:products **}

with

{** block-description:products_by_gid **}

and the line that looks like:

products=$items

with

products=$items|filter_items_by_gid:[5,7]

Note that [5,7] is an array of group_ids you want to filter on. If you want to select the group_ids, then it gets more complicated and you'll have to create a schema to define how to get the group_ids.

You would then create the following function in app/my_changes/func.php

function filter_items_by_gid(&$items, $gid_array) {
  $filtered_items = array();
  foreach($items as $product_id => $item) {
    foreach($gid_array as $gid) {
      if( in_array($gid, $item['usergroup_ids']) ) {
        $filtered_items[$product_id] = $item;
        break;
      }
  }
  // if you want to use the original $items if the filter didn't catch anything, then
  // return $filtered_items ? $filtered_items : $items;
  // otherwise
  return $filtered_items;  // to return a possibly empty list
}

This should set the list of products to be displayed in the product_list.tpl template as only those products that are in the specified usergroups.

You will then need to find the definition of the 'products' block in app/schemas/block_manager/blocks.php. It will be a line that looks like:

'products' => array(

Copy that entire structure (up to the line that defines 'categories') to your clipboard and create a file

app/addons/my_changes/schemas/block_manager/blocks.post.php and paste what you copied above.

Modify the first line from

'products' => array(

to


and then at the end, remove the last ',' (comma) and replace it with a ';' (semicolon) and add

return $schema;

to the end of the file.

Hope this helps get you started.

Hope this helps get you started.

Tbirnseth,

Thank you very much, this is very helpful. I have done what you recommended, but how do I get the "products by gid" block to show up under "create a block" when I am trying to add a block to a layout?

If the schema was added properly (the lines related to $schema['products_by_gid'] = array() then after clearing the cache, it should show in the blocks page.