Hi,
I set up a discount which should automatically add to cart a free item B when item A is added to cart, using the checkbox "Add "Discounted products" to cart.". This functionality was added here:
http://drupal.org/node/1220388
If I add both products to cart, the discount is correctly applied, but if I add product A to cart, product B is not automatically added to the cart.
I have been debugging the call to uc_cart_add_item() in uc_discounts_init(), and product B is properly added to the cart in
// If the item isn't in the cart yet, add it.
if (is_null($item) || $item === FALSE) {
db_query("INSERT INTO {uc_cart_products} (cart_id, nid, qty, changed, data) VALUES ('%s', %d, %d, %d, '%s')", $cid, $node->nid, $qty, time(), serialize($data));
if ($msg) {
drupal_set_message(t('<strong>@product-title</strong> added to <a href="!url">your shopping cart</a>.', array('@product-title' => $node->title, '!url' => url('cart'))));
}
}With 'properly added to cart' I mean I see a new row in table uc_cart_products ), but when the cart is finally displayed on browser it doesn't appear, and that row has disappeared from uc_cart_products.
Alternatively, if I change the $rebuild parameter in the call to uc_cart_add_item() to true, the problem seems to be solved.
Am I the only one having this problem? I have a few other Ubercart contrib modules installed on my site but I am not sure if this $rebuild trick could break something on Ubercart or is safe to do it, so it could be changed in uc_discounts_alt module.
This is my first issue sent to Drupal site, so please don't be hard on me if I did something wrong.
Marc ///
Comments
Comment #1
cedric_aI had the same problem, then I read comment #11 http://drupal.org/node/1220388#comment-4863086 and understood that the feature we're expecting don't exist yet : at least one 'required product' has to be set.
Comment #2
BigMike commentedI am also looking for this.
Our scenario:
Customer adds product to her cart which meets the requirement for the discount.
When she applies the discount code, we need a product to be magically added to the cart for free.
Currently, one has to first add this "free" product manually to her cart, then apply the discount code which can be configured to subtract the same amount as the product from the order (thereby the product becomes "free"). But it would be great if we could just enter a discount code that it itself inserts a product of value "$0" to the order (the best method to offer a free product if some condition is met).
I've searched around and cannot find this being done yet....
BigMike
Comment #3
hockey2112 commentedI, too, am interested in this...
Customer adds "Product A" to the cart, "Product B" automatically gets added "free". No discount code required.
As of right now, the customer needs to manually add Product B to get it for free (after already adding Product A. I do have Product A set as the "Required Products"... seems like it should be working, unless the functionality is not truly complete in this module.
Anyone have a fix?
Comment #4
stewart.adam commentedConfirmed that setting $rebuild to TRUE fixes the problem. You can easily achieve this by modifying this line in uc_discounts_init():
uc_cart_add_item($p['nid'], $p['qty'], $p['data'] + module_invoke_all('add_to_cart_data', $p), NULL, FALSE, FALSE, FALSE);Change it so that the last parameter to uc_cart_add_item(), $rebuild, is TRUE:
uc_cart_add_item($p['nid'], $p['qty'], $p['data'] + module_invoke_all('add_to_cart_data', $p), NULL, FALSE, FALSE, TRUE);I don't think this is harmful. You'll can see in the code for uc_cart_get_contents(), all $rebuild does is call uc_cart_get_contents() with $action set to 'rebuild'. The API for uc_cart_get_contents() say this about its $action parameter:
So that is exactly what we want and need to do.
Comment #5
budalokko commentedMy shop has been in production without problems with the change stated by #4 for the last 4 months.
#2: Maybe the adequate path for you is to apply #4 and use a codeless discount.
Comment #6
BigMike commentedThank you stewart.adam,
I just made the change and will be trying it out with our upcoming Labor Day sales event (we haven't used a discount code in a while now). Thank you very much for your time in trouble shooting and solving this!
ps. The line to be modified is found at line #118 of uc_discounts_alt\uc_discounts\uc_discounts.module
Cheers,
BigMike
Comment #7
dsobon commentedThis problem is still not solved (or I am doing something completely wrong)
Problem #1
---
Free item discount is not applied when the following is not set:
Edit discount rule => Conditions of Qualification => Required products => Filter Type => "No specific product"
Why is this a problem? no form validation errors. And most people assume no filter means dont filter-out any items.
Setting to "Filter by Class" => Product solves one part of the problem.
Problem #2
---
This code makes absolutely no sense, especially $times_to_apply:
times_to_apply can be simplified (but is still wrong) to: $added_qty / $qualifying_amount as $target_qty is cancelled out by eachother.
In my case, discount_amount is set to 1, and qualifying_amount set to $50
What I dont understand is why discount quantity is mixed with qualifying amounts... when qualifying amount should not even be involved with the end calculation.
my "solution" was to add the following code:
Comment #8
BigMike commentedIn regards to my reply #6, following #4 doesn't work when a discount code is used.
Alas, the help text for the "Add "Discounted products" to cart" checkbox includes, "Does not work with discounts that requires a Code."
I am looking into adding this functionality (as described in my reply #2) and will share if I can figure it out.
Thanks
Mike
Comment #9
BigMike commentedI have discovered a glitch/limitation. Using v6.x-2.4
When using either "Filter By Products" or "Filter By SKUs" and many products are selected, there seems to be a limit with the form of only 971 products that may be selected at the same time. I can highlight many more than this, but when I submit the changes to my discount rule, only a small percentage of the products I highlighted return as highlighted.
Looking at the database table `uc_discounts_products`, if I go one product over 971 then it ignores that product and returns the discount edit page with only 971 highlighted (I have verified this number by searching the db table using the discount_id I am working with).
We have over 5,000 products and a discount I am working on needs to apply to about 3,000 of these.
Is this a server load balancing issue? Can someone else with a large store confirm this limitation?
Thanks
Mike
UPDATE 1: I am planning to manually enter the products into the table but there is a column for "discount_product_id" that I can't tell if its used anywhere (I can't find any of the code utilizing it. Maybe its used for reports?). I see in the install file that this column is added as type "serial", so it seems that this is simply an auto-incrementing index number that maybe has a planned future use...
UPDATE 2: I successfully manually entered a few products being sure to properly increment the discount_product_id column and have confirmed that both A) the added products are now highlighted at the edit page of my discount rule, and B) my discount code is accepted for these manually entered products during checkout. Success!
UPDATE 3: I also confirmed that if you unselect all products in the form and submit, the manually added products are also removed from the discount code. So the system fully recognizes manual db entries.
Comment #10
ouissla commentedAs said in #7 I really don't understand the calculation of $time_to_apply.
Let's say I add my items with $qty = 2.
My discount is "buy 2 get 1 free" so I have to following values:
I'm expecting $times_to_apply to be 1. But with the module's formula it gives me 0.666666 so no product is added to the cart.
I hope this makes sense, my brain is freezing!
Anyone thinking the same and coming with a solution?