Closed (fixed)
Project:
Commerce File
Version:
7.x-1.x-dev
Component:
Code
Priority:
Normal
Category:
Feature request
Assigned:
Unassigned
Reporter:
Created:
1 Sep 2011 at 00:10 UTC
Updated:
18 Sep 2013 at 12:40 UTC
Jump to comment: Most recent file
Comments
Comment #1
recrit commentedThe field formatter for product reference field type is the way to do it. I'd use commerce cart's "commerce_cart_add_to_cart_form" formatter as a base.
Then extend with:
$license->is_active() && $license->access('view')The one caveat is that you'd want the add to cart to show if they are over the limit or do not have download access... and that is ultimately determined with the rule "Control access to licensed file downloads".
Comment #2
inolen commentedRan into this problem today, I needed to disable the add to cart button when the user had valid licenses already.
It seemed that displaying the file(s) field on a node was restricted to files you had access to already, so all that was needed was to disable the actual button.
recrit: I doubt this deals with the caveat you discussed (in my cases downloads are unlimited), but perhaps it's a good starting point.
Comment #3
recrit commentedi'd like to see this as a label alter instead of disabling the button. This way the user is notified of there current limits and can increase them by purchasing again. And then if products have files that are unlimited, then disable the button.
I see 2 add to cart settings:
[1] Alter products' labels to show current limits -or- perhaps show limits gained by purchase
[2] Disable button when a user has unlimited access to all products' files
I've added a license class method per http://drupalcode.org/project/commerce_file.git/commit/876331d. This will aid in determining if all limits on a license is unlimited.
Comment #4
recrit commentedComment #5
itamair commentednice to see something is ongoing on this feature request front, but actually nothing seems to work ...
The "sounds promising" patch at the #3 post seems to be uncompatible with the new commerce file release ... (my drupal sites crashes with it applied),
and the latest commerce file beta3 release implements a new formatter for the product file ("File link with Licence view access check") that seems not to affect this feature request topic: my Add to Cart Button is still there, also for an already licensed file ...
Is there something I am missing, or is still everything mostly undone?
Tnx for all these efforts ...
Comment #6
recrit commentednothing is done yet. Anything added by the module so far is for the commerce file field formatter, NOT add to cart form formatters. The commit noted in #3 is just providing a way to easily test if a license is unlimited, ie $license->is_unlimited()
Comment #7
itamair commentedComment #8
itamair commentedwith the latest release of Commerce Fie (7.x-1.0-beta3) the #2 patch seems to make my Drupal crashing.
What a pity: although as a temporary fix, it sounded good ...
Comment #9
itamair commentedany progress on this front ... ? sorry if I ask, again ...
Comment #10
mediapal commentedI'm another one who is waiting for a solution.
Thank you in advance.
Kirk
Comment #11
itamair commentedHi ... I really had to face this in my Drupal Commerce Project (www.inuedizioni.com), and I faced it, I solved it for my needs,
in a way that I guess might be more generally applied. Thus I share my solution that completely (and I hope also with some elegance) actually satisfies my needs ... (of course without the presumption it is the really definitive and most proper one).
I attach both the module that I created for me, and some screenshots that will help you better understand how it works.
This module version is called "commerce_file_addtochart_hide_on_license",
has the following dependencies:
views
eva
commerce
commerce_file
and works like this:
A - It provides an hook_form_alter on any "commerce_cart_add_to_cart_form_*" $form_id and through a quite complex query (that was hard to define), checks if the $user has got a license related to the file (related to the product type related to the product displayed ...) and if this is the case disables the "Add to chart" button and write a suffix message to it like '(you already bought this product)'.
B - At the same it embedds a coded view ("licensed_file_x_user_x_node") that defines an entity view (through the Eva module) attached to my entity and bundles/product display types (in my case it is just "Rivista", Magazine in Italian ...). This view is quite complex, but works properly, and outputs a not null result in case the $user has already bought the digital product (license granted and active). Its output is the link to the file (for download) and also its footer as a text exposing the link to the licensed files page for the same $user. This entity view results in an added field in the display management set up of the product display, and might be properly placed just after the add to chart button. Nice!
Note: I tried (and tried ...) to accomplish the same entity view (eva) result with a hook_field_access alter of the commerce_file same field access hook (just trying to give access to the file to the user that bought it, in the same way the administrator of commerce file has to it, according to commerce_file itself), but was unable to bypass it, because of the hook_field_access rule that return FALSE if any of hook_field_access returns FALSE.
The same I tried to do with a hook_preprocess_field function, but was unsuccessful the same ...
I just kept the commented code for both of this just as a memory ...
As a final result my app work like this, as shown in the attached screenshots (in the same way numbered):
1 - the commerce file is shown to every user with commerce_file administration permissions, and the add_to_chart too ...
2 - the add to chart button is shown and the commerce file is not shown to users (not commerce_file administrator) that didn't bought the product and haven't the licensed file granted yet
3 - the add_to_chart button disabled and the file shown (for immediate download) if the product is bought and its license granted (and active); also a link to licensed files page shown
I didn't test this in case of commerce_files products attached to the same product display (as hasn't been my case so far ... ), but I think might work, or somehow made it working though.
Anyway I am confident that this may be ready to be re-used by some you as it is (just after personalizing the entity view setting in terms of bundles, and fields labels translations ... that in my case are in italian) , or at least I hope may give some useful ideas to who, much clever than me, is in charge of finding more official solution.
So far, I am happy with this ... ;-)
Comment #12
Daga commentedJust wanted to chime in and mention that I am wanting a variant of this. I want to show the "Download" links instead of "Add to Cart" when the price is $0 (ie. "free"). For paid products, I plan to make a page that shows valid licenses.
At the moment it looks like Views + Views PHP are the best choice (against hacking DC). It would be nice to have an easier option.
Comment #13
recrit commentedI have revamped the hook_field_access implementation as of commit http://drupalcode.org/project/commerce_file.git/commit/0545368.
The commit makes the hook only test for non "view" operations of the field. The field formatters can then control the view of the field.
The current formatter provided by Commerce File is "File link with License view access check" which will check if the user has a valid license for the file, else it will show the file as plain text.
if you wanted a special field formatter to show for some other conditions, then you can use that as a base to create your own.
Comment #15
nicolas bouteille commentedOk I will try to use #11 but if someone now knows an easier way to check whether the current user has already bought a given file I would be glad to here it.
Comment #16
madelyncruz commentedI've got an error with #11 method. Is it compatible with the latest version of commerce file?
Comment #17
bojanz commentedOnly 7.x-1.x, not 7.x-2.x.