10 replies [Last post]
jruberto's picture
Offline
Joined: 03/16/2010
Juice: 33
Was this information Helpful?

I'm using hook_uc_cart_alter() to remove an attribute that just contains some serialized data & the customer should not see it.

<?php
function mymodule_uc_cart_alter(&$items) {
   
// Hides attribute that contains serialized node references
   
foreach($items as &$item) {
      unset(
$item->data['attributes'][5]);   // 5 is the aid of my "hidden" attribute
   
}
}
?>

It works just perfectly, hiding the ugly mess of serialized data. However, ticking the "Remove" box & hitting "Update Cart" does not remove the item from the cart -- there's no PHP error, nothing in watchdog, just the item is not removed from the cart.

I do feel like I might be approaching this at the wrong level -- if I get this working, I'm pretty sure the attribute I want to hide will still show up on the invoice or probably in random other places I can't think of now. (I've tried similar to above in hook_cart_display, hook_cart_item unsuccessfully)

-OR-

Is there a better way to achieve a "hidden attribute"? Man, I sure wouldn't mind if "hidden" was a choice along with Select box, Text field, Radio, etc. when adding an attribute. Or maybe some per-attribute permissions or something off the wall like that.

Ideas? Thanks!

jruberto's picture
Offline
Joined: 03/16/2010
Juice: 33
Re: Using uc_cart_alter() is breaking 'remove from cart'

I'd even be happy to do it with CSS if there's a theme function where i can stick a CSS class on each attribute with its attribute id.

peter_'s picture
Offline
Joined: 07/26/2010
Juice: 31
Hidden attributes FTW

Did you manage to find a solution to hide your attributes?

I am looking for the exact same feature. Hidden attributes would be the bees knees.

Sirgado's picture
Offline
Joined: 05/14/2010
Juice: 29
Ubercart uses node id and data array

I've same problem but using hook_cart_display.

As a nice guy told me on the Ubercart IRC channel, Ubercart uses the combination of node id and data array to figure out which thing to remove. So, unsetting some of the attributes is probably confusing it.

I'm looking on a workaround, I'll post here if I find it Eye-wink

Sirgado's picture
Offline
Joined: 05/14/2010
Juice: 29
Workaround

Ok, I've a workaround... it may not me best way but it works. Please let me know if there's a better way.

Ubercart doesn't allow to theme attributes individually. So, one way is to use hook_form_alter to alter the description field of each item. Unfortunately at that time Ubercart has filled the description field and attributes informations is not available, so you can't rebuild or write your own description based on attributes value. But the item data array is available, so you can pass data from add to cart to form alter.

So, using hook_add_to_cart_data, where full node object of product being added to cart is availabe, you can add any data you may need in form alter. Using that data you can write your own description field for each item.

In your case you may write in hook_add_to_cart all attributes data without your hidden attribute, and then use that in form alter to override default description.

For example:

function my_module_add_to_cart_data($form_values) {

  $result = array();

  [... some code to get $my_description]

  $result['my_module'] = array('my_desc' => $my_description);

  return $result;

}

Them in form alter you can get the data array (is serialized, unserialize it) and use it to write your item description.

I hope it helps.

ryanschmidt's picture
Offline
Joined: 11/17/2008
Juice: 235
Re: Using uc_cart_alter() is breaking 'remove from cart'

Did anyone ever figure out why that code was breaking the Remove from cart functionality? I'm having the same issue: http://www.ubercart.org/forum/support/18913/cant_remove_item_cart

I'm using the below code to mark products as shippable or not based on the value of the item's weight field. My reason for doing this is outlined here: http://www.ubercart.org/forum/support/18791/free_shipping_specific_produ...

The code works as expected. I commented some code out because I am deciding which is more efficient. Normally I just select the Remove checkbox associated with the item, then click Update cart. When I do that with this module enabled, it reloads the page as normal with the "Your cart has been updated" notification and NO errors. Unfortunately, it does NOT remove the item from the cart. There's no way to remove the item without disabling this code first.

My problem is that when using this code I am unable to remove the item that matches the if test from the cart.
Example: If I use the code that is not comment out ($item->weight !== 1) I am not able to remove any items that do not have weight = 1. If I use the commented code, I cannot remove any items.

What's wrong with my code that's causing this? I followed this information as much as possible: http://www.ubercart.org/docs/api/hook_cart_item

<?php
/**
* This module checks the item's 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;
    }
}
?>
Sirgado's picture
Offline
Joined: 05/14/2010
Juice: 29
Re: Re: Using uc_cart_alter() is breaking 'remove from cart'

It may be related to the problem of Ubercart identifying products in cart using id and data item info (see above).

Antway, Ubercart ask each product if they are shippable by calling this hook with first parameter ($op) equal to 'can_ship'. You may try this why to tell Ubercart which products are shippable and which are not.

ryanschmidt's picture
Offline
Joined: 11/17/2008
Juice: 235
Re: Re: Re: Using uc_cart_alter() is breaking 'remove from cart'

can_ship is the first option I tried. Unfortunately, I don't believe you can modify the item from there. I spent awhile trying and nothing would work. Any ideas?

ryanschmidt's picture
Offline
Joined: 11/17/2008
Juice: 235
Re: Using uc_cart_alter() is breaking 'remove from cart'

Any other ideas?

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

You have to update the item for ubercart. If you take whatever item you modified and send it to uc_cart_update_item($modified_item); , it should work.

Cheers!

Clayton

ryan88's picture
Offline
Joined: 10/17/2010
Juice: 17
Thank You!

Dude, thank you for this info. I was having the same problem and this makes it work correctly!

Just an FYI to anyone who is looking to remove an attribute before sending a product to the cart, here is what I did

<?php
function CUSTOM_MODULE_cart_item($op, &$item) {
  switch (
$op) {
    case
'load':

    
   

// I want to load the node data so I can access all the node fields
     
$node = node_load($item->nid);

    
   

// If the node type is a paid event
     
if($node->type == 'paidevent') {

        
      

// Change the price depending on what the user selected
         
$item->price = $item->data['attributes'][5];

        
      

// Remove the attribute so users don't see it upon checkout
         
unset($item->data['attributes'][5]);

        
      

// Update the item with Ubercart, this fixes the 'won't remove from cart' problem I was having
         
uc_cart_update_item($item);
      }
    break;
  }
}
?>