Great add-on for a great module. Has anyone tried filtering out items which are not in stock? I've check the code and it didn't seem obvious to me. It would be great if this were a checkbox in the admin section for Upsell.

Thanks in advance!

Comments

asanvicente1’s picture

Forgot to mention, I use the Stock module to track stock levels.

torgospizza’s picture

Hey, sorry for the delay. I haven't looked into this and I'm not sure what it would entail. Probably a simple addition to the SQL query if the Stock module is enabled. Can you roll a patch? If not I can look at this later.

drupalgideon’s picture

Subscribe

Bruno-2M’s picture

Thanks for this great module.
I'm interested also in this feature for both products and cart pages, the best would be a check box in the admin section of the module
Where stock.uc_product_stock > 0

Any help is welcome
Thanks

summit’s picture

Subscribing, needing same functionality, greetings, Martijn

ColinMctavish’s picture

subscribing

asanvicente1’s picture

Assigned: Unassigned » asanvicente1
Status: Active » Needs review

I finally ended up solving this for our site. (UC Upsell version 6.x-1.21). Now, only related items in stock appear in Upsell blocks. Two MySQL query changes to the uc_upsell_core.inc file in your uc_upsell folder are required.

NOTE: BE SURE to backup before making these changes, and DO NOT test this on a live site.

1. Open the /uc_upsell/uc_upsell_core.inc file and comment out this line (near line 155):

$related = db_result(db_query("SELECT `related_products` FROM {uc_upsell_products}
                WHERE nid = %d", $nid));

Beneath it, paste this:

$related = db_result(db_query("SELECT `related_products` FROM {uc_upsell_products}
                INNER JOIN uc_product_stock ON uc_product_stock.nid = node.nid</strong>
                WHERE nid = %d", $nid AND uc_product_stock.stock > 0));

2. Near line 208, comment out this line:

$randoms = db_query("SELECT `nid`
                FROM {node}
                WHERE `status` = 1
                AND `type` IN ('". implode("','", uc_product_product_types()) ."')
                AND NOT nid IN (". $filter_query .")
                ORDER BY RAND()");

and replace it with this:

$randoms = db_query("SELECT *
                FROM node  
                INNER JOIN uc_product_stock ON uc_product_stock.nid = node.nid              
                WHERE node.status = 1 
                AND node.type IN ('". implode("','", uc_product_product_types()) ."') 
                AND NOT node.nid IN (". $filter_query .") 
                AND uc_product_stock.stock > 0 
                ORDER BY RAND()");

After you clear your site's cache, you should see your Upsell results filtered.

Good luck!

wis1’s picture

I have:

user warning: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '/strong> WHERE nid = 0' at line 2 query: SELECT `related_products` FROM uc_upsell_products INNER JOIN uc_product_stock ON uc_product_stock.nid = node.nid WHERE nid = 0 in /my_site/all/modules/uc_upsell/uc_upsell_core.inc on line 158

how to resolve this error?

ratinakage’s picture

Any update on this?

mitrpaka’s picture

Version: 6.x-1.21 » 6.x-1.x-dev
Assigned: asanvicente1 » Unassigned
StatusFileSize
new3.13 KB

Changes provided in #7 applies for related and random nodes only, but doesn't prevent showing up associated items not in stock when related products are searched based on taxonomy (Handle Related Products based on Taxonomy -setting either set to 'Override' or 'Add')

Attached patch takes related products based on taxonomy into account as well.

mitrpaka’s picture

And here is more generic patch to this - Checkbox added to Global Settings whether filter out items not in stock as suggested originally by asanvicente1.

ermannob’s picture

Patch at #11 works for me.

Thanks!

iprosp’s picture

Patch #11 works if all your products have stock enabled. Every product that doesn't have stock enabled will never show up in the block.

$related = db_result(db_query("SELECT up.related_products
FROM {uc_upsell_products} up
JOIN {uc_product_stock} ps ON up.nid = ps.nid
WHERE up.nid = %d and ps.stock > 0", $nid)); 
reinvented’s picture

Status: Needs review » Needs work

The patches above appear to work properly for random association and for taxonomy-based selection, but for specific-product associations they modify the query as follows:

$related = db_result(db_query("SELECT `related_products` FROM {uc_upsell_products} WHERE nid = %d", $nid));

to:

$related = db_result(db_query("SELECT up.related_products
            FROM {uc_upsell_products} up
            INNER JOIN {uc_product_stock} ps ON up.nid = ps.nid
            WHERE up.nid = %d AND ps.stock > 0", $nid));    

which does not solve the original issue because it only filters out parent products that are out of stock when what we want to do is filter out child products that are out of stock.

For example, I have a product of with nid of 52659; the resulting query is:

SELECT up.related_products FROM uc_upsell_products up INNER JOIN uc_product_stock ps ON up.nid = ps.nid WHERE up.nid = 52659 AND ps.stock > 0

which returns:

a:6:{i:36703;s:5:"36703";i:52761;s:5:"52761";i:52671;s:5:"52671";i:52672;s:5:"52672";i:52673;s:5:"52673";i:52955;s:5:"52955";}

However one of those product nids returned -- 36703 -- has no stock, and there's nothing in the query modification that filters out based on this fact.

reinvented’s picture

I quick hack that got this working for specific-product associations in our situation (where we have stock-keeping turned on for all products) was to replace, in the patch:

$related = db_result(db_query("SELECT up.related_products
            FROM {uc_upsell_products} up
            INNER JOIN {uc_product_stock} ps ON up.nid = ps.nid
            WHERE up.nid = %d AND ps.stock > 0", $nid));

with:

$related = db_result(db_query("SELECT `related_products` FROM {uc_upsell_products} WHERE nid = %d", $nid));       
if ($related && is_array(unserialize($related))) {
  $related = unserialize($related);
  $related = db_query("SELECT nid FROM {uc_product_stock} WHERE stock > 0 and nid IN ('". implode("','", $related) ."')");    
  $nids = array();
  while ($result = db_fetch_object($related)) {
    $nids[$result->nid] = $result->nid;
  }
  $related = serialize($nids);
}