Closed (won't fix)
Project:
Commerce Core
Version:
7.x-1.x-dev
Component:
Developer experience
Priority:
Normal
Category:
Feature request
Assigned:
Unassigned
Reporter:
Created:
25 Sep 2012 at 18:15 UTC
Updated:
12 Sep 2025 at 11:15 UTC
Jump to comment: Most recent
Comments
Comment #1
rszrama commentedAs is you have two options:
You can see here the root idea is that we are determining before the form submission whether or not the product on the form is purchasable. From the customer's point of view, I believe this is the best behavior. Why have a submit button advertising an operation that actually isn't possible?
Comment #2
joachim commentedThanks for the quick feeback!
I seem to be hitting several DX problems here.
I've found the hook hook_commerce_product_calculate_sell_price_line_item_alter(), which seems to be where you're saying I need to act.
Changing the metadata with the wrapper doesn't seem to work (that's more a problem with EntityAPI's sparse docs though). But altering the line item directly works and on form submission I'm told I can't add it to the cart:
But AFAICT this hook only get invoked in commerce_cart_add_to_cart_form_submit(), so as you say, this isn't terribly good UX: the user is misled into submitting the form and only finds out they can't buy it once they've tried to.
So it seems to me I need to be altering the add to cart form.
The $context param is great, but I am wary of relying on the node in there, because an add to cart form can appear in Views AFAIK where presumably that could be empty.
Therefore, alter code has to look at the product(s) involved in the form. However, these are in a different structure depending on whether there's one or many. You can see from this code in commerce_stock that it's a bit awkward:
My use case here is that I can operate on the node level, so I can just zap the whole form. But I want the API security too, just in case later on we add other pathways to purchase products.
So several places where DX could be improved here I think:
- make it easier to sniff the commerce_cart_add_to_cart_form to see which products it is listing
- have a single point where adding a product to the cart can be denied (which would then be called both by the the form, and the API function commerce_cart_product_add().
Comment #3
tuccio commentedI tried unsetting the line item price, but the Add to Cart button still renders while clicking it causes a PDO exception. My product is a views display, not a node.
Comment #4
jsacksick commentedD7 is EOL, closing this.