Developers/Translate values for searching items

From Aimeos documentation

Developers
Other languages:
English 100% • ‎русский 88%


2016.x version

Translate values for searching items

Sometimes, the values used by the MShop framework (e.g. salutation constants: "company", "mrs", "miss", "mr") are different from the values used by external sources (e.g. only "m"/"f" for male/female). Searching for the constants from the MShop framework would find no results in this case. Therefore, it's necessary to transform the MShop framework values to the values of the external source before executing the search statement. Fortunately, there is a way provided by criteria plug-ins to create this mapping. You have to implement such a criteria plug-in first:

  1. namespace Aimeos\MShop\Common\Manager\Address\Plugin;
  2.  
  3. class MySalutation implements \AimeosMW\Criteria\Plugin\Iface
  4. {
  5.   public function translate( $value )
  6.   {
  7.     switch( $value )
  8.     {
  9.       case \Aimeos\MShop\Common\Item\Address\Base::SALUTATION_COMPANY:
  10.         return '';
  11.       case \Aimeos\MShop\Common\Item\Address\Base::SALUTATION_MRS:
  12.       case \Aimeos\MShop\Common\Item\Address\Base::SALUTATION_MISS:
  13.         return 'f';
  14.       case \Aimeos\MShop\Common\Item\Address\Base::SALUTATION_MR:
  15.         return 'm';
  16.     }
  17.   }
  18. }

The searchItems() method of the manager has to be adapted to create the criteria plug-in and hand it over to the getConditionString() method of the given criteria object implementing the \Aimeos\MW\Criteria\Iface:

  1. $plugins = array(
  2.   'customer.address.salutation' => new \Aimeos\MShop\Common\Manager\Address\Plugin\MySalutation()
  3. );
  4. //...
  5. $search->getConditionString( $types, $translations, $plugins )

It's important that the $plugins variable is an associative array. The key for each plug-in has to be the name of the code for searching whose value we wish to translate.