I've have been racking my brain for the past few days trying to convert this over to version 2.0.
I would just like the "add to cart" button to be disabled (maybe change text to "already purchased" when purchased. I've tried to do a license check to hook the form but it hasn't worked. Like when the file is shown once a license is created.
I've tried other ways, with no luck. Another one I've tried changed the button for entire product display and not for each multiple product.
Any advice on how to get this done?

Comments

bojanz’s picture

Status: Active » Closed (duplicate)

Hi, I'm tracking this in #2049629: Prevent the user from buying a file license twice.
My plan is to hide the add to cart form and show the download links instead.
You'll be able to copy the approach and supply something else if needed.

surfkid’s picture

That would be perfect. I saw that post also. I will watch both posts.
Thank you again!

bojanz’s picture

You can use something like this as a first measure:

/**
 * Implements hook_form_FORM_ID_alter().
 *
 * If the current user already has a license for a file product, then its
 * add to cart form should be hidden, and the download links shown instead.
 * This form alter hides the add to cart form, while the commerce_file
 * formatter on the commerce_file field shows the download links.
 */
function commerce_file_form_commerce_cart_add_to_cart_form_alter(&$form, &$form_state) {
  if (in_array($form_state['default_product']->type, commerce_file_product_types())) {
    $license = commerce_file_get_product_license($form_state['default_product']);
    if ($license) {
      $form['line_item_fields']['#access'] = FALSE;
      $form['submit']['#access'] = FALSE;
    }
  }
}

I need to brainstorm on more complete code (multiple referenced products, anonymous checkout).

surfkid’s picture

Thank you!
It works!
I am using multiple products on one product display and it does switch along with the price options.

I have the "file" link displayed under the add to cart form, so it already only shows if you have a license (download link or plain text) and hides when you don't have a license, so you may not need to have it switch to have a link. That is already possible now.

I tried this link,
http://agileadam.com/add-to-cart-tweaks
which also works checking the items in the cart. But I was trying to use the "commerce_file_get_product_license" as I think it's a better implementation. This link did not work for multiple referenced products as it checked for "product" and not license.
I like the "get_license" route also because if for some reason a customer wants to repurchase, you can just delete their order (and license) and the "add to cart" is back for repurchase. Maybe in other instances, other users may want the add to cart to appear back when limits are reached or something. Maybe make that configurable somehow.
The already in cart thing is cool though. It may be worth a look.
I tweaked the code a little to display "Already Purchased" on the button.
Thank you so much!!

brodiebrodie’s picture

Hi there,

could you tell me how you t tweaked the code a little to display "Already Purchased" on the button.?

Thanks for this

Brodie

brodiebrodie’s picture

Got it! kind of
$form['submit']['#disabled'] = TRUE;
$form['submit']['#value'] = t('Purchased');