6 replies [Last post]
tdbdev's picture
Offline
Joined: 07/07/2009
Juice: 7
Was this information Helpful?

Here is the situation.

I have generated a list of products with check boxes. You check the check boxes and enter in quantities and the click "add". The function i wrote loops through all of the checked products and runs the uc_cart_add_item($nid,$qty,NULL,NULL,FALSE,FALSE) function on each product...simple enough right?

Well everything is added to the cart just fine but when you go to checkout, the delivery information does NOT show up even if you have added shippable products.

BUT, if i go and add the exact same product using the normal "add to cart" button instead of the check boxes, then when you go to the checkout screen, the delivery information shows up fine.

So my testing scenario went like this:
1. Check 1 shippable product from checkbox list of products and set qty to 1.
2. Click "add" and then go to the checkout screen and Delivery Information DOES NOT show up.
3. Empty cart.
4. Go to normal list of products, find same shippable product from step 1 and click "add to cart"
5. go to checkout screen and Deliver Information CORRECTLY show up.

I am sure I am simply missing something simple that I need to do, but I am supposed to have this fixed by the end of the day and I have several others things I am working on, so I was hoping the community may be able to help me out. =)

Oh and I am using the UPS shipping quotes. That probably doesn't matter but i thought I would let you guys know.

-tim

Lyle's picture
Offline
AdministratoreLiTe!
Joined: 08/07/2007
Juice: 6846
Re: Delivery Information not showing up when using uc_cart_add_i

Since product features can make products not shippable through the choice of attributes, you have to specify in the third parameter to uc_cart_add_item() that the product is shippable. The easiest way to do this is too copy the uc_product_add_to_cart_form_submit() does it:

<?php
function uc_product_add_to_cart_form_submit($form, &$form_state) {
 
$form_state['redirect'] = uc_cart_add_item($form_state['values']['nid'], $form_state['values']['qty'],  module_invoke_all('add_to_cart_data', $form_state['values']));
}
?>

I said "easiest", not "easy". The part you're missing is the module_invoke_all('add_to_cart_data', $form_state['values']). This hook is how attributes can adjust the model number because the chosen attributes are part of $form_state['values'].

If the product you are adding doesn't have any attributes, then you can just use array('nid' => $nid, 'qty' => $qty) instead.

tdbdev's picture
Offline
Joined: 07/07/2009
Juice: 7
Re: Re: Delivery Information not showing up when using uc_cart_a

Thanks for the quick response,

I thought I might have to add something in the $data part of the function.
I am going to give this a shot and let you know how it turns out.

tdbdev's picture
Offline
Joined: 07/07/2009
Juice: 7
Re: Re: Re: Delivery Information not showing up when using uc_ca

module_invoke_all worked like a champ!

I did it slightly different than just passing the form values. Its probably not the best way to do it, but i just put the nid in a variable array and passed the variable to the function like so:

<?php
$values_nid
['nid']=$nid;
uc_cart_add_item($nid, $qty, module_invoke_all('add_to_cart_data', $values_nid), NULL, FALSE, FALSE);
?>

and it worked perfectly. I tested it on shippable and non-shippable products and it now properly displays the Delivery Information pane when needed.

Thanks again for all of your help!

-tim

bayofodeke's picture
Offline
Joined: 03/07/2011
Juice: 7
uc_cart_add_item breaking shipping quote

Btw, you guys are a life saver. I've been trying to fix this issue for the past week. Your suggestions above fixed my woes.

I happen to have two attributes, so I had to do some tweaking in order to get the code to work properly.

<?php
$data
= array('nid' => $nid, 'attributes' => array($aid1 => $oid1, $aid2=>$oid2));
uc_cart_add_item($nid, $qty, module_invoke_all('add_to_cart_data', $data));
?>

where $aid is the attribute ID and $oid is the option ID.

now shipping quotes show up as they need to!

Thanks again Lyle and Tim for your responses

Saoirse1916's picture
Offline
Joined: 06/06/2011
Juice: 3
Re: uc_cart_add_item breaking shipping quote

I thought I had a handle on this but after about 4 hours I'm still completely stumped. I've got attributes as well and I'm using a foreach loop to run through them and get them into the proper format for display.

<?php
//get ubercart attributes
$attributes = array();
if (isset(
$form_state['uc_membership_attributes'])) {
 
$att_build = array();
  foreach(
$form_state['uc_membership_attributes'] as $key => $value){
   
$att_build['aid'][] = $key;
   
$att_build['oid'][] = $value;
  }
 
//get results into the expected format
 
$attributes = array_combine($att_build['aid'], $att_build['oid']);
}

//add it to the cart
$data = array('nid' => $node->nid, 'attributes' => $attributes);
uc_cart_add_item($node->nid, $uc_membership_quantity, module_invoke_all('add_to_cart_data', $data));

//redirect to checkout
$form_state['redirect'] = 'cart/checkout';
}
?>

When I dsm($data) I get the proper result:

... (Array, 2 elements)
  nid (String, 3 characters ) 400
  attributes (Array, 2 elements)
    3 (String, 2 characters ) 13
    12 (String, 2 characters ) 35

But, while my attributes are picked up properly, I still have no Delivery information options. If I get rid of $attributes and just pass the $nid then I get delivery options but, of course, no attributes which means the price is wrong. Can anyone point me in the right direction?

pfrilling's picture
Offline
Joined: 01/06/2009
Juice: 36
Re: Re: uc_cart_add_item breaking shipping quote

I noticed the data that is returned from

<?php
module_invoke_all
('add_to_cart_data', $data)
?>

always has the shippable parameter set to FALSE. I solved this by saving the module_invoke_all() data into a variable. Then I edited that variable and re-added the shippable parameter as TRUE. Then I passed that data into the uc_cart_add_item() function. The code I used is:

<?php
$myArray
= array(
 
'attributes' => $data_array,
 
'shippable' => TRUE,
 
'restrict_qty' => FALSE,
 
'module' => 'uc_product',
);

// Run any other hooks on our product array.                              
$data = module_invoke_all('add_to_cart_data', $myArray);

// module_invoce_all() above is always returning FALSE for the shippable flag, let's force this to TRUE.
$data['shippable'] = TRUE;
              
uc_cart_add_item($nid, $quantity, $data, NULL, FALSE, FALSE);
?>