10 replies [Last post]
schaub123's picture
Offline
Bug FinderGetting busy with the Ubercode.PayPal Hero
Joined: 10/08/2007
Juice: 458
Was this information Helpful?

I can't find a clear example of modifying a text attribute for a product when it is added to the cart. I can't use hook_form_alter because I want the attribute value to be changed regardless if the product was added from a form or cart link. I thought I could just do with hook add_to_cart_data but it seems like it needs to be called directly with module_invoke_all?

I've tried:

function mymodule_add_to_cart($nid, $qty, $data) {
  $node = node_load($nid);
  if ($node->type = 'some_nodetype') {
    $data['attributes'][12] = 'change the value text';
    uc_cart_add_item($nid, $qty, $data + module_invoke_all('add_to_cart_data', $data), NULL, 'Some Message', FALSE, FALSE);
  }
}

I just want to change the value of attribute id 12 to some other text string whenever this product is added to the cart. I know I'm missing something simple. Any help is very greatly appreciated.

Christopher Schaub

TR
TR's picture
Offline
Bug FinderFAQ ModeratorGetting busy with the Ubercode.
Joined: 11/05/2007
Juice: 3424
Re: Need to modify attribute when adding to cart -- simple?

You can use hook_cart_item(). Something like this totally untested example:

<?php
function mymodule_cart_item($op, &$item) {

  switch (

$op) {
    case
'load':
     
$product = node_load($item->nid);

      if (

$product->type == 'some_nodetype') {
       
$item->data['attributes'][12] = 'change the value text';
      }
      break;
  }
}
?>
<tr>.
TR
TR's picture
Offline
Bug FinderFAQ ModeratorGetting busy with the Ubercode.
Joined: 11/05/2007
Juice: 3424
Re: Re: Need to modify attribute when adding to cart -- simple?

Sorry about the line breaks in the switch and if statements. The raw text in my post is correct (no breaks), but it seems like the PHP formatter on this site is broken after the upgrade.

<tr>.
schaub123's picture
Offline
Bug FinderGetting busy with the Ubercode.PayPal Hero
Joined: 10/08/2007
Juice: 458
Re: Re: Re: Need to modify attribute when adding to cart -- simp

Thanks a lot. It almost works, but I notice that now I'm unable to remove this item from my cart -- the "load" keeps getting called even after the remove op?

Christopher Schaub

TR
TR's picture
Offline
Bug FinderFAQ ModeratorGetting busy with the Ubercode.
Joined: 11/05/2007
Juice: 3424
Re: Re: Re: Re: Need to modify attribute when adding to cart --

Hmm, well I see one thing missing from my code, and that's a check on the nid to make sure you're only modifying the specific products that have that attribute. hook_cart_item() gets called for every single item in the cart. Other than that, there's nothing that should prevent any item from being removed from the cart. I've used this hook in this manner a number of times without a problem.

<tr>.
ryanschmidt's picture
Offline
Joined: 11/17/2008
Juice: 235
Re: Re: Re: Re: Re: Need to modify attribute when adding to cart

I am also unable to remove items from the cart after using this code. Any ideas?

<?php
/**
* This module checks the items weight and if '1' it marks the item as shippable. If '0' marks as non-shippable.
*/
function uc_nonshippable_cart_item($op, &$item) {
    switch (
$op) {
    case
'load':
   
        if(
$item->weight != 1) {
           
$item->data['shippable'] = '0';
        }
   
       
/*if($item->weight == 1) {
            $item->data['shippable'] = '1';
        } else {
            $item->data['shippable'] = '0';
        }*/

       

break;
    }
}
?>
liquidcms's picture
Offline
Joined: 09/13/2010
Juice: 28
dont think so

just trying to figure this stuff out as well..but your code doesnt seem like it could work..

i think, but really have no idea.. that:

uc_cart_add_item insert an item into cart, which then calls hook_add_to_cart to see if you want to modify $data..

so really dont need both. the hook is if you want to do something when the UI or some other method adds an item.

also, if you were using the hook.. it needs to return something.

ryanschmidt's picture
Offline
Joined: 11/17/2008
Juice: 235
Re: dont think so

Actually, the code does work just fine. The problem is that when using it, I am not able to remove items from the cart that match the if test in the code.

can_ship requires that you return something. load does not.

Please see this post that details the issue: http://www.ubercart.org/forum/support/18913/cant_remove_item_cart

ryanschmidt's picture
Offline
Joined: 11/17/2008
Juice: 235
Re: Need to modify attribute when adding to cart -- simple?

Any other ideas? I really need to figure this out

clay_ton's picture
Offline
Joined: 07/07/2011
Juice: 44
Attribute Issues

Was this ever resolved? I have run into this same problem when I manually change attributes in hook_cart_item.

Thanks!

Clayton

clay_ton's picture
Offline
Joined: 07/07/2011
Juice: 44
SOLUTION

Found the solution in another thread, linking it.

http://www.ubercart.org/forum/support/18913/cant_remove_item_cart

Cheers!

Clayton