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
		
   }
}
CommentFileSizeAuthor
#1 uc_cart_alter-1823112-1.patch910 byteselaman

Comments

elaman’s picture

Status: Active » Needs review
StatusFileSize
new910 bytes

Patch with hook_uc_cart_alter implementation to solve problem.

benitezv1ang’s picture

this code does not work in ubercart 3 drupal 7

$item->price = $item->price;
if ($item->data->qty > 5) {
  $item->price = $item->price + 20;
}

I need help with php i tried differnet ways but does no work.

This is what I want to accomplish
2 attributes

Paper

<select id="edit-attributes-2" class="form-select ajax-processed" name="attributes[2]">
<option selected="selected" value="3">14pt C2S (Gloss Cover)</option>
<option value="4">16pt C2S (Gloss Cover)</option>
<option value="41">100# White Linen Cover</option>
<option value="42">16pt C1S (Gloss Cover)</option>
<option value="43">14pt Uncoated Cover</option>
</select>

Quantity

<select id="edit-attributes-3" class="form-select required ajax-processed" name="attributes[3]">
<option selected="selected" value="">Please select</option>
<option value="5">250</option>
<option value="6">500</option>
<option value="53">1000</option>
<option value="54">2500</option>
<option value="55">5000</option>
<option value="56">10000</option>
</select>

This is what I want to accomplish

If (paper == 16pt C2S (Gloss Cover) && Quantity == 2500){
$item->price = $item->price + 120;
}

Please help

elaman’s picture

Did you tried to apply patch #1?

benitezv1ang’s picture

Yes 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">

elaman’s picture

Try this code:

$item->price = $item->price;
if ($item->qty > 5) {
  $item->price = $item->price + 20;
}
benitezv1ang’s picture

Does not work . can you please attach the module you have to try.
thanks

benitezv1ang’s picture

I tested the module with this code and works.
$item->price = $item->price + 5;
I does not work with this code

if ($item->qty > 5) {
  $item->price = $item->price + 5;
}

$item->qty and $item->data->qty is not working
Please help
Thanks

Neo13’s picture

Anyone got this working with quantity?

benitezv1ang’s picture

i need to figure out this too. I tried diferent codes with no luck

Neo13’s picture

Status: Needs review » Reviewed & tested by the community

I applied provided patch and it works :)

lokolo’s picture

same problem here, not working with quantity.

update: now it works, dont know really why, perhaps cache issue...

benitezv1ang’s picture

Here 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

lokolo’s picture

Thank you very much for the code. Got it working now.
Cheers!

pakati’s picture

Dear 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

Neo13’s picture

Hi,

I am using this for selected attribute option and it works without a problem: $item->data['attributes'][1]

muhammad.tanweer’s picture

I 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