Active
Project:
Commerce Core
Version:
7.x-1.x-dev
Component:
User experience
Priority:
Normal
Category:
Feature request
Assigned:
Unassigned
Reporter:
Created:
5 Oct 2011 at 21:43 UTC
Updated:
25 Sep 2015 at 14:08 UTC
Jump to comment: Most recent
Comments
Comment #1
davidwhthomas commentedSubscribing.
Comment #2
davidwhthomas commentedOK, I needed this too and did it with hook_form_alter, perhaps there's a better way, but it works.
I hope that helps.
cheers,
DT
P.S Commerce rocks :-)
Comment #3
davidwhthomas commentedComment #4
rszrama commentedThanks for posting a solution, David! : )
Comment #5
essbee commentedBrilliant.
Only issues I see are:
I'll work on the above two and repost adjusted solution.
Thanks again David, agree Commerce rocks!
Sam
Comment #6
rtdean93 commentedThanks for posting this. Worked great.
Question... how do I format the price.
Current display is "30 USD"
I'd like to format is as "$30.00"
Comment #7
essbee commentedI have the formatting fixed, just need to change a line in the code, I'm working through proper handling to ensure the calculated sell price is used rather than the stored product price (ie to include taxes etc).
If all you want is the currency properly formatted, replace the function "commerce_currency_amount_to_decimal" with "commerce_currency_format", same variables are accepted.
Correct procedure though should be using commerce_product_calculate_sell_price to ensure it is compatible with other modules that alter the price (discounts, taxes etc).
Hopefully will post soon.
Comment #8
essbee commentedOk the following works and utilises the calculate price, and current currency formatting settings, but its a bit inelegant. Would appreciate someone suggesting the correct way to display the formatted, calculated price.
Comment #9
davidwhthomas commentedThanks essbee, nice adjusted solution :-)
DT
Comment #10
essbee commentedNo worries, thank you for doing the hard part.
Also I added a div wrapper to allow me to style the label and price.
Comment #11
rtdean93 commentedThis works great. Thanks for the fast and efficient fixes.
Comment #12
jessicakoh commentedHow do I use the code? Where do I paste?
Comment #13
essbee commentedYou'll need to create a module.
I suggest you start here: http://drupal.org/node/361112
The code provided is the content for the .module file, you will also need to create a .info file. You will need to rename the two functions replacing the word "example" with the name of your module.
Comment #14
davidwhthomas commented@jessicakoh While a module is the best place to put the code, in D7 you may also be able to add to template.php, changing "example" to your theme name.
The example was tested from a module.
DT
Comment #15
jessicakoh commented@davidwhthomas Thank you for the tips. Save my time, I can quickly test out a code.
Comment #16
timodwhit commentedJust out of curiosity: With this module/code as a base would it be possible to invoke rules to calculate a price based on "Term Selection" on the line item using rules, prior to adding to cart? If so sign me up! :-)
Thanks for the code!
Comment #17
paulguy commentedThanks for the code too !
But in case of multiple products with attributes (like sizes, colors etc) and when I change one of them the form is reloaded but not the price. And I can figure why the hook_alter is not called on form refresh ...
How could I display the price of the current product (mixes of attributes) displayed and refresh it on change ?
Same question with a quantity select field ...
Thanks by advance !
Comment #18
davidwhthomas commented@paulguy,
Not sure what you did there, but the code will refresh and show the price of the current selected product, based on attribute combination. At least, it does for me.
Comment #19
rszrama commentedI'm not entirely sure why this is still marked as "needs review", but since that status is specifically for patch review, I'm just going to make this an open feature request. Would be great to be able to get this into core if possible, but it'll need a patch and tests to ensure it accommodates the necessary use cases.
Comment #20
murzUsing full hook_form_alter() will slow down site, because this function launches on each form on page, so if we have 10 forms, this function will be called 10 times.
So be better to use hook_form_BASE_FORM_ID_alter(), for this form it will be
mymodulename_form_commerce_cart_add_to_cart_form_alter(), so instead of 2 functions you will need only one:
Comment #21
rszrama commentedYou can use hook_form_alter() just the same and wrap your code in an if statement on the $form_id parameter. No performance hit there either. The hook's still going to be invoked whether you implement it or not, and your code will still only be executed once.
Comment #22
rtdean93 commentedI am using a price formatter to show discounts applied to the price... the formatters work fine in views, but are not working with this form alter. How would I place the formatted price in this form?
Comment #23
murzWhen using hook_form_BASE_FORM_ID_alter(), function is invoked only on BASE_FORM_ID forms, so if page have 10 other forms and 1 commerce form, this function will called only once.
But if we use hook_form_alter(), function will called 11 times.
If function do only one "if statement", php each time will spend cpu time and memory for calling this function, passing $form and $form_state objects into it, parsing if statement, etc, so we will lost small piece of performance (not so big, but not zero) on this operations.
If each module will add his hook_form_alter() to each form, this many small pieces at all can slow down site.
So, I think, better is to use hook_form_BASE_FORM_ID_alter().
Comment #24
rszrama commentedHeh, fair enough. The performance hit is really trivial, but yes, may as well use the specific hook. My comment was a bit of cross-talk from another issue where the question was about loading cached entities or something.
Comment #25
stijn.blomme commented@rtdean93 I ran into the exact same problem.
I was able to fix it by using the field api to collect the renderable array for this field.
i'm using the commerce_price_savings_formatter to format the field.
edit: just noticed this only works if you have your price field set to visible in the display settings
edit: if you set the field to hidden Drupal will not store the formatter connected to the field
It will still render if you insert the formatter hardcoded.
Comment #26
willieseabrook commentedHi everyone
Thanks for providing this, it was very close to what I needed and it pops up on the first couple of google results.
I needed to show the formatted price *with components* though as we have a legal requirement to show the tax component of an advertised price.
Code is below. Not sure if "commerce_price_field_formatter_view" is the right approach, but it does work. Also you'll need to be careful as this only works on node view pages - I use $node to get the language. We only need the form shown in this way on public node view pages and the site is multilingual.
Comment #27
willieseabrook commentedImprovement on my previous example.
We probably shouldn't be using "commerce_price_field_formatter_view" directly? Instead using 'field_view_value'?
So I think this is is a better approach (although its pseudo and not tested):
Comment #28
joachim commentedWhat about the case when there is only a single attribute field in the add to cart form? I'd like to show the price in the actual attribute widget in this case.
I'm wondering whether this warrants a separate issue though, as it's going to be implemented totally differently.
Comment #29
lsolesen commented@joachim That is a different issue, but I am looking for the same thing, so I started it here: #1823356: Show price in form element single attribute field. You are welcome to clarify.
Comment #30
nicolas bouteille commented#20 worked for me.
I would be great if this functionnality could be added to the settings of the product reference field in the Manage display screen just as the quantity widget is. I personally don't know how to do this yet.
Comment #31
candelas commented@Murz thanks
#20 worked for me.
i subscribe to the feature request petition.
Comment #32
jonhattanComment #33
sujith.nara commentedCommerce pricing attributes module updates price field dynamically. But the price field shown in commerce cart form doesn't change.
How to show this dynamic price field in cart form?
Comment #34
etiennechataignier commentedThe answer is here : http://drupal.stackexchange.com/a/105950/13979