No control over position of price on rendered product

Created on 3 December 2017, about 7 years ago
Updated 19 June 2024, 6 months ago

Like this:

Color
   Red

Size
   Medium

Price
   $ 49,00

Add to cart

I tried:

  1. changing the field order in the product type and product variation type settings
  2. overriding commerce-product.html.twig
  3. adding some jQuery / Javascript code
  4. positioning the price div using CSS
  5. using hook_form_BASE_FORM_ID_alter() to alter the form

Solution #1 did not work out because the variation fields (color and size in this example) and the add-to-cart-button appear to be packed together as "Variations" in the product type settings.

Solution #2 has the same issue. The variation fields and add-to-cart-button are packed together in "product.variations" as one placeholder. I am not able to decompose it into seperate fields.

For solution #3 i tried moving the price div to the right location using jQuery, like this:

$("article .field--type-commerce-price").insertBefore(".commerce-order-item-add-to-cart-form .form-actions");

At first glance this works. However when the user starts changing variation fields the containing form gets removed along with the price. Commerce regenerates this form in order to update all the variation fields.

So far #4 gave the best results. I posted it on https://drupal.stackexchange.com/questions/248004/commerce-2-display-pri... but it is hacky. Using a top margin i created some space above the add-to-cart-button. Then i position the price div into that space. The CSS-code looks like this:

.commerce-order-item-add-to-cart-form .form-actions {
   margin-top: 4em;
}
article .field--type-commerce-price {
   position: absolute;
   bottom: 2.5em;
}

For solution #5 i was able to insert custom text before the add-to-cart-button which looks promising:

function HOOK_form_commerce_order_item_add_to_cart_form_alter(&$form, FormStateInterface $form_state, $form_id) {
   $form['test'] = array(
      '#markup' => 'Here is where the price should go.',
      '#weight' => 99
   );
}

But then i cannot figure out how to obtain the actual price from there. Then i found a D7 solution at:
https://drupal.stackexchange.com/questions/105946/drupal-commerce-add-to...
It looks like this:

function mytheme_form_commerce_cart_add_to_cart_form_alter(&$form, &$form_state, $form_id) {
  $product = $form_state['default_product'];
  $view_mode = $form_state['context']['view_mode'];
  $form['price'] = field_view_field('commerce_product', $product, 'commerce_price', $view_mode);
}

But i am not able to produce a D8 equivalent for this, in particular the last line.

function mytheme_form_commerce_order_item_add_to_cart_form_alter(&$form, FormStateInterface $form_state, $form_id) {
   $product = $form_state->get('product'));
   $view_mode = $form_state->get('form_display')->getMode();
   // form['price'] = $product->get(???)
}

This is silly. I'm struggling for weeks now to get this working but I am getting nowhere. Does anyone have a better idea?

🐛 Bug report
Status

Closed: won't fix

Version

2.0

Component

Product

Created by

🇳🇱Netherlands rjb

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • 🇿🇦South Africa niki v

    It works as designed: the intermingling comes from having product-type display fields numbered 0,1,2,3... and also having product-variation or attribute fields numbered 0,1,2,3.... Number each field in each display as you want them to appear in the product page display. As the variation fields containing price, stock, weight, etc are injected into the product-type display, you use "show row weights" and number them in the product-variation display as you want them in the product-type display.

    I put a simple example here:

    Product display
    image - 0
    body - 1

    Product variation
    price - 2
    stock level - 3

    Product display
    add to cart form - 4
    categories - 5
    tags - 6

  • 🇺🇸United States hockey2112

    Anyone have a working solution yet? #30 does not work. I am working on a new ecommerce website, and was hoping there might have been a real fix for this issue in the several years since it was reported.

  • 🇷🇸Serbia bojanz

    This problem was solved by integrating with Layout Builder. If you're not using Layout Builder, then you write your own Twig template. There is no middle ground, and there won't be any other fix.

  • Status changed to Closed: won't fix 6 months ago
Production build 0.71.5 2024