- Posts: 38
- Thank you received: 0
Product Database Query - Narrow the results
15 years 2 months ago #21991
by mrdan
Product Database Query - Narrow the results was created by mrdan
In the file / view product / listing_div, I wish to narrow the results of the category database query so that custom product fields are reflected in the query.
I want to modify the code that displays the products in a particular category (below). How can I narrow the products displayed based on a particular value of a custom product field (for instance, WHERE xyz = "abc")?:
foreach($this->rows as $row){
if (
?>
<div class="hikashop_product" style="width:<?php echo $width;?>%;">
<div class="hikashop_container">
<div class="hikashop_subcontainer">
<?php
$this->row =& $row;
$this->setLayout('listing_'.$this->params->get('div_item_layout_type'));
echo $this->loadTemplate();
?>
</div>
</div>
</div>
<?php if($current_column>=$columns){ ?>
<div style="clear:both"></div>
<?php
$current_column=0;
}
$current_column++;
}
I want to modify the code that displays the products in a particular category (below). How can I narrow the products displayed based on a particular value of a custom product field (for instance, WHERE xyz = "abc")?:
foreach($this->rows as $row){
if (
?>
<div class="hikashop_product" style="width:<?php echo $width;?>%;">
<div class="hikashop_container">
<div class="hikashop_subcontainer">
<?php
$this->row =& $row;
$this->setLayout('listing_'.$this->params->get('div_item_layout_type'));
echo $this->loadTemplate();
?>
</div>
</div>
</div>
<?php if($current_column>=$columns){ ?>
<div style="clear:both"></div>
<?php
$current_column=0;
}
$current_column++;
}
Please Log in or Create an account to join the conversation.
15 years 2 months ago - 15 years 2 months ago #22001
by nicolas
Replied by nicolas on topic Re: Product Database Query - Narrow the results
You can't do that in the view. You need to create a hikashop plugin and implement the method onBeforeProductListingLoad :
www.hikashop.com/support/documentation/6...ntation.html#content
That way, you can dynamically add filters to the products loading query.
www.hikashop.com/support/documentation/6...ntation.html#content
That way, you can dynamically add filters to the products loading query.
Last edit: 15 years 2 months ago by nicolas.
Please Log in or Create an account to join the conversation.
15 years 2 months ago #22005
by mrdan
Replied by mrdan on topic Re: Product Database Query - Narrow the results
Thanks for your quick response and help. I give it a whirl.
Please Log in or Create an account to join the conversation.
15 years 1 month ago #22061
by mrdan
Replied by mrdan on topic Re: Product Database Query - Narrow the results
I've added the plugin, but the filter is ignored on the front end and the back end throws an error:
Warning: Invalid argument supplied for foreach() in /home/.../administrator/components/com_hikashop/views/dashboard/view.html.php on line 423
I don't think I'm constructing the filter value properly. Can you advise?
Here is plugin:
<?php
defined('_JEXEC') or die('Restricted access');
?>
<?php
class plgHikashopCategoryFilters extends JPlugin{
function plgHikashopCategoryFilters(&$subject, $config){
parent::__construct($subject, $config);
}
function onBeforeCategoryListingLoad(&$filters){
$ageval = $_POST;
$locval = $_POST;
$typeval = $_POST;
$filters[0] = "(age = '$ageval') AND (type = '$typeval') AND (location = '$locval')";
//echo $filters[0];
return true;
}
}
Warning: Invalid argument supplied for foreach() in /home/.../administrator/components/com_hikashop/views/dashboard/view.html.php on line 423
I don't think I'm constructing the filter value properly. Can you advise?
Here is plugin:
<?php
defined('_JEXEC') or die('Restricted access');
?>
<?php
class plgHikashopCategoryFilters extends JPlugin{
function plgHikashopCategoryFilters(&$subject, $config){
parent::__construct($subject, $config);
}
function onBeforeCategoryListingLoad(&$filters){
$ageval = $_POST;
$locval = $_POST;
$typeval = $_POST;
$filters[0] = "(age = '$ageval') AND (type = '$typeval') AND (location = '$locval')";
//echo $filters[0];
return true;
}
}
Please Log in or Create an account to join the conversation.
15 years 1 month ago - 15 years 1 month ago #22063
by nicolas
Replied by nicolas on topic Re: Product Database Query - Narrow the results
That's indeed not how you should do it.
First you need to filter the variables from the POST. Otherwise, your website could be easily hacked. Second, you should not change the filters already in the $filters variable but ADD new filters.
Instead of:
$filters[0] = "(age = '$ageval') AND (type = '$typeval') AND (location = '$locval')";
you should do:
$db =& JFactory::getDBO();
$filters[] = "age = ".$db->Quote($ageval);
$filters[] = "type = ".$db->Quote($typeval);
$filters[] = "location = ".$db->Quote($locval);
First you need to filter the variables from the POST. Otherwise, your website could be easily hacked. Second, you should not change the filters already in the $filters variable but ADD new filters.
Instead of:
$filters[0] = "(age = '$ageval') AND (type = '$typeval') AND (location = '$locval')";
you should do:
$db =& JFactory::getDBO();
$filters[] = "age = ".$db->Quote($ageval);
$filters[] = "type = ".$db->Quote($typeval);
$filters[] = "location = ".$db->Quote($locval);
Last edit: 15 years 1 month ago by nicolas. Reason: quotes issue corrected
Please Log in or Create an account to join the conversation.
15 years 1 month ago #22066
by mrdan
Replied by mrdan on topic Re: Product Database Query - Narrow the results
Thanks for the help.
I changed it to this but it still does not seem to have any effect on front end and still get same warning on back end. These are custom fields defined for the product. Any ideas? :
<?php
defined('_JEXEC') or die('Restricted access');
?>
<?php
class plgHikashopCategoryFilters extends JPlugin{
function plgHikashopCategoryFilters(&$subject, $config){
parent::__construct($subject, $config);
}
function onBeforeCategoryListingLoad(&$filters){
if(isset($_POST)) { $age = stripslashes($_POST); } else { $age = "All";};
if(isset($_POST)) { $location = stripslashes($_POST); } else { $location = "All";};
if(isset($_POST)) { $type = stripslashes($_POST); } else { $type = "All";};
$db =& JFactory::getDBO();
$filters[] = "age = '".$db->Quote($age)."'";
$filters[] = "type = '".$db->Quote($type)."'";
$filters[] = "location = '".$db->Quote($location)."'";
echo $filters[0];
echo $filters[1];
echo $filters[2];
echo $filters[3];
echo $filters[4];
return true;
}
}
I changed it to this but it still does not seem to have any effect on front end and still get same warning on back end. These are custom fields defined for the product. Any ideas? :
<?php
defined('_JEXEC') or die('Restricted access');
?>
<?php
class plgHikashopCategoryFilters extends JPlugin{
function plgHikashopCategoryFilters(&$subject, $config){
parent::__construct($subject, $config);
}
function onBeforeCategoryListingLoad(&$filters){
if(isset($_POST)) { $age = stripslashes($_POST); } else { $age = "All";};
if(isset($_POST)) { $location = stripslashes($_POST); } else { $location = "All";};
if(isset($_POST)) { $type = stripslashes($_POST); } else { $type = "All";};
$db =& JFactory::getDBO();
$filters[] = "age = '".$db->Quote($age)."'";
$filters[] = "type = '".$db->Quote($type)."'";
$filters[] = "location = '".$db->Quote($location)."'";
echo $filters[0];
echo $filters[1];
echo $filters[2];
echo $filters[3];
echo $filters[4];
return true;
}
}
Please Log in or Create an account to join the conversation.
15 years 1 month ago #22076
by nicolas
Replied by nicolas on topic Re: Product Database Query - Narrow the results
That seems fine. Is your plugin's xml file correct ? Did you publish the plugin in joomla ? (the warning on the back end is not related. Could you copy/paste here the line 423 of the file administrator/components/com_hikashop/views/dashboard/view.html.php ? )
Please Log in or Create an account to join the conversation.
15 years 1 month ago - 15 years 1 month ago #22083
by mrdan
Replied by mrdan on topic Re: Product Database Query - Narrow the results
HI,
Here is message in back end when clicking on Components Hikashop (message goes away when I disable the plugin):
a.category_parent_id = 4age = ''All''type = ''All''location = ''All''trans =
Warning: Invalid argument supplied for foreach() in /administrator/components/com_hikashop/views/dashboard/view.html.php on line 423
Warning: Invalid argument supplied for foreach() in /administrator/components/com_hikashop/views/dashboard/view.html.php on line 423
Warning: Invalid argument supplied for foreach() in /administrator/components/com_hikashop/views/dashboard/view.html.php on line 423
Warning: Invalid argument supplied for foreach() in /administrator/components/com_hikashop/views/dashboard/view.html.php on line 423
Warning: Invalid argument supplied for foreach() in /administrator/components/com_hikashop/views/dashboard/view.html.php on line 423
Warning: Invalid argument supplied for foreach() in /administrator/components/com_hikashop/views/dashboard/view.html.php on line 423
a.category_parent_id = 4age = ''All''type = ''All''location = ''All''trans =
Here is XML file, maybe I'm missing something:
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE install SYSTEM " dev.joomla.org/xml/1.5/plugin-install.dtd ">
<install type="plugin" version="1.5" method="upgrade" group="hikashop">
<name>Hikashop Category Filters Plugin</name>
<creationDate>July 2011</creationDate>
<version>1.0.0</version>
<author>Sullivan</author>
<authorEmail>info@gophercreative.com
<authorUrl> www.gophercreative.com
<copyright>Copyright (C) 2011</copyright>
<license> www.gnu.org/licenses/gpl-2.0.html GNU/GPL
<description>This plugin adds extra filters to category selection</description>
<files>
<filename plugin="categoryfilters">categoryfilters.php</filename>
</files>
<params addpath="/components/com_hikashop/params">
</params>
</install>
Here is message in back end when clicking on Components Hikashop (message goes away when I disable the plugin):
a.category_parent_id = 4age = ''All''type = ''All''location = ''All''trans =
Warning: Invalid argument supplied for foreach() in /administrator/components/com_hikashop/views/dashboard/view.html.php on line 423
Warning: Invalid argument supplied for foreach() in /administrator/components/com_hikashop/views/dashboard/view.html.php on line 423
Warning: Invalid argument supplied for foreach() in /administrator/components/com_hikashop/views/dashboard/view.html.php on line 423
Warning: Invalid argument supplied for foreach() in /administrator/components/com_hikashop/views/dashboard/view.html.php on line 423
Warning: Invalid argument supplied for foreach() in /administrator/components/com_hikashop/views/dashboard/view.html.php on line 423
Warning: Invalid argument supplied for foreach() in /administrator/components/com_hikashop/views/dashboard/view.html.php on line 423
a.category_parent_id = 4age = ''All''type = ''All''location = ''All''trans =
Here is XML file, maybe I'm missing something:
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE install SYSTEM " dev.joomla.org/xml/1.5/plugin-install.dtd ">
<install type="plugin" version="1.5" method="upgrade" group="hikashop">
<name>Hikashop Category Filters Plugin</name>
<creationDate>July 2011</creationDate>
<version>1.0.0</version>
<author>Sullivan</author>
<authorEmail>info@gophercreative.com
<authorUrl> www.gophercreative.com
<copyright>Copyright (C) 2011</copyright>
<license> www.gnu.org/licenses/gpl-2.0.html GNU/GPL
<description>This plugin adds extra filters to category selection</description>
<files>
<filename plugin="categoryfilters">categoryfilters.php</filename>
</files>
<params addpath="/components/com_hikashop/params">
</params>
</install>
Last edit: 15 years 1 month ago by mrdan.
Please Log in or Create an account to join the conversation.
15 years 1 month ago #22085
by nicolas
Replied by nicolas on topic Re: Product Database Query - Narrow the results
The problem might be the quotes.
Please try with :
$filters[] = "age = ".$db->Quote($ageval);
$filters[] = "type = ".$db->Quote($typeval);
$filters[] = "location = ".$db->Quote($locval);
instead of :
$filters[] = "age = '".$db->Quote($age)."'";
$filters[] = "type = '".$db->Quote($type)."'";
$filters[] = "location = '".$db->Quote($location)."'";
Please try with :
$filters[] = "age = ".$db->Quote($ageval);
$filters[] = "type = ".$db->Quote($typeval);
$filters[] = "location = ".$db->Quote($locval);
instead of :
$filters[] = "age = '".$db->Quote($age)."'";
$filters[] = "type = '".$db->Quote($type)."'";
$filters[] = "location = '".$db->Quote($location)."'";
Please Log in or Create an account to join the conversation.
15 years 1 month ago #22087
by mrdan
Replied by mrdan on topic Re: Product Database Query - Narrow the results
Same thing except now values displayed with echo command have single quotes around them instead of double quotes.
Please Log in or Create an account to join the conversation.
15 years 1 month ago #22089
by nicolas
Replied by nicolas on topic Re: Product Database Query - Narrow the results
Wait, the function you're implementing is onBeforeCategoryListingLoad, but on the link you gave, I only see products, no categories... That's why it's not working. You should implement onBeforeProductListingLoad as I said in my first post.
Please Log in or Create an account to join the conversation.
15 years 1 month ago #22091
by mrdan
Replied by mrdan on topic Re: Product Database Query - Narrow the results
Thanks very much for finding my mistake. Works great now!
Please Log in or Create an account to join the conversation.
Time to create page: 0.272 seconds