1
0
mirror of synced 2025-01-18 08:51:41 +03:00

ref #89649 Coupon transfer (#312)

This commit is contained in:
Kocmonavtik 2023-09-01 12:24:57 +03:00 committed by GitHub
parent 4d3883fe45
commit 0566309c24
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
14 changed files with 194 additions and 10 deletions

View File

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

View File

@ -1 +1 @@
4.6.10
4.6.11

View File

@ -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"

View File

@ -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 "Способы оплаты"

View File

@ -341,6 +341,52 @@ abstract class WC_Retailcrm_Abstracts_Settings extends WC_Integration
}
}
/**
* 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
*/

View File

@ -31,6 +31,9 @@ if (!class_exists('WC_Retailcrm_History')) :
/** @var string */
protected $bindField = 'externalId';
/** @var bool */
protected $recalculateCoupons = false;
/**
* WC_Retailcrm_History constructor.
*
@ -270,19 +273,30 @@ if (!class_exists('WC_Retailcrm_History')) :
$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(
@ -294,7 +308,6 @@ if (!class_exists('WC_Retailcrm_History')) :
continue;
}
// @codeCoverageIgnoreEnd
}
} else {
break;
@ -454,6 +467,8 @@ if (!class_exists('WC_Retailcrm_History')) :
}
if (array_key_exists('items', $order)) {
$this->recalculateCoupons = true;
foreach ($order['items'] as $key => $crmProduct) {
if (!isset($crmProduct['offer'][$this->bindField])) {
continue;
@ -999,6 +1014,72 @@ if (!class_exists('WC_Retailcrm_History')) :
}
}
/**
* 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.
*

View File

@ -435,6 +435,20 @@ if (!class_exists('WC_Retailcrm_Orders')) :
}
}
$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);

Binary file not shown.

Binary file not shown.

View File

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

View File

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

View File

@ -16,7 +16,7 @@
*
* @link https://wordpress.org/plugins/woo-retailcrm/
*
* @version 4.6.10
* @version 4.6.11
*
* @package RetailCRM
*/

View File

@ -79,6 +79,7 @@ class WC_Retailcrm_Test_Case_Helper extends WC_Unit_Test_Case
),
'product_description' => 'full',
'stores_for_uploading' => ['woocommerce', 'main'],
'woo_coupon_apply_field' => 'testField',
];
update_option(WC_Retailcrm_Base::$option_key, $options);

View File

@ -120,6 +120,7 @@ class WC_Retailcrm_Orders_Test extends WC_Retailcrm_Test_Case_Helper
$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);
}
@ -596,6 +597,11 @@ class WC_Retailcrm_Orders_Test extends WC_Retailcrm_Test_Case_Helper
{
/** @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)) {