0

I'm in the early stages of building a client site that is heavily centred around paid membership. Depending on a fairly complex set of rules, members will join the site and be charged different amounts for their annual membership based on fields they fill in on the signup form.

I am using Ubercart and Content Field Profile to create a custom membership form. The membership boils down to having 1-many products added during the sign up, based on the values entered in the form.

Is there a way I can do this without coding it all myself using submit or validate hooks and uc_add_product or similar?

3 Answers 3

1

I would look into Rules (may require some patches and the Ubercart Rules module, as I don't know if it can add products to the cart by default).

That said, a Google search (which I'm sure you've also done) doesn't hint at much. If you have time, I would suggest doing it the way you're planning (validate and submit hooks and uc_add_item). It will work well. Then contribute back what you've done either to an existing, similar module or as a new uc_ module.

Before doing so, you could ask in #drupal or, more specifically, #drupal-ubercart on IRC.

1

Thanks to wizonesolutions and a bit of googling, I came up with a very simple solution using a custom module.

I've now moved to Drupal 7. Using a combination of Logintoboggan, Rules, and Entity Fields, I can set up complex conditions for membership using the admin interface rather than code. More importantly, this means my clients will be able to do the same.

Ubercart Rules is fantastically useful, but doesn't have support for adding products to a user's cart out of the box. This was easily fixed with a small custom module which added the action. Below is the code from that module:

<?php

/**
 * @file
 * This file contains the Rules hooks and functions necessary to enable adding
 * products to carts from Rule actions.
 */

/**
 * Implements hook_rules_action_info().
 */
function mycustommodule_rules_action_info() {

  $actions['mycustommodule_add_product'] = array(
    'label' => t('Add a product to the cart'),
    'group' => t('MyCustomModule Order'),
    'base' => 'mycustommodule_add_product_to_cart',
    'parameter' => array(    
      'product' => array(
        'type' => 'text',
        'label' => t('Product'),
        'restriction' => 'input',
        'options list' => 'mycustommodule_product_list_options',
      ),
      'quantity' => array(
        'type' => 'text',
        'label' => t('Quantity'),
      ),
    ),
  );

  return $actions;
}

/* * ****************************************************************************
 * Condition Callbacks and Forms                                              *
 * **************************************************************************** */

/**
 * Return a list of product nodes, keyed to their nid.
 */
function mycustommodule_product_list_options() {
  // Get a list of all products
  $query = db_select('node', 'n')
    ->fields('n', array('nid', 'title'))
    ->orderBy('title');
  $query->join('uc_products', 'p', 'n.vid = p.vid');

  $result = $query->execute();

  foreach($result as $row) {
    $return[$row->nid] = $row->title;
  }

  return $return;

}

/**
 * Add a product to the user's cart, based on the rule.
 * 
 * @param int $product
 *   nid of the product to add
 * @param nid $quantity 
 *  quanity of product to add
 */
function mycustommodule_add_product_to_cart($product, $quantity = 1) {
  uc_cart_add_item($product, $quantity);
}

I hope someone finds this useful.

0

We are having actually the same problem that we cant add automatically a product to cart with a rules. (annual membership) Unfortunatlky we are using commerce and we are asking ourself what param to change in your code for using it with commerce instead of UC. Do you have an idea or some key for us?

thank you very much. sebastien L.

1
  • Sorry, I'm not familiar with commerce.module as yet.
    – cam8001
    Commented Sep 7, 2011 at 10:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.