4

I need to alter Ubercart product price on Ubercart 3, Drupal 7, when product is added to cart. I need to check some conditions, and if met, change the price. I thought i can go with

function MYMODULE_uc_cart_item($op, &$item) {  
  switch ($op) {
    case 'load':
      drupal_set_message(t('It works!'), 'status');
      break;
  }
}

but not sure am i targeting right hook.

It seems that it does not work (it is not invoked).

Thanks for clues

  • hooks are the right way to programmatically interact with modules. May be you can set it with rules module, i am not sure. – niksmac Jan 21 '13 at 13:19
  • no, i want to do it from my custom module. hook is described here, but cant get it to work: ubercart.org/docs/api/hook_cart_item – NenadP Jan 21 '13 at 13:22
  • Please post the code youre using in your custom module. – niksmac Jan 21 '13 at 13:26
  • See updated code. – NenadP Jan 21 '13 at 13:33
  • function hook_uc_cart_item($op, &$item) { the work hook should be replaced with your modules name like function YOURMODULENAME_uc_cart_item($op, &$item) – niksmac Jan 21 '13 at 14:04
2

Here is another hook you can play with hook_uc_add_to_cart

Parameters:
$nid - the node ID of the product
$qty - the quantity being added
$data - the data array, including attributes and model number adjustments

Here is how to work with hooks

Drupal's module system is based on the concept of "hooks". A hook is a PHP function that is named foo_bar(), where "foo" is the name of the module (whose filename is thus foo.module) and "bar" is the name of the hook. Each hook has a defined set of parameters and a specified result type.

function hook_uc_cart_item($op, &$item) { the work hook should be replaced with your modules name like

function YOURMODULENAME_uc_cart_item($op, &$item)

See Hooks

  • 1
    Sorry if question have misleaded you.I implemented it in my module, of course, by replacing hook with my module name (i have written some custom modules before). I cannot get it to work though, so I hope someone experienced in hooking Ubercart might help. sorry for trouble, i will try to change question a bit. – NenadP Jan 21 '13 at 14:10
  • @HoverFusion see my edit. – niksmac Jan 21 '13 at 14:15
  • Nikhil, thanks for suggestion, here's my upvote :) and thanks for your effort. I answered the question myself, as it was issue with cache. – NenadP Jan 21 '13 at 14:27
  • You suggested the hook that best fits my question, but my final goal is little more complex, so i will go mainly with uc_cart_alter, see my answer update. – NenadP Jan 21 '13 at 15:24
2

The problem was in clearing cache. For some reason, clearing cache from Drush does not completely work. (I need to clear it from interface). It might be also APC i implemented at some point. I have issues when clearing cache for modules/other code.

EDIT:

I can't get hook_uc_cart_item to work so far. (even as hook_cart_item) hook_add_to_cart works, and hook_uc_cart_alter also works (for me)

I think i might take hook_cart_alter approach, as it gets items every time cart is viewed, in fact, they say it gets loaded all the time, and they have warned of performance issue, but as i need to watch on cart all the time as i have some dynamic calculations depending what is in cart, i think, if devel and kcachegrind dont complain, i will not, too :)

I see uc_coupon module also uses hook_cart_alter without fear :)

  • Did you ever find a solution? – coderama Feb 19 '15 at 19:00
  • I can't remember, it was ages ago! :) Sorry – NenadP Feb 19 '15 at 19:04
  • Posting my answer because it took me 2 hours to find what seems so obvious now. – coderama Feb 19 '15 at 19:08
2

This is how I solved it:

function mymodule_uc_cart_alter(&$items) 
{
    $items[45]->price = 29393;
}

In this example I'm just hardcoding the change to demonstrate how to change the value. Obviously you would apply your own logic here based on whatever your requirements are.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.