Hello,

I used to use hook_uc_cart_item with the load op to interact with cart items but it has been removed from the beta 4.
I search how to interact again with cart items, I've seen hook_uc_cart_display but it was only launched on checkout page ... not on cart page
Is there a solution in beta 4 for that ?

Thanks

Comments

wodenx’s picture

Cart items are now entities - so you can use the full entity-api to interact (see system.api.php).

longwave’s picture

Category: bug » support

I guess some example code would be useful to help people migrate from hook_uc_cart_item to the entity system. Perhaps we could document the entity hooks in the cart API docs file?

longwave’s picture

Title: can't interact with cart » Document entity hooks
Version: 7.x-3.0-rc4 » 7.x-3.x-dev
Component: Cart/checkout » Documentation
Category: support » task
bensey’s picture

Hi there,
can someone please just make a quick comment about this here to point some of us in the right direction?

I'd love to be running rc4 but as described in #1 I can't make it do what I need it to do either.

A few simple lines of code using the straightforward hook_uc_cart_item was all that was needed to adjust prices and quantities for various customer's wishes, and they carried through to the checkout. Easy!

Now since cart items as entities this process seems rather bewildering. I thought I had found the solution with hook_uc_cart_pane but changes there do not carry through to the checkout. Cart items as entities is going to be awesome, but entity api documents are way to complex for me to get a handle on how to do something as simple as described above.

I've spent hours trawling through google and the code of other contrib modules, most of which are currently broken in rc4 also.

Any clues as to which entity hooks we should start looking at would be much appreciated,
ASAP please if someone can spare even just 5 or 10 minutes...

Thanks.

wodenx’s picture

Not sure exactly what it is you want to do.

If you want to adjust the *display* of an item (e.g. add some text, change the price that's displayed), use hook_entity_view() or hook_entity_view_alter() (both are documented in system.api.php, and look at uc_taxes.module for an example). This is roughly the equivalent of the old hook_uc_cart_item('display',...).

If you want to make a persistent alteration to the item (e.g. create a discount), use hook_uc_product_alter() - which is documented in uc_product.api.php. This corresponds to the old hook_uc_cart_item('load', ...).

Hope that's helpful.

wodenx’s picture

But note that changes made using hook_uc_product_alter() may appear on the node display page - the hook is designed to allow you to use the contents of the $product->data array to alter price, qty and other fields. Usually the data array is populated when the item is added to the cart via hook_uc_add_to_cart_data(), which is invoked by both uc_product and uc_product_kit when processing the add-to-cart form.

wodenx’s picture

Oh - there's also hook_node_load() which can be used to alter a product when it's first loaded. The difference between doing that and using hook_uc_product_alter() is as described above -- the latter can react to a fully populated $product->data array (which will usually be empty when the node is loaded).

bensey’s picture

Thanks for the speedy reply wodenx, and for more info that I was hoping for.
That gives me something to get me started, I think I really need to bite the bullet and delve into entities in a big way..!

In answer to what it is I wish to do in #5,
right now it is pretty basic stuff (simplified here with dummy variables):

foreach ($cart_items as $item){
    if ($apply_discount) {
      if ($item->qty >= 3) {
        $item->cart_price = $item->sell_price - 1;
    }
  }
}

And then have the adjusted results carry through to the checkout.
Got it all working well in the cart with hook_uc_cart_pane but all adjustments were left behind when moving to the checkout.

Thanks again for your speedy feedback..!

mgifford’s picture

Where is the handbook page for the D7 version. Is there at least a stub page available for this? The switch to entities is huge.

seanr’s picture

Ugh. This change just really wrecked my day. Thanks a lot (and this is part of why I'm moving to Commerce for all new work).

I need to be able to handle quantity related pricing including during updates in the cart; will hook_uc_product_alter get me there?

markusa’s picture

Yeah this really sucks.....would be great if there was ANY documentation as you'll make big changes

markusa’s picture

Does anyone have any example code?

gary.evans’s picture

Here you go, Hope this Helps:

Example Updating Price based on Attribute;

function MYMODULE_uc_product_alter(&$node) {
		if (isset($node->data['attributes']) && is_array($node->data['attributes'])) {
			$someattribute = $node->data['attributes'][1];			 
			if($someattribute == 'somevalue'){
				$node->list_price = 1000000000
				$node->price = 10000000;
			}  
	  }
}

Example Updating Price based on QTY;

function MYMODULE_uc_product_alter(&$node) {
	$someattribute = $node->data['attributes'][1];			 
	if($node->qty > 20){
		$node->list_price = 1000000000
		$node->price = 10000000;
	}  
}
SilviuChingaru’s picture

I also thin those entity hooks should be docume ted in *.api.* files. Someone should add them on each Uc module that uses entities now.