The following code works great in a template.

{% set priceProduct = currentUser.priceTable.relatedTo({    
    targetElement: product.id,
    field: 'product'
}).one() %}

{{ priceProduct.price }}

Where priceTable is a Super Table, product.id is from an entry element (we're in loop).

'product' is an entry field in the Super Table row and 'price' is a plaintext field in the Super Table row.

How might we go about replicating this functionality in a plugin?

Craft Pro 3.0.29

Super Table 2.0.11

up vote 4 down vote accepted
$element = Craft::$app->getUser()->getIdentity()->priceTable->relatedTo([
    'targetElement' => $product->id,
    'field' => 'product'
])->one()

But to be honest your code in Twig should not work as expected since you only grab the query and modify it. Your output is your search criteria, not the value of an element.

  • Thank you. (I was so close!) Yeah, I wondered about that. Not getting a deprecation warning either. Fixed in the edit. – Steve Mock Nov 9 at 15:58
  • Nothing was deprecated, you just display the criteria of your ElementQuery there is nothing wrong about that, it is just not what you want – Robin Schambach Nov 9 at 16:00

Your Answer

 

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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