Customize API request

Set additional product filters

Task

You want to manipulate the item list with additional filters.

Solution

With the help of an OXID eShop module you can extend the API request:

First create a module directory (here specified with <module_root>) and a metadata.php that looks like this:

<?php
/* <module_root>/metadata.php */

$sMetadataVersion = '1.1';

$aModule = array(
    'id'     => 'makaira/extension',
    'title'  => 'Makaira :: Extension',
    'extend' => array(
        'makaira_connect_request_handler' => '<module_root>/makaira_extension_request_handler'
    ),
    'files'    => array(),
    'blocks'   => array(),
    'settings' => array(),
);

Next comes the override of the makaira_connect_request_handler::modifyRequest() method. This allows easy manipulation of the API request and setting additional filters.

<?php
/* <module_root>/makaira_extension_request_handler.php */

class makaira_extension_request_handler extends makaira_extension_request_handler_parent
{
    protected function modifyRequest(Query $query)
    {
        /*
        $query->customFilter = [
            "product" => [
                "and" => [
                    [
                        "field"    => "OXSTOCK",
                        "operator" => "gt",
                        "value"    => 2
                    ],
                    [
                        "attribute" => "d2eb444e35c0a71f0a85df8194acb5b6",
                        "operator"  => "eq",
                        "value"     => ["blau", "schwarz"]
                    ],
                    [
                        "attribute" => "9438ac75bac3e344628b14bf7ed82c15",
                        "dataType"  => "integer",
                        "operator"  => "gteq",
                        "value"     => 10
                    ],
                ],
                "or"  => [],
                "not" => []
            ]
        ];
        */

        return parent::modifyRequest($query);
    }
}
Information

The customFilter property accepts filters for product fields and attributes that are linked using and, or or not. These can also be nested within each other. As of Makaira-Connect version 2018.11.0, attributes can be imported specifically as numeric attributes. If such an attribute is to be accessed via the customFilter, dataType must be set (integer or float).

The customFilter can be applied only to product attributes, only to variant attributes, or both. So instead of product, makaira-product (variants only) or makaira-productgroup (products only) can be used:

makaira-product filters only by variant attributes

makaira-productgroup filters only by product attributes

product filters on both levels simultaneously: by product and variant attributes

If multiple customFilters with different types are used, they are linked with and .

Provide additional data for autosuggest

Task

You want to display additional fields in Autosuggest, for example, the manufacturer of an item.

Solution

The returned fields of the autosuggest are provided by the following function:

<?php

protected function getFieldsForResults()
{
    $fields = ['OXID', 'OXTITLE', 'OXVARSELECT'];

    return $fields;
}

The function can be found in the Connect module at src/oxid/core/makaira_connect_autosuggester.php.

By extending the class makaira_connect_autosuggester the function can be overridden and more fields can be added.

<?php

class makaira_project_autosuggester extends makaira_project_autosuggester_parent
{
    /**
     * Getter method for resulting fields
     *
     * @return array
     */
    protected function getFieldsForResults()
    {
        $fields   = parent::getFieldsForResults();
        $fields[] = 'MANUFACTURERTITLE';

        return $fields;
    }

    /**
     * data preparation hook
     *
     * @param object $doc
     * @param \oxArticle $product
     *
     * @return array
     */
    protected function prepareProductItem(&$doc, &$product)
    {
        $aItem = parent::prepareProductItem($doc, $product);
        // add additional field
        $aItem['manufacturer'] = $doc->fields['manufacturertitle']

        return $aItem;
    }
}
Information

Calling the parent method ensures that the default autosuggest fields will continue to be used.

When preparing the data for autosuggest, an instance of the item is created. To avoid the associated database request, the loadProductItem method can be overridden instead of the prepareProductItem method.

<?php

class makaira_project_autosuggester extends makaira_project_autosuggester_parent
{
    /**
     * Prepare the data
     *
     * @param object $doc
     *
     * @return array
     */
    protected function loadProductItem($doc)
    {
        if (empty($doc->fields['oxtitle'])) {
            return [];
        }

        $title = $doc->fields['oxtitle'];
        if (!empty($doc->fields['oxvarselect'])) {
            $title .= ' | ' . $doc->fields['oxvarselect'];
        }

        $aItem = [];
        $aItem['label'] = $title;
        $aItem['value'] = $title;
        $aItem['link']  = $doc->fields['url']
        $aItem['type']  = 'product';

        return $aItem;
    }
}