discount by product type works when viewing but not in cart/checkout

Project:Discounts
Component:Code
Category:
Priority:normal
Assigned:Unassigned
Status:active
Description
Project: 
Discounts

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
Producttype >= 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.

Version: 
Ubercart 1.0
frost's picture
Offline
Joined: 07/23/2008
Juice: 134
problem fixed

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;

}

psynaptic's picture
Offline
Early adopter... addicted to alphas.Not KulvikTheminator
Joined: 08/28/2007
Juice: 731
Re: problem fixed
Assigned to:frost» psynaptic

This issue has been moved to http://drupal.org/node/323860