0

In Drupal 7/Commerce, I would like to display a view of product fields attached to a product so that when the add-to-cart selection is updated and product fields are updated via ajax, the view is also updated (as with typical product fields).

In my case, I want to create an entity reference field ("included hardware") referencing other products attached to a product and use a view to display the result. This must happen at the product level, and not with the product's display node.

I have tried EVA. These fields don't appear on Product Display View Mode configuration.

I have attempted Display Suite (block field). When I select a display template for the product I see the fields on the product type display, but they don't show up on the Product Display node.

Can anyone point me to a solution for displaying a view within a product display that changes when a new product is selected?

1

The fields that change on a product display as different variations are selected on the Add to Cart form are updated via a series of Ajax commands returned by the form's refresh handler. It's about as complicated as the Forms API gets, to be honest, but it lets us do things like render the related fields and return them with instructions to the JavaScript client on where to place the HTML snippets to reflect the updates.

Because your View isn't associated with the product in the same way, you really need to trigger a refresh of its contents yourself. I'm not aware of any modules that will do that for you unless you can try / rule out Views Field. That may work with the product refresh system to support replacing the View alongside other fields.

That said, that's kind of overkill and would require a rearchitecture of your product displays. I would just hook into hook_commerce_cart_attributes_refresh_alter() yourself, re-render the View using the product ID stored in $form['product_id']['#value'], and return a command to drop it into place similar to how the other product fields are updated.

  • 1
    I am truly grateful for your prompt response. I will look into "Views Field" first thing. Unfortunately, when Drupal site building, I rely on the frontend building features of Drupal and community modules to get through, which for the most part have been sufficient. I will also attempt to follow along with any instructions I can find to make the most of your suggestion. – dgdoing Aug 20 '18 at 21:48
  • 1
    My current solution works around the limitations of displaying product fields by creating a new content type (product_note) that references products with an Entity Reference field. This way, I'm able to reference the product_note content from the product and display the results in a product field as a rendered entity. Because I can use EVA fields within the product_note content type displays, I'm now able to use a view within a field to format my display, which works with ajax and product displays. – dgdoing Aug 24 '18 at 15:25

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.