Skip to content

Commit

Permalink
ref #89649 Coupon transfer (#312)
Browse files Browse the repository at this point in the history
  • Loading branch information
Kocmonavtik authored Sep 1, 2023
1 parent 4d3883f commit 0566309
Show file tree
Hide file tree
Showing 14 changed files with 194 additions and 10 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
## 2023-08-31 4.6.11
* Added the ability to work with coupons through the CRM system

## 2023-07-19 4.6.10
* Abandoned cart transfer fix

Expand Down
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
4.6.10
4.6.11
15 changes: 15 additions & 0 deletions resources/pot/retailcrm-es_ES.pot
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,21 @@ msgstr "Ajustes del stock"
msgid "Statuses"
msgstr "Los estados"

msgid "Coupon"
msgstr "Cupón"

msgid "When working with coupons via CRM, it is impossible to transfer manual discounts."
msgstr "El trabajo con cupones a través del CRM no permite transferir descuentos manuales."

msgid "The user field must be in the String or Text format."
msgstr "El campo personalizado debe tener el formato Hilo o Texto."

msgid "When using multiple coupons, separation is supported using spaces, line breaks, characters `;` `,`."
msgstr "Si se usan varios cupones, es posible separarlos por espacios, nueva línea o caracteres `;` `,`."

msgid "For example: code_coupon_1; code_coupon_2, code_coupon_3 code_coupon_4"
msgstr "Por ejemplo, code_coupon_1; code_coupon_2, code_coupon_3 code_coupon_4"

msgid "Payment types"
msgstr "Métodos de pago"

Expand Down
15 changes: 15 additions & 0 deletions resources/pot/retailcrm-ru_RU.pot
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,21 @@ msgstr "Настройка управления остатками"
msgid "Statuses"
msgstr "Статусы"

msgid "Coupon"
msgstr "Купон"

msgid "When working with coupons via CRM, it is impossible to transfer manual discounts."
msgstr "При работе с купонами через CRM невозможно передавать ручные скидки."

msgid "The user field must be in the String or Text format."
msgstr "Пользовательское поле должно быть формата Строка или Текст."

msgid "When using multiple coupons, separation is supported using spaces, line breaks, characters `;` `,`."
msgstr "При использовании нескольких купонов, поддерживается разделение с помощью пробелов, переноса строки, символами `;` `,`."

msgid "For example: code_coupon_1; code_coupon_2, code_coupon_3 code_coupon_4"
msgstr "Например: code_coupon_1; code_coupon_2, code_coupon_3 code_coupon_4"

msgid "Payment types"
msgstr "Способы оплаты"

Expand Down
46 changes: 46 additions & 0 deletions src/include/abstracts/class-wc-retailcrm-abstracts-settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,52 @@ public function init_form_fields()
}
}

/**
* Coupon options
*/
$coupon_option_list = ['not-upload' => __("Don't send to CRM", 'retailcrm')];
$retailcrm_metaFiels_list = $this->apiClient->customFieldsList(
['entity' => 'order', 'type' => ['string', 'text']]
);

if (!empty($retailcrm_metaFiels_list) && $retailcrm_metaFiels_list->isSuccessful()) {
foreach ($retailcrm_metaFiels_list['customFields'] as $retailcrm_metaField) {
$coupon_option_list[$retailcrm_metaField['code']] = $retailcrm_metaField['name'];
}

$this->form_fields[] = [
'title' => __("Coupon", 'retailcrm'),
'type' => 'heading',
'description' => '',
'id' => 'coupon_options'
];

$this->form_fields['coupon_notification'] = [
'id' => 'coupon_options',
'css' => 'max-width:400px;resize: none;height:215px;',
'type' => 'textarea',
'title' => __('Attention!', 'retailcrm'),
'value' => '',
'placeholder' => __('When working with coupons via CRM, it is impossible to transfer manual discounts.', 'retailcrm') .
PHP_EOL . PHP_EOL .
__('The user field must be in the String or Text format.', 'retailcrm') .
PHP_EOL .
__('When using multiple coupons, separation is supported using spaces, line breaks, characters `;` `,`.', 'retailcrm') .
PHP_EOL .
__('For example: code_coupon_1; code_coupon_2, code_coupon_3 code_coupon_4', 'retailcrm'),
'custom_attributes' => ['readonly' => 'readonly'],
];

$this->form_fields['woo_coupon_apply_field'] = [
'title' => __('Coupon', 'retailcrm'),
'css' => 'min-width:350px;',
'class' => 'select',
'type' => 'select',
'options' => $coupon_option_list,
'desc_tip' => true,
];
}

/**
* Meta data options
*/
Expand Down
93 changes: 87 additions & 6 deletions src/include/class-wc-retailcrm-history.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ class WC_Retailcrm_History
/** @var string */
protected $bindField = 'externalId';

/** @var bool */
protected $recalculateCoupons = false;

/**
* WC_Retailcrm_History constructor.
*
Expand Down Expand Up @@ -270,19 +273,30 @@ protected function ordersHistory()
$this->updateMetaData($customFields, $order, $wcOrder);

$wcOrderNumber = $wcOrder->get_order_number();
$orderEditData = [];

if (
$order['number'] != $wcOrderNumber
&& isset($this->retailcrmSettings['update_number'])
&& $this->retailcrmSettings['update_number'] == WC_Retailcrm_Base::YES
) {
$this->retailcrm->ordersEdit(
['id' => $order['id'], 'number' => $wcOrderNumber],
'id'
);
$orderEditData['number'] = $wcOrderNumber;
}

$items = $this->updateItemsForUsedCoupons($orderHistory, $wcOrder);

if (!empty($items)) {
$orderEditData['items'] = $items;
$orderEditData['discountManualAmount'] = 0;
$orderEditData['discountManualPercent'] = 0;
}

if (!empty($orderEditData)) {
$orderEditData['id'] = $order['id'];

$this->retailcrm->ordersEdit($orderEditData, 'id');
}
}
// @codeCoverageIgnoreStart
} catch (Exception $exception) {
WC_Retailcrm_Logger::add(
sprintf(
Expand All @@ -294,7 +308,6 @@ protected function ordersHistory()

continue;
}
// @codeCoverageIgnoreEnd
}
} else {
break;
Expand Down Expand Up @@ -454,6 +467,8 @@ protected function orderUpdate($order, $options)
}

if (array_key_exists('items', $order)) {
$this->recalculateCoupons = true;

foreach ($order['items'] as $key => $crmProduct) {
if (!isset($crmProduct['offer'][$this->bindField])) {
continue;
Expand Down Expand Up @@ -999,6 +1014,72 @@ protected function editOrder($order, $event = 'create')
}
}

/**
* Checks use coupons and updates offers
*
* @param array $orderHistory
* @param array $wcOrder
*
* @return array
*/
private function updateItemsForUsedCoupons($orderHistory, $wcOrder)
{
$couponField = $this->retailcrmSettings['woo_coupon_apply_field'];

if ($couponField === 'not-upload') {
return [];
}

$rewriteItems = false;
$wcOrderCoupons = $wcOrder->get_coupon_codes();

if (isset($orderHistory['customFields'])
&& array_key_exists($couponField, $orderHistory['customFields'])
&& empty($orderHistory['customFields'][$couponField])
&& $wcOrderCoupons
) {
foreach ($wcOrderCoupons as $code) {
$wcOrder->remove_coupon($code);

$rewriteItems = true;
}
}

if (!empty($orderHistory['customFields'][$couponField])) {
$masCoupons = preg_split("/[\s,;]+/", $orderHistory['customFields'][$couponField]);

foreach (array_diff($masCoupons, $wcOrderCoupons) as $coupon) {
$wcOrder->apply_coupon($coupon);

$rewriteItems = true;
}

foreach (array_diff($wcOrderCoupons, $masCoupons) as $coupon) {
$wcOrder->remove_coupon($coupon);

$rewriteItems = true;
}
}

if (($rewriteItems || $this->recalculateCoupons)
&& ($wcOrderCoupons || !empty($orderHistory['customFields'][$couponField]))) {
$wcOrder->recalculate_coupons();

$orderItem = new WC_Retailcrm_Order_Item($this->retailcrmSettings);
$orderItems = [];

foreach ($wcOrder->get_items() as $item) {
$orderItems[] = $orderItem->build($item)->getData();

$orderItem->resetData();
}

return $orderItems;
}

return [];
}

/**
* Returns data for address_1 and address_2(if exist data for this field) for WC order.
*
Expand Down
14 changes: 14 additions & 0 deletions src/include/class-wc-retailcrm-orders.php
Original file line number Diff line number Diff line change
Expand Up @@ -435,6 +435,20 @@ protected function processOrder($order, $update = false)
}
}

$couponCustomField = $this->retailcrm_settings['woo_coupon_apply_field'];

if ($couponCustomField !== 'not-upload') {
$codeCoupons = [];

foreach ($order->get_coupons() as $coupon) {
if (!empty($coupon->get_code())) {
$codeCoupons[] = $coupon->get_code();
}
}

$orderData['customFields'][$couponCustomField] = implode('; ', $codeCoupons);
}

$this->order = WC_Retailcrm_Plugin::clearArray($orderData);
$this->processOrderCustomerInfo($order, $update);

Expand Down
Binary file modified src/languages/retailcrm-es_ES.mo
Binary file not shown.
Binary file modified src/languages/retailcrm-ru_RU.mo
Binary file not shown.
5 changes: 4 additions & 1 deletion src/readme.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ Tags: Интеграция, Simla.com, simla
Requires PHP: 7.0
Requires at least: 5.3
Tested up to: 6.2
Stable tag: 4.6.10
Stable tag: 4.6.11
License: GPLv1 or later
License URI: http://www.gnu.org/licenses/gpl-1.0.html

Expand Down Expand Up @@ -82,6 +82,9 @@ Asegúrate de tener una clave API específica para cada tienda. Las siguientes i


== Changelog ==
= 4.6.11 =
* Added the ability to work with coupons through the CRM system

= 4.6.10 =
* Abandoned cart transfer fix

Expand Down
2 changes: 1 addition & 1 deletion src/retailcrm.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* Description: Integration plugin for WooCommerce & Simla.com
* Author: RetailDriver LLC
* Author URI: http://retailcrm.pro/
* Version: 4.6.10
* Version: 4.6.11
* Tested up to: 6.2
* WC requires at least: 5.4
* WC tested up to: 7.8
Expand Down
2 changes: 1 addition & 1 deletion src/uninstall.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
*
* @link https://wordpress.org/plugins/woo-retailcrm/
*
* @version 4.6.10
* @version 4.6.11
*
* @package RetailCRM
*/
Expand Down
1 change: 1 addition & 0 deletions tests/helpers/class-wc-retailcrm-test-case-helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ protected function setOptions()
),
'product_description' => 'full',
'stores_for_uploading' => ['woocommerce', 'main'],
'woo_coupon_apply_field' => 'testField',
];

update_option(WC_Retailcrm_Base::$option_key, $options);
Expand Down
6 changes: 6 additions & 0 deletions tests/test-wc-retailcrm-orders.php
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ public function test_order_create($retailcrm)
$this->assertEquals($orderData['customFields']['crm_order'], 'test_custom_fields');
$this->assertEquals($orderData['customerComment'], 'crm_customer_comment_test');
$this->assertEquals($orderData['delivery']['address']['text'], 'crm_address_text_test');
$this->assertEquals($orderData['customFields']['testField'], 'test1; test2');
} else {
$this->assertEquals(null, $order);
}
Expand Down Expand Up @@ -596,6 +597,11 @@ private function createTestOrder()
{
/** @var WC_Order order */
$this->order = WC_Helper_Order::create_order(0);
$coupon1 = WC_Helper_Coupon::create_coupon('test1');
$coupon2 = WC_Helper_Coupon::create_coupon('test2');

$this->order->apply_coupon($coupon1);
$this->order->apply_coupon($coupon2);

foreach ($this->order->get_address('billing') as $prop => $value) {
if (method_exists($this->order, 'set_shipping_' . $prop)) {
Expand Down

0 comments on commit 0566309

Please sign in to comment.