4

I use drupal commerce trying to build a website which sells content (nodes with restricted access). The thing is that, if user have bought the product once, he shouldn't have a possibility to do it again.

Currently i'm using form_commerce_add_to_cart_form_alter to check if user has already added this product to cart and if yes - hide "Add to Cart" button to prevent doing it twice and set a text that this very product is already in the cart. It works totally fine.

What I want to do next are two things:

1) to check if this current user has this product under active (not canceled and not complete) order, and if yes - hide "Add to cart button" and set a text that this product is under order processing at now.

2) to check if this current user has purchased this very product ever before and if he has, do the same trick - hide "Add to cart button" and set a text that User already owns this product.

My problem is that I'm very new to commerce and unfortunately totally don't understand how can I get all orders for the current user and check for some product IDs there... Will be very appreciated for any advice or a tip. Thanks a lot!

2 Answers 2

3

If you have the Entity API module installed, you can do it using entity_metadata_wrappers and EntityFieldQuery like this:

$query = new EntityFieldQuery();

$query->entityCondition('entity_type', 'commerce_order')
  ->entityCondition('uid', $uid);
$result = $query->execute();

if (isset($result['commerce_order'])) {
  $user_order_ids = array_keys($result['commerce_order']);
}

// Array for storing user's products.
$products = array();

foreach($user_order_ids as $user_order_id) {

  $o = entity_metadata_wrapper('commerce_order', $user_order_id);

  $line_items = $o->commerce_line_items->value();

  foreach ($line_items as $line_item) {
    $l = entity_metadata_wrapper('commerce_line_item', $line_item->line_item_id);
    $products[$o->status->value()][] = $l->commerce_product->product_id->value();
  }
}

This should give you a 2 dimensional array of products like this:

... (Array, 1 element)
  cart(Array, 2 elements)
    0 (String, 3 characters ) 203
    1 (String, 3 characters ) 158
  other_state(Array, 2 elements)
    0 (String, 3 characters ) 187
0
1

You can get a lot of the way there just by using Rules and Views, which is why we designed Commerce the way we did. So there isn't going to be a setting or a single feature module that meets all these needs at once, but you can do some things like the following:

  1. You can prevent an item from being added to the cart by unsetting the product price for the item on the page using a product pricing rule. You'd do this by checking to see if the line item ID in the rule is 0, which means it's a temporary line item used to perform the price calculation. Then check to see if the product is already in the cart (or even purchased) and unset the price just like we do in the default pricing rule for when the product is disabled. That will automatically disable the button with the message "Product not available", so you'll likely want to do something to change that to a more meaningful message.

  2. You can also update your product display to not combine line items in the cart in the event that the user is able to find an Add to Cart button for the same product. Then you can disable the quantity textfield in the shopping cart form View so the user can't edit it, or you can do something like is demonstrated in this post to turn the textfield into a plaintext rendering of the quantity. You can also use Rules to react to the add to cart process and ensure the quantity of any related line items is 1. There are drawbacks to using a pure UI approach. As I hinted in part 1, you're limited to what the button text can read. It should be different for different scenarios (already added vs. already purchased). Additionally, you don't have any way while users remain anonymous to inspect the orders in their session for products you know they've already purchased. That seems to me to be a fairly minor feature, though, as ideally they'd know they just purchased something - or else they could log in.

Ultimately, I think these needs would be best satisfied by a custom module that includes the code I mention from part 2 and supports configurable messages where applicable. I don't know of any such module or if these sorts of features are already extant in the 1.x of Commerce File.

The answer found here and it is written by Ryan Szrama.

1
  • I've combined both advices and this helped me much. Many thanks! :)
    – john
    Commented May 17, 2015 at 19:01

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.