Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

E-Commerce Modules /

Discuss about the various e-commerce modules available:
Ecommerce, SS Shop, SilverCart and SwipeStripe
Alternatively, have a look the shared mailinglist.

Moderators: martimiz, Nicolaas, Sean, Ed, frankmullenger, biapar, Willr, Ingo, Jedateach, swaiba

Front end CheckBox sorting


Go to End


10 Posts   5402 Views

Avatar
ambient

Community Member, 130 Posts

4 April 2012 at 9:18pm

Hi all,
I'm working on a site where I'm using the e-commerce module to display products in a shop and using the 'product groups' to help sort the products e.g. shirts, jackets etc.

Everything works fine so far, click on shirts, it shows me all the shirts, click on jackets, it shows me jackets etc.

What I want to do is for the user to be able to tick a box for each category to display them. e.g. tick shirts to see shirts , tick shirts and tick jackets to see shirts and jackets, etc.

I don't know where to begin with this and have not been able to find anything to help me in the forums so far.

Can anyone help me figure this one out?

Thanks guys

Avatar
Jedateach

Forum Moderator, 238 Posts

11 April 2012 at 1:47pm

Hi Ambient,

I would start by understanding the ProductGroup.php file, and classes it contains. Specifically the ProductsShowable function is what gets the products to show from the database, based on the current product group being viewed.

You'll want to customise that function, or somehow replicate it so that it includes products from multiple selected categories.

Take a look at this sub-module I created, which is similar to what you're wanting to achieve:
https://github.com/burnbright/silverstripe-shop-brandbrowsing

Hope that helps

Jeremy

Avatar
ambient

Community Member, 130 Posts

14 April 2012 at 4:05am

Hi Jeremy,
Having a bit of a rough time with all this :)

I've been looking at the function below like you said. What I have now is the 'FeaturedProduct' option activated in the sort menu (Sort by Alphabetical Lowest Price Most Popular Featured)

When I click on 'Featured' it sorts the products featured first followed by non featured.

If when I click on'Featured' I could get it to ONLY show featured products then I think I'd be well on my way to getting this solved.

Of course the reason I'm here is I haven't been able to do that myself. Do you know what I'd need to do to make that happen?

function ProductsShowable($extraFilter = '', $recursive = true){
		$filter = ""; //
		$join = "";
		
		$this->extend('updateFilter',$extraFilter);
		
		if($extraFilter) $filter.= " AND $extraFilter";
		if(self::$must_have_price) $filter .= " AND \"Price\" > 0";

		$limit = (isset($_GET['start']) && (int)$_GET['start'] > 0) ? (int)$_GET['start'].",".self::$page_length : "0,".self::$page_length;
		$sort = (isset($_GET['sortby'])) ? Convert::raw2sql($_GET['sortby']) : "\"FeaturedProduct\" DESC,\"Title\"";

		//hard coded sort configuration //TODO: make these custom
		if($sort == "NumberSold") $sort .= " DESC";


		$groupids = array($this->ID);

		if(($recursive === true || $recursive === 'true') && self::$include_child_groups && $childgroups = $this->ChildGroups(true))
			$groupids = array_merge($groupids,$childgroups->map('ID','ID'));

		$groupidsimpl = implode(',',$groupids);

		$join = $this->getManyManyJoin('Products','Product');
		$multicatfilter = $this->getManyManyFilter('Products','Product');

		//TODO: get products that appear in child groups (make this optional)

		$products = DataObject::get('Product',"(\"ParentID\" IN ($groupidsimpl) OR $multicatfilter) $filter",$sort,$join,$limit);

		$allproducts = DataObject::get('Product',"\"ParentID\" IN ($groupidsimpl) $filter","",$join);
		
		if($allproducts) $products->TotalCount = $allproducts->Count(); //add total count to returned data for 'showing x to y of z products'
		if($products && $products instanceof DataObjectSet) $products->removeDuplicates();
		return $products;
	}

Avatar
ambient

Community Member, 130 Posts

17 April 2012 at 11:57pm

No thoughts on this :(

If I can get the 'featured' items to show by themselves then I could change 'featured items' to 'shirts' and add new CMS fields for jackets etc. etc. (I can't use extra product groups to break these down as that will show all jackets instead of just the ones in the current section)

Is this a lot more complicated than I think?
Obviously I'm not a very strong programmer but it seems that if ProductGroups.php can already 'Sort' by featured items then to 'Only' show featured items should be just a few changes in the code? Or am I being naive :)

If i'm doing a horrible job of explaining this please let me know as my head is spinning right now ;)

Avatar
Jedateach

Forum Moderator, 238 Posts

23 April 2012 at 10:28am

Edited: 23/04/2012 10:31am

Hi Ambient,

Is there a way you can provide a visual mockup of what you are wanting to create?

From what I gather, you want to add an additional form to the product group pages, so you can filter what is being shown.
To create that form, you need to add a checkbox for every product group.
Take the form submission result, and create a filter (SQL where query), which you can probably just pass to ProductsShowable

Here's a rough attempt:


class FilterableProductGroup extends ProductGroup{}

class FilterableProductGroup_controller extends ProductGroup_controller{

//create form, to be included in template
function FilterForm(){
    $fields = new FieldSet(
        //add checkbox set field for every page group
);
    $actions = new FieldSet(new FormAction('savefilter','Filter'));
    return new Form($this,'')
}

//do the actual saving of the filter form
function savefilter($data,$form){
    //store filter in session
   //redirect back
}

//use this in template
function FilteredProducts(){
   $savedfilter = //create filter from saved session data

    return $this->ProductsShowable($savedfilter);

}

}

Alternatively (instead of subclassing ProductGroup) you could put this code in an Extension to extend ProductGroup_Controller, and just hook into the updateFilter to get the saved filter from the session.

Hope that helps.

Avatar
ambient

Community Member, 130 Posts

24 April 2012 at 5:38am

Hiya Jeremy,

I've attached 2 images.

Exhibit A is what I was hoping to achieve before I realised it would be way too complicated for me to figure out. It has multiple checkboxes that can be clicked on to filter products shown.

Exhibit B is what I would settle for if I could get it to work. It would be a dropdown where the user can refine by one item.

I played around duplicating and changing the Featured/Sort code in the Product.php and ProductGroup.php and it works to the extent of sorting socks first, or shirts first if chosen from the dropdown. But I can't get it to work by showing ONLY socks or ONLY shirts. So thats where I'm stuck right now.

I had a play around with the code you gave me but I'm not sure of a few things.

I created FilterableProductGroup.php and pasted your code in.

Do I need to add something to this line? $savedfilter = //create filter from saved session data

What would I write in the ProductGroup.ss template to call this function?

Attached Files
Avatar
Jedateach

Forum Moderator, 238 Posts

24 April 2012 at 8:31am

Hi Ambient,

What I've proposed is in line with your exhibit A.

$savedfilter should contain a string like "\"Product\".\"ParentID\" IN (12,25,36,2)" ...which is an additional filter that ensures the products shown are only those from groups which have ids given. The ids will be produced from the saved filter form submission. You just need to figure out how to save form data to the session, and then produce that list of ids from it.

In your ProductGroup.ss template, you'll need to replace 'Products' with 'FilteredProducts'. Or just copy the template to a FilterableProductGroup.ss template, and make that same change.

Avatar
ambient

Community Member, 130 Posts

25 April 2012 at 2:00am

Hi Jeremy,

I appreciate your help on this. Unfortunately figuring out the 'save form data to the session' stuff was well beyond my skillset :(

I do realise now that its a huge task, one thats hopefully in the pipeline for future releases of the ecommerce/shop modules.

I am still trying to figure out how to 'limit' rather than 'sort' the products when I click on e.g. 'featured'.

Is the answer to this staring me in the face? I create a new filter option, have the checkbox in the admin but still can't figure out how to show only those items instead of just displaying them first.

I duplicated and renamed the below function and any other relative code related to it and it does sort my new options but won't limit them.

Do I need to change 'sort' to 'limit' in some parts of it?

function SortLinks(){

		if(count(ListingGroup::get_sort_options()) <= 0) return null;



		$sort = (isset($_GET['sortby'])) ? Convert::raw2sql($_GET['sortby']) : "Title";

		$dos = new DataObjectSet();

		foreach(ListingGroup::get_sort_options() as $field => $name){

			$current = ($field == $sort) ? 'current' : false;

			$dos->push(new ArrayData(array(

				'Name' => $name,

				'Link' => $this->Link()."?sortby=$field",

				'Current' => $current

			)));

		}

		return $dos;

	}

I found this online which seems to be more in depth versions of the Product.php and ProductGroup.php files. would it be easier to do it using these versions?

Go to Top