Since hook_uc_cart_item is no longer supported, make this change to line 64 of uc_custom_price.module or something similar.
FROM
function uc_custom_price_uc_cart_item($op, $item) {
switch ($op) {
case 'load':
$product = node_load($item->nid);
// should really initialize this on load so we don't have to check later
$code = isset($product->custom_code) ? $product->custom_code : '';
if (!empty($code)) {
$eval_code = token_replace($code, array('product' => $product, 'uc_cart_item' => $item));
eval($eval_code);
}
break;
}
}
TO
function uc_custom_price_uc_product_alter(&$node) {
$code = isset($node->custom_code) ? $node->custom_code : '';
if (!empty($code)) {
//RUN CUSTOM CALC
}
}
Comments
Comment #1
elamanPatch with hook_uc_cart_alter implementation to solve problem.
Comment #2
benitezv1ang commentedthis code does not work in ubercart 3 drupal 7
I need help with php i tried differnet ways but does no work.
This is what I want to accomplish
2 attributes
Paper
Quantity
This is what I want to accomplish
If (paper == 16pt C2S (Gloss Cover) && Quantity == 2500){
$item->price = $item->price + 120;
}
Please help
Comment #3
elamanDid you tried to apply patch #1?
Comment #4
benitezv1ang commentedYes Im using patch #1
with this code
$item->price = $item->price;
if ($item->data->qty > 5) {
$item->price = $item->price + 20;
}
and I get this red message
Notice: Trying to get property of non-object in eval() (line 2 of /home/content/27/8309527/html/sites/all/modules/uc_custom_price/uc_custom_price.module(101) : eval()'d code).
I have Qty input field
<input id="edit-qty" class="form-text required" type="text" maxlength="6" size="5" value="1" name="qty">Comment #5
elamanTry this code:
Comment #6
benitezv1ang commentedDoes not work . can you please attach the module you have to try.
thanks
Comment #7
benitezv1ang commentedI tested the module with this code and works.
$item->price = $item->price + 5;I does not work with this code
$item->qty and $item->data->qty is not working
Please help
Thanks
Comment #8
Neo13 commentedAnyone got this working with quantity?
Comment #9
benitezv1ang commentedi need to figure out this too. I tried diferent codes with no luck
Comment #10
Neo13 commentedI applied provided patch and it works :)
Comment #11
lokolo commentedsame problem here, not working with quantity.
update: now it works, dont know really why, perhaps cache issue...
Comment #12
benitezv1ang commentedHere you have the code for drupal 7. with input box and dropdown selections
For input boxes
$test = is_array($item->data) ? $item->data['attributes'][22] : 0;
$item->price = $item->price;
if ( $test > 2) {
$item->price = $item->price + 5;
}
For dropdown boxes
$test = is_array($item->data) ? $item->data['attributes'][19] : 61;
$item->price = $item->price;
if ( $test > 60) {
$item->price = $item->price + 5;
}
use firebug for attributes number (['attributes'][19]) and option number(: 61;)
and use Patch # 1
Comment #13
lokolo commentedThank you very much for the code. Got it working now.
Cheers!
Comment #14
pakati commentedDear All,
I have applied the patch, and try to use two attributes in the PHP calculation (width and height entered by the costumer).
The firebug tells me the codes from the product form:
Id Name Type Value Label Size Maximum Length
edit-attributes-3 attributes[3] text 123 Height (mm) * 60 128
edit-attributes-2 attributes[2] text 321 Width (mm) * 60 128
But whatever I try to call them and use the values that the costumer entered, I got error messages. (parse error, array error...)
I'm playing with
$item->data['attributes'][2]but it's not working.What is the proper way to call those attributes and use their values?
Thanks
Comment #15
Neo13 commentedHi,
I am using this for selected attribute option and it works without a problem: $item->data['attributes'][1]
Comment #16
muhammad.tanweer commentedI had a requirement to alter the price per product on the basis of the quantity of the product added to cart. This post helped me put my code. Here is how I achieved my goal.
/**
* Implements hook_uc_cart_item().
*
* Price calculation takes place here.
*/
function uc_custom_price_uc_cart_item($op, $item) {
switch ($op) {
case 'load':
$product = node_load($item->nid);
$qty_1 = $product->field_qty_1['und'][0]['value'] ? $product->field_qty_1['und'][0]['value'] : 0;
$price_1 = $product->field_price_1['und'][0]['value'] ? $product->field_price_1['und'][0]['value'] : 0;
$qty_2 = $product->field_qty_2['und'][0]['value'] ? $product->field_qty_2['und'][0]['value'] : 0;
$price_2 = $product->field_price_2['und'][0]['value'] ? $product->field_price_2['und'][0]['value'] : 0;
$qty_3 = $product->field_qty_3['und'][0]['value'] ? $product->field_qty_3['und'][0]['value'] : 0;
$price_3 = $product->field_price_3['und'][0]['value'] ? $product->field_price_3['und'][0]['value'] : 0;
$qty_4 = $product->field_qty_4['und'][0]['value'] ? $product->field_qty_4['und'][0]['value'] : 0;
$price_4 = $product->field_price_4['und'][0]['value'] ? $product->field_price_4['und'][0]['value'] : 0;
//if qty_1 and price_1 are input, these values will be included as a pricing tier
if ($qty_1 && $price_1) {
//if there is a minimum qauntity, the order quantity will automatically be set to this amount if user inputs a lesser number
if ($qty_1 > 1) {
$code = "if(\$" . "item->qty < {$qty_1}) {\$" . "item->qty = {$qty_1};} ";
}
$code .= "if(\$" . "item->qty >= {$qty_1}) {\$" . "item->price = {$price_1};} ";
}
//if qty_2 and price_2 are input, these values will be included as a pricing tier
if ($qty_2 && $price_2) {
$code .= "if(\$" . "item->qty >= {$qty_2}) {\$" . "item->price = {$price_2};} ";
}
//if qty_3 and price_3 are input, these values will be included as a pricing tier
if ($qty_3 && $price_3) {
$code .= "if(\$" . "item->qty >= {$qty_3}) {\$" . "item->price = {$price_3};} ";
}
//if qty_4 and price_4 are input, these values will be included as a pricing tier
if ($qty_4 && $price_4) {
$code .= "if(\$" . "item->qty >= {$qty_4}) {\$" . "item->price = {$price_4};} ";
}
// should really initialize this on load so we don't have to check later
// $code = isset($product->custom_code) ? $product->custom_code : '';
if (!empty($code)) {
$eval_code = token_replace($code, array('product' => $product, 'uc_cart_item' => $item));
eval($eval_code);
}
break;
}
}
/**
* Implements hook_uc_cart_alter().
*
* Fix module behavior for Ubercart version > 3.2.
*/
function uc_custom_price_uc_cart_alter(&$items) {
// manually call function for each cart item
foreach ($items as &$item) {
uc_custom_price_uc_cart_item('load', $item);
}
}
As you will see, I have 4 different quantities to alter the price for. May be this will help someone with a similar problem.
Muhammad Tanweer,
http://www.app-desk.com