Drupal Commerce Add To Cart Form Tweaks / Registration Improvements

In this example I’m showing how to improve the Add to Cart button for Drupal Commerce products to show differently based on whether or not users have already purchased a product, or whether the product is already in their cart.

I’m currently working on a site where users are registering for “programs,” which are synonymous with classes, courses, etc. I’m using the following modules (among others) in this example:

  • Drupal Commerce
  • Registration
  • Commerce Registration

Please understand that when a user registers for a course, they are actually purchasing a product that has a referenced Registration entity.

Here are the objectives I address with the code below:

Only allow the user to register once for each program (purchase a product once), and update the Add to Cart button to tell them they’ve already enrolled in the program (purchased the product).

If the user already added a program to their cart, update the Add To Cart button to tell them they’ve already added the program to their cart. As far as I can see, you could use this code for any type of product. You would simply replace “program” with the product type you wish to target. Please read the comments throughout the code to understand how the pieces work.

There are some considerations for people looking to do this with registrations, specifically. Note that the code below doesn’t handle the situation where a registration is canceled but the order isn’t updated to remove the associated program product line item. This is because we’re looking at the user’s product order history, not the user’s registration status for each program. To overcome this, instead of looking at the products a user has purchased, maybe we should look at registrations that are in the system for the user. I’ll post a comment to this post when I’ve written that functionality.

DISCLAIMER: This is not a plug-and-play module for a reason: there would be much to consider to make a contributed module that would work for every situation. This is simply an example of what you can do with a few functions for a custom implementation on a site.

Code

Result

Here is what the output looks like:

programs_mod

Tagged with: , , , , ,
Posted in Development
7 comments on “Drupal Commerce Add To Cart Form Tweaks / Registration Improvements
  1. Avinash Raut says:

    Modify code to get product ids from commerce_line_item
    you made some very good join it help me to get exact that i want
    Thanks once again …

    /******************** get Product id for your custom (business logic)
    ********************* add in commerce_checkout module (commerce_checkout_complete) fucntion..
    *********************/
    function commerce_checkout_get_users_purchased_products($order_id) {
    /*start*/
    global $user;
    $purchased_product_ids = &drupal_static(__FUNCTION__);

    if (!isset($purchased_product_ids)) {
    $query = db_select(‘commerce_order’, ‘corder’);
    $query->join(‘commerce_line_item’, ‘li’, ‘corder.order_id = li.order_id’);
    $query->join(‘field_data_commerce_product’, ‘prod’, ‘li.line_item_id = prod.entity_id’);
    $query->condition(‘corder.uid’, $user->uid, ‘=’)
    ->condition(‘li.order_id’,$order_id, ‘=’)
    ->condition(‘corder.status’, ‘pending’, ‘=’)
    ->fields(‘prod’, array(‘commerce_product_product_id’));
    $result = $query->execute();

    $purchased_product_ids = array_unique($result->fetchCol());
    }

    return $purchased_product_ids;

    /*end*/
    }

  2. Andrew says:

    This looks great. I am trying to do something very similar.

    Where do I put this code to intigrate into my site?

    Thanks for your help

    Andrew

  3. Andrew says:

    In addition, does this apply to the active cart before checkout or is this user specific. Why I ask is that I want the user to be able to make separate bookings for different children. Each child has to have an individual order. So if this stops a user from purcvhasing two tickets to the same course it would not work for me.

    Thanks again

    Andrew

  4. Chandan singh says:

    what if, product display having more than one product in case of product variation

  5. Juan Pablo says:

    Hey!! thank you for this, you pointed me on the good way to write a similar module.

    Thanks man!

  6. Hans K says:

    Thanks for this code. It works very well for one thing. I use it for the Commerce Add-to-cart button and had to change:
    $form['submit']['#disabled'] = TRUE;

    for:
    $form['submit']['#attributes']['disabled'] = 'disabled';

  7. Natan Miranda says:

    Hey man, you saved my life!!! Tanks a lot! haha I was searching how check if user already purshased one product to limit only one product id by user. Perfect, tnks 😉

Leave a Reply

Your email address will not be published. Required fields are marked *

*