2

I have 2 sections: Gift Cards and Merchants.

In Gift Cards, I have a entries field that is limited to the Merchants section and limited to 1 selection.

In Merchants, I have a field merchantId that is a plaintextfield.

In my plugin, I would like to return all gift cards that have a merchantId of 123. I've tried a few different ways, but all ways have either given no results or I get a 'missing index Merchant' error in the FieldServices class.

Hopefully someone here has done something similar and can point me in the right direction.

0

1 Answer 1

8

First, you should get all the Merchant Entries with the merchantId of 123

$criteria = craft()->elements->getCriteria(ElementType::Entry);
$criteria->section('merchants');
$criteria->merchantId = '123';

$merchants = $criteria->find();

Next, find all Gift Card entries that are related to the found merchants.

$criteria = craft()->elements->getCriteria(ElementType::Entry);
$criteria->section = 'giftCards';
$criteria->relatedTo = array(
    'targetElement' => $merchants,
    'field' => 'YOUR-ENTRIES-FIELD'
);

$gift_cards = $criteria->find();
1
  • This is exactly how I ended up getting it to work. I think I had some parameters switched around the first time through.
    – Conor
    Commented Mar 30, 2015 at 14:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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