Added filter for order status update hook

This commit is contained in:
max-baranikov 2022-03-04 12:40:08 +03:00 committed by GitHub
parent 9ef8d48dfc
commit 805e2414a0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 58 additions and 49 deletions

View File

@ -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
<...>
<?php
// custom/filters/RetailcrmFilterOrderStatusUpdate.php
class RetailcrmFilterOrderStatusUpdate implements RetailcrmFilterInterface
{
/**
* {@inheritDoc}
*/
public static function filter($object, array $parameters)
{
// get data from Order object
$order = new Order($parameters['id_order']);
$trackingNumbers = [];
foreach ($order->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;
}
}
```

View File

@ -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 `<prestashop-root>/modules/retailcrm/custom/hooks`. Filename and classname must match the filter name.
To use filters you should define a new class in `<prestashop-root>/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
<?php
// custom/hooks/RetailcrmFilterSaveCustomerAddress.php
class RetailcrmFilterSaveCustomerAddress implements RetailcrmFilterInterface
{
/**
* @inheritDoc
*/
public static function filter($object, array $parameters)
{
/**
* @var array $dataCrm CRM address data
* @var Address $object CMS Address object
*/
$dataCrm = $parameters['dataCrm'];
if (isset($dataCrm['dni'])) {
$object->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

View File

@ -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;
}

View File

@ -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';