News:

You may pay someone to create your store, or you visit our seminar and become a professional yourself with the silver certification

Main Menu

Custom plugin for filtering on custom field(s)

Started by panoss, December 22, 2017, 22:56:23 PM

Previous topic - Next topic

panoss

(I suppose there is no embeded possibility for filtering on custom fields, right?)

I have a custom field named 'type'.

A form has this action: action="/times/index.php/en/component/virtuemart/search?Itemid=<?php echo $activemenu_Itemid; ?>" method="get"
So, when the submit button of this form is clicked, the category view is displayed(\components\com_virtuemart\views\category\tmpl\default.php).
And the plgVmBeforeProductSearch trigger is triggered. (this is why I load the category view, for this trigger to be triggered. I 'm not sure this is the correct approach. :-\)

So I 'm trying to make a custom vm plugin for filtering on custom fields.
My plugin extends vmCustomPlugin.
In trigger 'plgVmBeforeProductSearch':
public function plgVmBeforeProductSearch(&$select, &$joinedTables, &$where, &$groupBy, &$orderBy, &$joinLan) {
...changing the value of variables, does change the $q (variable for the query) in line 830-832 of vmmodel(\administrator\components\com_virtuemart\helpers\vmmodel.php).
BUT the result of the query does not change! (returns all products)
What am I doing wrong?
Virtuemart 3.2.4 on Joomla! 3.8.0

Studio 42

YOu need to add your own query to
$joinedTables and &$where
see vmmodel how it's done in the core

panoss

Quote from: Studio 42 on December 23, 2017, 16:28:05 PM
YOu need to add your own query to
$joinedTables and &$where
see vmmodel how it's done in the core
Yes, this is what I did, the query DOES change (I print it (with var_dump at line 834 of vmmodel)), but the result in the screen does not change!
It always fetches all records!
Virtuemart 3.2.4 on Joomla! 3.8.0

Studio 42

Have you check if your code is called.
Add for eg jexit('I am called'); in our plgVmBeforeProductSearch function

panoss

#4
Quote from: Studio 42 on December 23, 2017, 21:15:39 PM
Have you check if your code is called.
Add for eg jexit('I am called'); in our plgVmBeforeProductSearch function
I tried this and the code is called.

I think the problem lays in form 's action:
Quote from: panoss on December 22, 2017, 22:56:23 PM
A form has this action: action="/times/index.php/en/component/virtuemart/search?Itemid=<?php echo $activemenu_Itemid; ?>" method="get"
I 'll try some changes in it.
Virtuemart 3.2.4 on Joomla! 3.8.0

panoss

#5
I 'm stuck, any help is welcome. :-\
I don't know which view I should use, the category view or some other view?

Edit:
I made some progress, I commented out line 726 (at VirtueMartModelProduct):
if ($this->searchplugin !== 0){
...and of course and line 730:
}

Now seems to work!
(strange, the function plgVmBeforeProductSearch get called in either case, with these lines commented out or not, but the result is correct onle when commented out!)

So, would it be normal if I left these lines commented out or this might cause me some problem?
Virtuemart 3.2.4 on Joomla! 3.8.0

Studio 42

You need to set a customfield_parent id
$active_category_id = vRequest::getInt('virtuemart_category_id', '0');
$link = JRoute::_('index.php?option=com_virtuemart&view=category&virtuemart_category_id='.$active_category_id.'&custom_parent_id='.$custom_id);

It work without any changes in the core using this trick
And add the values in your form

balai


panoss

Quote from: Studio 42 on December 25, 2017, 11:32:17 AM
You need to set a customfield_parent id
$active_category_id = vRequest::getInt('virtuemart_category_id', '0');
$link = JRoute::_('index.php?option=com_virtuemart&view=category&virtuemart_category_id='.$active_category_id.'&custom_parent_id='.$custom_id);

It work without any changes in the core using this trick
And add the values in your form
Both variables ($active_category_id and $custom_id) are null, so it doesn't work if I don't comment out the above mentioned lines.
Virtuemart 3.2.4 on Joomla! 3.8.0

Studio 42

custom_id is a value you have to set yourself in your plugin
Category can be empty,in this case, it display product from all categories.

panoss

Virtuemart 3.2.4 on Joomla! 3.8.0

Studio 42

Quote from: panoss on December 22, 2017, 22:56:23 PM
I have a custom field named 'type'.

A form has this action: action="/times/index.php/en/component/virtuemart/search?Itemid=<?php echo $activemenu_Itemid; ?>" method="get"

So I 'm trying to make a custom vm plugin for filtering on custom fields.

This custom field have a virtuemart_custom_id. So you need to set this so VM know that you want filter a customfield

panoss

#12
Quote from: Studio 42 on December 26, 2017, 18:30:39 PM
Quote from: panoss on December 22, 2017, 22:56:23 PM
I have a custom field named 'type'.

A form has this action: action="/times/index.php/en/component/virtuemart/search?Itemid=<?php echo $activemenu_Itemid; ?>" method="get"

So I 'm trying to make a custom vm plugin for filtering on custom fields.

This custom field have a virtuemart_custom_id. So you need to set this so VM know that you want filter a customfield
What do you mean I have 'to set this'?
My custom field has a virtuemart_custom_id = 3.
I inserted in my url 'customfields[3]' and 'virtuemart_custom_id=3': index.php/en/filters-en-gb?customfields[3]=Fashion&virtuemart_custom_id=3.

It's not working.
Virtuemart 3.2.4 on Joomla! 3.8.0

Studio 42

You need to use &custom_parent_id=3 not customfields[3]=Fashion in the URL
I now it's not logic, but if no custom_parent_id is set, then the filter is not applied

panoss

Studio 42 it works perfectly the way you suggested!
Thank you very much (also thanks to everybody who answered).
Virtuemart 3.2.4 on Joomla! 3.8.0