Is there any way to show attributes (title, price etc.) of commerce products which are referenced by the same node at which the eva view will be attached to?

Product reference field: field_product_ref (multiple selection allowed)

Currently, I can only do the following:

  • Entity type = node
  • Bundles = myContentType
  • Arguments:id

Then add Contextual Filters (arguments) = Content: Products (field_product_ref)
Configure this contextual filter: Provide default value => Content ID from URL.

Of course, that results in showing exactly that product which has the same ID as the node has. Instead I would need an option for Contextual Filters like "is referenced by node:id".

Do you have any idea?

My ultimate goal is to show one add to cart form for multiple products attached to the node which is referencing the products. That can be done with commerce add to cart extras (but still needs to be attached to the node with eva).

mkadin answered this question in another issue:

Instead of passing the id of the node in as an argument. Use the tokens option under argument. Then you can use the token for the node reference. That should handle that for you.

Sadly, I still don't get it working. Here are my settings:
Entity Content settings:

  • Arguments: [node:field_product_ref]

Contextual Filter

  • Content: Products (field_product_ref) -> "Display all results for the specified field"

Did I miss something?

Comments

mkadin’s picture

When you use that token, the argument coming in will be the nid of the product that is being referred to. Thus, your contextual filter should be the nid, not the field_product_ref...you've already got the product's nid coming in.

haggins’s picture

Thank you mkadin.
The problem is that I have to set up a view which shows the commerce product entity. Otherwhise I have no access to the product attributes at the fields-section. In this case, there's no nid to select under contextual filter - only product entity related things (including the product ID) and the field_product_ref.

Do you see the problem?

mkadin’s picture

Product ID is what I meant to say. I'm not super familiar with drupal commerce, and I forgot that products aren't nodes but their own entities. Try setting up the contextual filter with the product ID.

haggins’s picture

I did so. The resulting query looks like this:

SELECT commerce_product.product_id AS product_id, 'commerce_product' AS field_data_field_description_commerce_product_entity_type, 'commerce_product' AS field_data_commerce_price_commerce_product_entity_type FROM commerce_product commerce_product WHERE (( (commerce_product.product_id = 'product2, product2_2' ) )) LIMIT 10 OFFSET 0

The problem seems to be this condition:
commerce_product.product_id = 'product2, product2_2'

1. Problem:
Instead of the product ID the token argument for field_product_ref provides the referenced sku's.
=> Solution: set sku as contextual filter

2. Problem:
The condition should be
(commerce_product.sku = 'product2' OR commerce_product.sku = 'product2_2' )
and not concatenating the different skus.

Is this a bug or do I still miss something?

edit: it's all working fine when only one product is referenced. However, with multiple products the above problem appears.

mkadin’s picture

Hmmm...that seems to be how the token is interpreted. You can try setting up your view a little bit differently.

Set up your view so that it displays products, then use a reverse relationship from the product to the node that references it. Set up your contextual filter to be the nid and use the relationship that you just created. Then change the EVA argument setting to 'nid'. That oughta do the trick.

haggins’s picture

Great, that's it! Thank you mkadin!

Comparison:

If you want to attach a view of commerce products referenced by the actual node via a product reference field, your view needs to look like this:

Type: Commerce Product

Entity Content Settings:

  • Entity type: Node
  • Bundles: your node type
  • Arguments: id

Relationships:

  • Commerce Product: Node referencing products from field_product_reference

Contextual Filters:

  • (Node referencing products from field_product_reference) Content: Nid
mkadin’s picture

Status: Active » Fixed

Glad to help :)

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.