Discount by product type doesn't work in cart/checkout
| Project: | UC Discounts |
| Version: | 5.x-1.x-dev |
| Component: | Code |
| Category: | bug report |
| Priority: | critical |
| Assigned: | Unassigned |
| Status: | active |
Jump to:
This issue was copied from discount_product_type_works_when_viewing_not_cartcheckout
Original post (by frost):
Environment:
Drupal 5.8
Ubercart 1.2
Discount version: pcambra's version
Test: For users of a certain role, discount all products of a certain class
How I did it:
- Created a role “member” and added my user to the role
- Created a product class “Seeds”
- Created a product of class “seeds” called “test seeds”. Price set at 100.
- Created a discount called “memberdiscount” as follows:
- CONDITIONS: Userrole = member; Product type >= 1; Type Seeds
- ACTION: Discount all products of a type (seeds) from order, qty = 1, amount = 10%
Behaviour:
- 10% Discount appears when viewing product
- When product is added to cart, Full price is used.
- When cart is checked out, full price is used, with no discount details of any kind shown
Debugging:
When viewing the product node:
uc_discounts_product_discount_price creates a "pseudo shopping cart" of a single item, the current node. The product details for this include the product's type (ie class). This pseudo shoppping cart is passed into uc_discounts_product_class_check, which correctly returns that the conditions are satisfied.
When adding product to cart:
uc_discounts_product_discount_price is called, but this time the product is not a full node, and the product type information is not available. This causes the uc_discounts_product_class_check to fail, so the discount isn't applied.
--
+1 for someone who is familiar with Discount code to take over maintenance.
I'll provide a small bounty ($100) for the new maintainer once he/she takes over, establishes a single codestream, and fixes this particular bug.
Comment (by frost):
I've dug into the code and believe I have identified the issue, and have applied a fix for the incorrect behaviour. The problem was that products in the cart did not have type information, and so discounts by type weren't being triggered when checking out.
Since there is no maintainer and no single codestream, I will just post a description of what I did.
Although I'm withdrawing my bounty for the bug fix, I will still offer a $50 bounty if someone comes forward and starts maintaining this module, as it is an important feature for Ubercart, and the lack of it almost sent me off using e-commerce.
CODE CHANGES
All changes were made within the uc_discounts.module file.
In function uc_discounts_product_discount_price:
Original:
// to use same code as above function, create an pseudo shopping cart
// array from the specified product
$cart_copy = array();
$prod_proto = drupal_clone($product);
$prod_proto->cart_id = 0;
$prod_proto->qty = 1;
$prod_proto->data = array();
$prod_proto->price = $product->sell_price;
$cart_copy[] = $prod_proto;Changed version:
// to use same code as above function, create an pseudo shopping cart
// array from the specified product
$cart_copy = array();
$prod_proto = drupal_clone($product);
$prod_proto->cart_id = 0;
$prod_proto->qty = 1;
$prod_proto->data = array();
$prod_proto->price = $product->sell_price;
// Product needs to have type for the discounts by type to work
$n = node_load($product->nid);
$prod_proto->type = $n->type;
$cart_copy[] = $prod_proto;In function uc_discounts_apply_discounts:
Original:
foreach ($cart as $cart_item) {
$cart_copy[] = drupal_clone($cart_item);
}
// get and store total price
$total_price = 0;
foreach ($cart_copy as $product) {
$n = node_load($product->nid);
$product->type = $n->type;
$total_price += $product->price * $product->qty;
}Changed version:
foreach ($cart as $cart_item) {
// Product needs to have type for the discounts by type to work
$n = node_load($cart_item->nid);
$cart_item->type = $n->type;
$cart_copy[] = drupal_clone($cart_item);
}
// get and store total price
$total_price = 0;
foreach ($cart_copy as $product) {
$n = node_load($product->nid);
// NOTE: the next line didn't do anything outside of the foreach's scope,
// because $product is a value, not a reference into the $cart_copy array
// $product->type = $n->type;
$total_price += $product->price * $product->qty;
}
#1
I tried these patches and my site immediately crashed....
It's running on a Windows server (I apologise for that, I don't have any control over that) and once I had uploaded the changes, immediately got a, "PHP has encountered a Stack overflow" error message.