Product Database Query - Narrow the results

  • Posts: 38
  • Thank you received: 0
12 years 8 months ago #21991

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++;
}

Please Log in or Create an account to join the conversation.

  • Posts: 81379
  • Thank you received: 13037
  • MODERATOR
12 years 8 months ago #22001

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.

Last edit: 12 years 8 months ago by nicolas.

Please Log in or Create an account to join the conversation.

  • Posts: 38
  • Thank you received: 0
12 years 8 months ago #22005

Thanks for your quick response and help. I give it a whirl.

Please Log in or Create an account to join the conversation.

  • Posts: 38
  • Thank you received: 0
12 years 8 months ago #22061

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;
}
}

Please Log in or Create an account to join the conversation.

  • Posts: 81379
  • Thank you received: 13037
  • MODERATOR
12 years 8 months ago #22063

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);

Last edit: 12 years 8 months ago by nicolas. Reason: quotes issue corrected

Please Log in or Create an account to join the conversation.

  • Posts: 38
  • Thank you received: 0
12 years 8 months ago #22066

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;
}
}

Please Log in or Create an account to join the conversation.

  • Posts: 81379
  • Thank you received: 13037
  • MODERATOR
12 years 8 months ago #22076

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.

  • Posts: 38
  • Thank you received: 0
12 years 8 months ago #22083

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>This email address is being protected from spambots. You need JavaScript enabled to view it.</authorEmail>
<authorUrl> www.gophercreative.com </authorUrl>
<copyright>Copyright (C) 2011</copyright>
<license> www.gnu.org/licenses/gpl-2.0.html GNU/GPL</license>
<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: 12 years 8 months ago by mrdan.

Please Log in or Create an account to join the conversation.

  • Posts: 81379
  • Thank you received: 13037
  • MODERATOR
12 years 8 months ago #22085

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 Log in or Create an account to join the conversation.

  • Posts: 38
  • Thank you received: 0
12 years 8 months ago #22087

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.

  • Posts: 81379
  • Thank you received: 13037
  • MODERATOR
12 years 8 months ago #22089

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.

  • Posts: 38
  • Thank you received: 0
12 years 8 months ago #22091

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.077 seconds
Powered by Kunena Forum