I've added a simple "add_to_cart" operation for uc_views_bulk_operations.

Patch is attached.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

longwave’s picture

Status: Needs review » Fixed

Committed to CVS, but using hook_action_info() rather than hook_node_operations().

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.

goose2000’s picture

Category: feature » support

Hi,

I was wondering if this operation is included in the latest dev version?

EDIT, yes it is showing up for me, ignore...

amorsent’s picture

It seems to me that the shippable flag does not properly get set.

The following variation seems to work for me.

/**
* "Add to cart" node action.
*/
function uc_views_bulk_operations_add_to_cart_action($node) {
$data = array('module' => 'uc_product', 'shippable' => $node->shippable);
uc_cart_add_item($node->nid, 1, $data);
}

amorsent’s picture

Status: Closed (fixed) » Needs work

As I noted in my last comment, the shippable flag is the most noticeable deficiency here ( you will know it's missing when the checkout form skips the shipping info pane ). However, the main issue is that hook_add_to_cart_data is not fired.

Modules may expect to use this hook to attach additional data to the cart item. There are 2 implementations in ubercart itself.
uc_product_add_to_cart_data sets the shippable flag.
uc_attribute_add_to_cart_data handles the product's attributes.

Take a look at the buy it now button's submit function and note how uc_cart_add_item is called.

function uc_catalog_buy_it_now_form_submit($form, &$form_state) {
  $form_state['redirect'] = uc_cart_add_item($form_state['values']['nid'], 1,   module_invoke_all('add_to_cart_data', $form_state['values']), NULL, variable_get('uc_cart_add_item_msg', TRUE));
}

To properly build the $data argument for uc_cart_add_item we need to call hook_add_to_cart_data.
At the bare minimum we'll also need to fake a $form_state with the nid

function uc_views_bulk_operations_add_to_cart_action($node) {
  $form_state['values']['nid'] = $node->nid;
  $data = module_invoke_all('add_to_cart_data', $form_state['values']);
  uc_cart_add_item($node->nid, 1, $data);
}

Alternately, we could drupal_execute the buy it now form. This would have the advantage of allowing other modules their form_alter opportunity. This may be vital for the attributes module, I'm not sure. At any rate, I think that woud look something like this:

function uc_views_bulk_operations_add_to_cart_action($node) {
  $form_state['values']['nid'] = $node->nid;
  $form_state['values']['op'] = variable_get('uc_teaser_add_to_cart_text', t('Add to cart'));
  drupal_execute('uc_catalog_buy_it_now_form', $form_state, $node);
}