I'm a bit surprised this hasn't been requested yet, but, it would make adding a product "add-to-cart" form to a page a BREEZE. Instead of having to create a view which looks for the add_to_cart form, it could just be picked up as needed. And, for Panels users, it would fall into "Miscellaneous" and be added to a panel with little or no frustration.

I've tried exploring the hook_block() code, but don't really understand how I can pull the node ID into the block and then call uc_product_add_to_cart_form.

Any takers?
Willing to put a small bounty on this.

Comments

wjaspers’s picture

Status: Needs review » Active

Here's what I have thus far. GOT IT! The only strange thing is that it doesn't show up in the Core block manager.

For now, I've just plopped this at the bottom of ubercart/uc_product/uc_product.module

function uc_product_block($op = 'list', $delta = 0, $edit = array()) {
  // The $op parameter determines what piece of information is being requested.
  switch ($op) {
    case 'list':
      // A block can provide default settings. In this case we'll enable the 
      // block and make it visible only on the 'node/*' pages. 
      $blocks[0] = array(
        'info'       => t('Add product to cart form'),
        'status'     => TRUE,
        'weight'     => 0,
        'visibility' => 1,
        'pages'      => 'node/*',
        'cache'      => BLOCK_NO_CACHE,
      );
      return $blocks;
    case 'configure':
      $form = array();
      // nothing to configure.
      return $form;
    case 'save':
      // If $op is "save", we need to save settings from the configuration form.
      // Since the first block is the only one that allows configuration, we
      // need to check $delta to make sure we only save it.
      return;
    case 'view': 
      // If $op is "view", then we need to generate the block for display
      // purposes. The $delta parameter tells us which block is being requested.
      switch ($delta) {
        case 0:
          if (arg(0)=='node' && is_numeric(arg(1))) {
            $node = node_load(array('nid'=>arg(1)));
          } else return;
          // If this node isn't a product, just call it quits.
          if(!uc_product_is_product($node)) {
            return;
          }
          // The subject is displayed at the top of the block. Note that it
          // should be passed through t() for translation.
          $block['subject'] = t('Add to Cart');
          // The content of the block is typically generated by calling a custom
          // function.
          $block['content'] = theme('uc_product_add_to_cart',$node);
          break;
     }
     return $block;
     default: return;
  }
}


EDIT!!!: I realized later that this would best be placed in the uc_cart module file.

wjaspers’s picture

Status: Active » Needs review
wjaspers’s picture

Status: Active » Needs review

And, for Drupal 7 ... you'll need this code, since hook_block is now deprecated.

function uc_product_block_info() {
  $blocks = array();
  $blocks['uc_add_to_cart_block'] = array(
    'info'       => t('Add product to cart form'),
    'visibility' => BLOCK_VISIBILITY_LISTED,
    'pages'      => 'node/*',
    'cache'      => DRUPAL_NO_CACHE,
  );
  return $blocks;
}

function uc_product_block_view() {
  if (arg(0)=='node' && is_numeric(arg(1))) {
    $nid = arg(1);
    $node = node_load($nid);
  } 
  else {
    return array();
  }
  
  // If this node isn't a product, just call it quits.
  if (!uc_product_is_product($node)) {
    return array();
  }
  
  // The subject is displayed at the top of the block. Note that it
  // should be passed through t() for translation.
  $block[$delta]['subject'] = t('Add to Cart');
  
  // The content of the block is typically generated by calling a custom
  // function.
  $block[$delta]['content'] = drupal_get_form('uc_product_add_to_cart_form', $node);
  
  return $block[$delta];
}
wjaspers’s picture

Status: Closed (works as designed) » Needs review

Decided to make a module of it.
Code to follow soon.
http://drupal.org/sandbox/wjaspers/1145762

wjaspers’s picture

Status: Needs review » Closed (works as designed)
TR’s picture

Issue summary: View changes
Status: Needs review » Closed (works as designed)

This issue was improperly reopened due to a bug in the drupal.org upgrade to D7. See #2125263: Issue status not taken from last comment during D7 migration for details.