0

Is there a way of hooking into the commerce cart form and adding a custom field which so happens to be the product image that I want to display?

I've tried via the view itself but I have no idea on how to when there is only the Order or Order Item objects available in the commerce cart form itself.

I've also tried the following hooks:

  • theme_views_pre_render
  • theme_preprocess_views_view
  • theme_views_data_alter
  • theme_commerce_cart_form_views_pre_render
  • theme_form_alter

But i've had no luck.

Can anyone suggest a solution, it's only temporary as I should imagine it will eventually be entwined with the drupal commerce module.

Thanks in advance.

  • 1
    The entire first column is a rendered product variation, so the regular ways will all work (Manage display / overriding the twig template) – Bojan Zivanovic Feb 3 '18 at 18:53
0

You can do it with views. Just use relationship (example: productvariation)

Then you can choose the field you want to show in your cart-form.

  • Hi Per, I remember adding this question as to when drupal 8 commerce was more in it's infancy, by which the productvariation option when adding a relationship was not in the dropdown menu. I'm guessing it's now been added. – cwiggo May 22 '18 at 11:46
0

This can be accomplished from the front end by simply editing your commerce_cart_form view and using relationships to "relate" your way backwards from the order item to the product display.

Edit your cart form view and click Advanced on the right side of the screen. Then under Relationships click "Add" then add and apply the following relationships in this order.

  1. Product variation - Order item - "The purchased product variation." This will give access to add any of the fields under the Product Variation you are relating. it will also give you access to add the product type relationship in step 2.

  2. Product - Product variation - "The parent product." This will give access to add any of the fields that you have in the Product Type you are relating, (including the image field from your product display). This will also give you access to add a content relationship as seen in step 3.

Once you have added those relationships, you will be able to access and add any of the fields you want from both the product variation, and the product type under Fields.

If you are displaying with nodes and/or need fields that are in content types that have product references, you will also need to add the following relationship:

  1. Content using field_YOUR_PRODUCT_REFERENCE - Product - "Relate each Content with a field_YOUR_PRODUCT_REFERENCE set to the product." This will bring in all the fields for the display node.

    No custom code required. :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.