diff --git a/README.md b/README.md index cf98c11..1654243 100644 --- a/README.md +++ b/README.md @@ -23,3 +23,14 @@ Module allows integrate CMS Prestashop with [retailCRM](https://www.retailcrm.pr * This release contains an experimental feature "corporate customers". Use at your own risk. * This release only supports retailCRM API v5. + +#### Customization + +If you want to change the default behavior of a module classes and be sure that these changes won't be overwritten during the module upgrade process, you should **copy the original classes** that you are going to customize to the `prestashop-root/modules/retailcrm_custom/classes` directory. + +From here you can modify the methods of the classes for your own purposes, and they will not be affected during the module upgrade process. + +Keep in mind that: + +* If the logic and classes of the module have changed a lot after an upgrade, your customized logic may cause the module to malfunction. **You should always check for changes after an upgrade and update your customized classes if needed.** +* This feature does not allow to customize the base class (file `retailcrm.php`). For this you can use the standard Prestashop override feature. diff --git a/retailcrm/bootstrap.php b/retailcrm/bootstrap.php index d867b64..5c80da4 100644 --- a/retailcrm/bootstrap.php +++ b/retailcrm/bootstrap.php @@ -60,6 +60,12 @@ class RetailcrmAutoloader */ protected static $pathTop; + /** + * The top level directory where recursion for custom classes will begin. + * + */ + protected static $pathTopCustom; + /** * Autoload function for registration with spl_autoload_register * @@ -70,6 +76,21 @@ class RetailcrmAutoloader */ public static function loader($className) { + if (file_exists(self::$pathTopCustom) && is_dir(self::$pathTopCustom)) { + $directory = new RecursiveDirectoryIterator(self::$pathTopCustom); + $fileIterator = new RecursiveIteratorIterator($directory); + $filename = $className . self::$fileExt; + + foreach ($fileIterator as $file) { + if (Tools::strtolower($file->getFilename()) === Tools::strtolower($filename) + && $file->isReadable() + && !class_exists($className) + ) { + include_once $file->getPathname(); + } + } + } + $directory = new RecursiveDirectoryIterator(self::$pathTop); $fileIterator = new RecursiveIteratorIterator($directory); $filename = $className . self::$fileExt; @@ -104,8 +125,20 @@ class RetailcrmAutoloader { self::$pathTop = $path; } + + /** + * Sets the $pathTopCustom property + * + * @param string $path The path representing the top level where recursion for custom + * classes should begin. Defaults to 'modules/retailcrm_custom/classes'. + */ + public static function setPathCustom($path) + { + self::$pathTopCustom = $path; + } } RetailcrmAutoloader::setPath(realpath(dirname(__FILE__))); +RetailcrmAutoloader::setPathCustom(realpath(_PS_MODULE_DIR_ . '/retailcrm_custom/classes')); RetailcrmAutoloader::setFileExt('.php'); spl_autoload_register('RetailcrmAutoloader::loader'); diff --git a/retailcrm/lib/RetailcrmOrderBuilder.php b/retailcrm/lib/RetailcrmOrderBuilder.php index 44ae657..af4a406 100644 --- a/retailcrm/lib/RetailcrmOrderBuilder.php +++ b/retailcrm/lib/RetailcrmOrderBuilder.php @@ -894,6 +894,8 @@ class RetailcrmOrderBuilder } } + $crmOrder['discountManualAmount'] = round($order->total_discounts, 2); + if (isset($payment[$paymentType]) && !empty($payment[$paymentType])) { $order_payment = array( 'externalId' => $order->id . '#' . $order->reference, @@ -901,8 +903,6 @@ class RetailcrmOrderBuilder 'type' => $payment[$paymentType] ); - $crmOrder['discountManualAmount'] = round($order->total_discounts, 2); - if (((float) $crmOrder['discountManualAmount']) > ((float) $order_payment['amount'])) { $crmOrder['discountManualAmount'] = $order_payment['amount']; }