@@ -56,45 +56,34 @@ public function search($data)
5656 $ data ['include ' ] = 'users,groups ' ;
5757 return $ this ->_call ('search/incremental ' , $ data );
5858 }
59-
59+
60+ /**
61+ * Retrieves a Zendesk Support ticket associated to an order with a custom ticket field
62+ *
63+ * @param int $orderIncrementId
64+ * @return array|boolean
65+ */
6066 public function forOrder ($ orderIncrementId )
6167 {
68+ $ fieldId = Mage::getStoreConfig ('zendesk/frontend_features/order_field_id ' );
69+ if (!$ fieldId ) {
70+ return false ;
71+ }
72+
6273 if (!$ orderIncrementId ) {
6374 throw new InvalidArgumentException ('Order Increment ID not valid ' );
6475 }
6576
6677 $ response = $ this ->_call ('search.json ' ,
6778 array (
68- 'query ' => ' type:ticket ' . $ orderIncrementId ,
79+ 'query ' => " type:ticket fieldValue: { $ orderIncrementId}" ,
6980 'sort_order ' => 'desc ' ,
7081 'sort_by ' => 'updated_at ' ,
7182 )
7283 );
7384
74- // Now check through the tickets to make sure the appropriate field has been filled out with the order number
75- $ tickets = array ();
76- $ fieldId = Mage::getStoreConfig ('zendesk/frontend_features/order_field_id ' );
77-
78- if (!$ fieldId ) {
79- return false ;
80- }
81-
82- foreach ($ response ['results ' ] as $ ticket ) {
83- foreach ($ ticket ['fields ' ] as $ field ) {
84- if ($ field ['id ' ] == $ fieldId ) {
85- // Check if the value matches our order number
86- if ($ field ['value ' ] == $ orderIncrementId ) {
87- $ tickets [] = $ ticket ;
88- }
89-
90- // Regardless of whether the value matches, this is the correct field, so move to the next ticket
91- continue ;
92- }
93- }
94- }
95-
96- if (count ($ tickets )) {
97- return $ tickets ;
85+ if (count ($ response ['results ' ])) {
86+ return $ response ['results ' ];
9887 } else {
9988 return false ;
10089 }
0 commit comments