diff --git a/doc/3. Customization/Examples.md b/doc/3. Customization/Examples.md index 0181828..d46cb0f 100644 --- a/doc/3. Customization/Examples.md +++ b/doc/3. Customization/Examples.md @@ -1,14 +1,18 @@ # Examples -- [Classes](#classes) - - [Prices with discounts to icml](#prices-with-discounts-to-icml) -- [Filters](#filters) +- [Classes](#classes) + - [Prices with discounts to icml](#prices-with-discounts-to-icml) +- [Filters](#filters) + - [Set order custom field on status change](#set-order-custom-field-on-status-change) ## Classes + ### Prices with discounts to ICML + Customization for generate ICML catalog with prices including discounts Put code to `.../retailcrm/custom/classes/RetailcrmCatalog.php`: + ```php <...> $price = !empty($product['rate']) @@ -40,12 +44,44 @@ if (!empty($product['manufacturer_name'])) { ``` ## Filters -### ... -... -Put code to `...`: +### Set order custom field on status change + +Put code to `custom/filters/RetailcrmFilterOrderStatusUpdate.php`: + ```php -<...> -code -<...> +getShipping() as $shipping) { + $trackingNumbers[] = $shipping['tracking_number']; + } + + $object['customFields']['tracking'] = implode(', ', $trackingNumbers); + + // get data from the database + $sql = 'SELECT important_data FROM ' . _DB_PREFIX_ . 'important_table + WHERE id_order = ' . pSQL($order->id); + + $data = []; + foreach (Db::getInstance()->ExecuteS($sql) as $row) { + $data[] = $row['important_data']; + } + + $object['customFields']['important_data'] = implode(', ', $data); + + return $object; + } +} ``` diff --git a/doc/3. Customization/Filters.md b/doc/3. Customization/Filters.md index caee29c..8a82b09 100644 --- a/doc/3. Customization/Filters.md +++ b/doc/3. Customization/Filters.md @@ -3,45 +3,17 @@ ## Usage If you want to modify data, sent between CRM and CMS you can use custom filters. -To use filters you should define a new class in `/modules/retailcrm/custom/hooks`. Filename and classname must match the filter name. +To use filters you should define a new class in `/modules/retailcrm/custom/filters`. Filename and classname must match the filter name. Filter class should implement interface *RetailcrmFilterInterface*. In filter class you must define *filter()* function, which will take initial `$object` and return customized `$object`. -## Example - -The example below shows you how to customize address data, loaded from CRM history during back sync: - -```php -dni = $dataCrm['dni']; - } - - return $object; - } -} -``` +You can see more examples on the [Examples](Examples.md) page ## List of filters There are list of available filters: * *RetailcrmFilterProcessOrder* - order array, which will be sent to CRM +* *RetailcrmFilterOrderStatusUpdate* - order array, which will be sent to CRM when the status is changed * *RetailcrmFilterProcessCustomer* - customer array, which will be sent to CRM * *RetailcrmFilterProcessCustomerCorporate* - corporate customer array, which will be sent to CRM * *RetailcrmFilterProcessAddress* - address array, which will be sent to CRM diff --git a/retailcrm/retailcrm.php b/retailcrm/retailcrm.php index 826529d..d0a29a0 100644 --- a/retailcrm/retailcrm.php +++ b/retailcrm/retailcrm.php @@ -853,19 +853,20 @@ class RetailCRM extends Module return false; } elseif (isset($params['newOrderStatus'])) { + $order = [ + 'externalId' => $params['id_order'], + ]; + $statusCode = $params['newOrderStatus']->id; if (array_key_exists($statusCode, $status) && !empty($status[$statusCode])) { - $orderStatus = $status[$statusCode]; + $order['status'] = $status[$statusCode]; } - if (isset($orderStatus)) { - $this->api->ordersEdit( - [ - 'externalId' => $params['id_order'], - 'status' => $orderStatus, - ] - ); + $order = RetailcrmTools::filter('RetailcrmFilterOrderStatusUpdate', $order, $params); + + if (isset($order['externalId']) && 1 < count($order)) { + $this->api->ordersEdit($order); return true; } diff --git a/tests/RetailcrmTest.php b/tests/RetailcrmTest.php index 7cad122..bb879be 100644 --- a/tests/RetailcrmTest.php +++ b/tests/RetailcrmTest.php @@ -234,7 +234,7 @@ class RetailCRMTest extends RetailcrmTestCase $cart = $this->createMock('Cart'); $cart->expects($this->any())->method('getProducts')->willReturn($this->getProducts()); $cart->expects($this->any())->method('getAddressCollection')->willReturn($this->getAddressCollection()); - $status = new StdClass(); + $status = new stdClass(); $reference = $order->reference; $updReference = 'test';