-1

I'm editing the view Shopping cart form (Commerce Order) to add the node's title to the form.

I did the following:

  1. Add relationship: Commerce Line item: Product
  2. Add relationship: Commerce product: Node referencing products from field_product
  3. Add field: Field: Title and set Relationship to "Node referencing products from field_product"
  4. Added the nodes Title field.

Which gives me the nodes' title, but then I also get every node associated with that product.

My commerce setup is the following:

  1. I have a single product called "Voting on a Dog"
  2. I created a new content type and referenced the product "Voting for a Dog".
  3. I created 12 pieces of content that have the dogs pics, bio etc.

When I go to checkout it shows all the content I created vs just the one I'm checking out with (the node I added to the cart). I removed all the relationships and tried the hook function.

I dumped all the variables from this hook to see if the title was already there since the nid is listed.. but can't find it.

function custom_commerce_form_views_form_commerce_cart_form_default_alter(&$form, 
&$form_state) {
  dsm($form,'Form');
  dsm($form_state,'Form State');
  }

In short, I want to add the nodes title to the Shopping cart view. Below is the view before I start adding relationships. enter image description here

This is the view after I add the relationships

enter image description here

I just want to add the node's title to the first view without the rest of the nodes showing up.

  • What are you trying to achieve? What is your question? – Neograph734 Sep 13 '17 at 21:42
  • I edited my original post. Thanks for responding. – Max Sep 14 '17 at 12:09
1

This may not be possible without custom code. The problem here is that the Views relationship you're using is what we call a reverse relationship - it joins the product to nodes that reference it, not vice versa like other relationships with join out to referenced entities. When you join out like that, you can easily add a filter based on the delta value of the reference field to limit the number of rows you get, but for the reverse relationship there is no such option.

What you most likely would need is a custom Views handler that knows how to join based on the display context data in a product line item's data array. No such thing exists as far as I know, and it's likely a non-trivial task to make work well.

  • Thanks Ryan. That's way over my head, but I'll give it a go. Since the nodes path is already linked to the product, I was hoping there would be an easier way to bring the node title in as well. – Max Sep 15 '17 at 15:23
  • One thing that comes to mind would be instead of trying to make the relationship work, if you just need the node title, you have all the data in the line item you'd need to load that node or even directly query the database for the title. In that case, you could make a custom Views field handler instead that puts the node title into the View output directly using that data. – Ryan Szrama Sep 16 '17 at 1:53
0

I know this is probably not the correct way, but after trying a views handler, I ran across this and it does work.

function custom_commerce_commerce_line_item_type_info_alter(&$line_item_types) {
$line_item_types['product']['callbacks'] = array('title' => '_custom_commerce_commerce_product_line_item_title');

//dsm($line_item_types,'info_alter');
 }

/**
* Returns an appropriate title for this line item for this site
*/
function _custom_commerce_commerce_product_line_item_title($line_item) {
$title_id = $line_item->data['context']['entity']['entity_id'];
$node2 = node_load($title_id);
$line_item=$node2->title;
return $line_item;
}

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.