LemonStand Forum: Filtering products by a custom field - LemonStand Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Filtering products by a custom field

#1 User is offline   chris*grafik 

  • LemonFan
  • PipPipPipPipPip
  • Group: Members
  • Posts: 56
  • Joined: 08-October 10

Posted 18 March 2012 - 09:31 PM

Hi

We have a custom field on our products on a clothing, where site admin is required to select xs,s, m, l or xl.

xs = size 8, s = 10, m = 12, l = 14 and xl = 16+.

We then have a page for each of 8, 10, 12, 14 and 16+.

We then filter each of these pages using the following code (developed by Aleksey, thanks!):

<?php
  
$size = 'l';

$products = Shop_Product::create()->apply_filters()->order('created_at desc');
$products->apply_visibility()->apply_customer_group_visibility();
$products->where('
                ((shop_products.enabled=1 and (disable_completely is null or disable_completely=0) and not (
                    shop_products.track_inventory is not null and shop_products.track_inventory=1 and shop_products.hide_if_out_of_stock is not null and shop_products.hide_if_out_of_stock=1 and ((shop_products.stock_alert_threshold is not null and shop_products.in_stock <= shop_products.stock_alert_threshold) or (shop_products.stock_alert_threshold is null and shop_products.in_stock=0))
                    )) or exists(select * from shop_products grouped_products where grouped_products.product_id is not null and grouped_products.product_id=shop_products.id and grouped_products.enabled=1
                    and not (
                        grouped_products.track_inventory is not null and grouped_products.track_inventory=1 and grouped_products.hide_if_out_of_stock is not null and grouped_products.hide_if_out_of_stock=1 and ((grouped_products.stock_alert_threshold is not null and grouped_products.in_stock <= grouped_products.stock_alert_threshold) or (grouped_products.stock_alert_threshold is null and grouped_products.in_stock=0))
                )))');
$products->where('
    ((disable_completely is null or disable_completely=0) and product_id is null)
    or (
      product_id is not null and
      (select ifnull(parent.disable_completely, 0) from shop_products parent where parent.id=shop_products.product_id) = 0
    )
  ');
  

$products->where('x_product_size=?', $size);


  
$this->render_partial('shop:product_list', array(
  'products'=>$products,
  'paginate'=>false,
  'page_index'=>$this->request_param(1, 0),
)) 
  


?>


The issue we are having is that if ->apply_filters() is used, then the output does not display products where the master product has a stock level of 0. If we remove ->apply_filters(), then it shows all products for that size, regardless of stock level, as long as the master product has stock. If we use ->apply_filters() on size xl, then no products show at all.

Thanks for your help.
0

#2 User is offline   chris*grafik 

  • LemonFan
  • PipPipPipPipPip
  • Group: Members
  • Posts: 56
  • Joined: 08-October 10

Posted 19 March 2012 - 03:39 PM

I thought that this may have been down to user admin error, but it seems not. Any help would be most appreciated, thanks.
0

#3 User is offline   Aleksey 

  • Co-Founder
  • Group: +Administrators
  • Posts: 4,066
  • Joined: 31-October 09

Posted 19 March 2012 - 04:45 PM

Hi Chirs,

Please try to remove apply_filters(), apply_visibility(), apply_customer_group_visibility() and use apply_availability()

$products = Shop_Product::create()->apply_availability()->order('created_at desc');
$products->where('
...


If it works, try to add apply_visibility() and apply_customer_group_visibility() calls.

Thank you

#4 User is offline   chris*grafik 

  • LemonFan
  • PipPipPipPipPip
  • Group: Members
  • Posts: 56
  • Joined: 08-October 10

Posted 19 March 2012 - 07:26 PM

Hi Aleksey, thanks for your help.

When I attempted to use apply_availability, the system threw the following error: Method apply_availability is not defined in class Shop_Product.
0

#5 User is offline   Aleksey 

  • Co-Founder
  • Group: +Administrators
  • Posts: 4,066
  • Joined: 31-October 09

Posted 20 March 2012 - 03:55 PM

Hi Chris,

Oh, there is a typo in the method name. We will fix it in the future releases (the old method will still work). Please use the apply_avaliability() method.

Thank you

Share this topic:


Page 1 of 1

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users