commit
8e888dcc2f
@ -1,3 +1,7 @@
|
||||
## 2018-08-06 v3.3.0
|
||||
* Переработана механика обработки истории изменений (добавлена склейка всех изменений)
|
||||
* Добавлен фильтр "retailcrm_history_before_save" для модификации данных истории
|
||||
|
||||
## 2018-07-19 v3.2.0
|
||||
* Улучшен метод выборки данных о доставках и оплатах в настройках плагина. (Выбираются все типы оплат, а не только разрешенные. Доставки, которые созданы для отдельных зон, передаются как сервисы)
|
||||
* Исправлены ошибки при обработке истории изменений
|
||||
|
106
src/config/objects.xml
Normal file
106
src/config/objects.xml
Normal file
@ -0,0 +1,106 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<options>
|
||||
<fields>
|
||||
<field id="id" group="customer">id</field>
|
||||
<field id="first_name" group="customer">firstName</field>
|
||||
<field id="last_name" group="customer">lastName</field>
|
||||
<field id="patronymic" group="customer">patronymic</field>
|
||||
<field id="email" group="customer">email</field>
|
||||
<field id="birthday" group="customer">birthday</field>
|
||||
<field id="phones" group="customer">phones</field>
|
||||
<field id="manager" group="customer">manager</field>
|
||||
<field id="commentary" group="customer">commentary</field>
|
||||
<field id="external_id" group="customer">externalId</field>
|
||||
<field id="cumulative_discount" group="customer">cumulativeDiscount</field>
|
||||
<field id="personal_discount" group="customer">personalDiscount</field>
|
||||
<field id="discount_card_number" group="customer">discountCardNumber</field>
|
||||
|
||||
<field id="address.index" group="customerAddress">index</field>
|
||||
<field id="address.country" group="customerAddress">country</field>
|
||||
<field id="address.region" group="customerAddress">region</field>
|
||||
<field id="address.city" group="customerAddress">city</field>
|
||||
<field id="address.street" group="customerAddress">street</field>
|
||||
<field id="address.building" group="customerAddress">building</field>
|
||||
<field id="address.house" group="customerAddress">house</field>
|
||||
<field id="address.block" group="customerAddress">block</field>
|
||||
<field id="address.flat" group="customerAddress">flat</field>
|
||||
<field id="address.floor" group="customerAddress">floor</field>
|
||||
<field id="address.intercom_code" group="customerAddress">intercomCode</field>
|
||||
<field id="address.metro" group="customerAddress">metro</field>
|
||||
<field id="address.notes" group="customerAddress">notes</field>
|
||||
|
||||
<field id="contragent.contragent_type" group="customerContragent">contragentType</field>
|
||||
<field id="contragent.legal_name" group="customerContragent">legalName</field>
|
||||
<field id="contragent.legal_address" group="customerContragent">legalAddress</field>
|
||||
<field id="contragent.certificate_number" group="customerContragent">certificateNumber</field>
|
||||
<field id="contragent.certificate_date" group="customerContragent">certificateDate</field>
|
||||
<field id="contragent.bank" group="customerContragent">bank</field>
|
||||
<field id="contragent.bank_address" group="customerContragent">bankAddress</field>
|
||||
<field id="contragent.corr_account" group="customerContragent">corrAccount</field>
|
||||
<field id="contragent.bank_account" group="customerContragent">bankAccount</field>
|
||||
|
||||
<field id="id" group="order">id</field>
|
||||
<field id="created_at" group="order">createdAt</field>
|
||||
<field id="order_type" group="order">orderType</field>
|
||||
<field id="order_method" group="order">orderMethod</field>
|
||||
<field id="site" group="order">site</field>
|
||||
<field id="status" group="order">status</field>
|
||||
<field id="manager" group="order">manager</field>
|
||||
<field id="first_name" group="order">firstName</field>
|
||||
<field id="last_name" group="order">lastName</field>
|
||||
<field id="patronymic" group="order">patronymic</field>
|
||||
<field id="phone" group="order">phone</field>
|
||||
<field id="additional_phone" group="order">additionalPhone</field>
|
||||
<field id="email" group="order">email</field>
|
||||
<field id="payment_type" group="order">paymentType</field>
|
||||
<field id="payment_status" group="order">paymentStatus</field>
|
||||
<field id="discount" group="order">discount</field>
|
||||
<field id="discount_percent" group="order">discountPercent</field>
|
||||
<field id="prepay_sum" group="order">prepaySum</field>
|
||||
<field id="customer_comment" group="order">customerComment</field>
|
||||
<field id="manager_comment" group="order">managerComment</field>
|
||||
<field id="shipment_store" group="order">shipmentStore</field>
|
||||
<field id="shipment_date" group="order">shipmentDate</field>
|
||||
<field id="shipped" group="order">shipped</field>
|
||||
<!--<field id="order_product" group="order">item</field>-->
|
||||
<field id="payment" group="order">payment</field>
|
||||
<field id="payments.amount" group="order">amount</field>
|
||||
<field id="payments.paid_at" group="order">paidAt</field>
|
||||
<field id="payments.comment" group="order">comment</field>
|
||||
<field id="payments.type" group="order">type</field>
|
||||
<field id="payments.status" group="order">status</field>
|
||||
|
||||
<field id="order_product.id" group="item">id</field>
|
||||
<field id="order_product.initial_price" group="item">initialPrice</field>
|
||||
<field id="order_product.discount" group="item">discount</field>
|
||||
<field id="order_product.discount_percent" group="item">discountPercent</field>
|
||||
<field id="order_product.quantity" group="item">quantity</field>
|
||||
<field id="order_product.status" group="item">status</field>
|
||||
<field id="order_product.summ" group="item">summ</field>
|
||||
|
||||
<field id="delivery_type" group="delivery">code</field>
|
||||
<field id="delivery_service" group="delivery">service</field>
|
||||
<field id="delivery_date" group="delivery">date</field>
|
||||
<field id="delivery_time" group="delivery">time</field>
|
||||
<field id="delivery_cost" group="delivery">cost</field>
|
||||
<field id="delivery_net_cost" group="delivery">netCost</field>
|
||||
|
||||
<field id="delivery_address.country" group="orderAddress">country</field>
|
||||
<field id="delivery_address.index" group="orderAddress">index</field>
|
||||
<field id="delivery_address.region" group="orderAddress">region</field>
|
||||
<field id="delivery_address.city" group="orderAddress">city</field>
|
||||
<field id="delivery_address.street" group="orderAddress">street</field>
|
||||
<field id="delivery_address.building" group="orderAddress">building</field>
|
||||
<field id="delivery_address.house" group="orderAddress">house</field>
|
||||
<field id="delivery_address.block" group="orderAddress">block</field>
|
||||
<field id="delivery_address.flat" group="orderAddress">flat</field>
|
||||
<field id="delivery_address.floor" group="orderAddress">floor</field>
|
||||
<field id="delivery_address.intercom_code" group="orderAddress">intercomCode</field>
|
||||
<field id="delivery_address.metro" group="orderAddress">metro</field>
|
||||
<field id="delivery_address.notes" group="orderAddress">notes</field>
|
||||
|
||||
<field id="integration_delivery_data.status" group="integrationDelivery">status</field>
|
||||
<field id="integration_delivery_data.track_number" group="integrationDelivery">trackNumber</field>
|
||||
<field id="integration_delivery_data.courier" group="integrationDelivery">courier</field>
|
||||
</fields>
|
||||
</options>
|
@ -160,7 +160,7 @@ if ( ! class_exists( 'WC_Retailcrm_History' ) ) :
|
||||
* @param string $date
|
||||
* @param int $since_id
|
||||
*
|
||||
* @return null
|
||||
* @return boolean
|
||||
*/
|
||||
protected function ordersHistory($date, $since_id)
|
||||
{
|
||||
@ -174,22 +174,34 @@ if ( ! class_exists( 'WC_Retailcrm_History' ) ) :
|
||||
|
||||
if ($response->isSuccessful()) {
|
||||
if (empty($response['history'])) {
|
||||
return;
|
||||
return false;
|
||||
}
|
||||
|
||||
$history = $response['history'];
|
||||
$end_change = end($history);
|
||||
$new_since_id = $end_change['id'];
|
||||
|
||||
foreach ($history as $record) {
|
||||
if ($record['source'] == 'api' && $record['apiKey']['current'] == true) {
|
||||
continue;
|
||||
}
|
||||
$last_change = end($history);
|
||||
$historyAssembly = self::assemblyOrder($response['history']);
|
||||
|
||||
WC_Retailcrm_Plugin::$history_run = true;
|
||||
|
||||
foreach ($historyAssembly as $orderHistory) {
|
||||
$order = apply_filters('retailcrm_history_before_save', $orderHistory);
|
||||
|
||||
if (isset($order['deleted']) && $order['deleted'] == true) {
|
||||
continue;
|
||||
}
|
||||
|
||||
try {
|
||||
$this->orderEdit($record, $options);
|
||||
if (isset($order['externalId'])) {
|
||||
$wc_order_id = $this->orderUpdate($order, $options);
|
||||
} else {
|
||||
$wc_order_id = $this->orderCreate($order, $options);
|
||||
}
|
||||
|
||||
$wc_order = wc_get_order($wc_order_id);
|
||||
|
||||
if ($wc_order instanceof WC_Order) {
|
||||
$this->update_total($wc_order);
|
||||
}
|
||||
} catch (Exception $exception) {
|
||||
$logger = new WC_Logger();
|
||||
$logger->add('retailcrm',
|
||||
@ -199,328 +211,71 @@ if ( ! class_exists( 'WC_Retailcrm_History' ) ) :
|
||||
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
update_option('retailcrm_orders_history_since_id', $last_change['id']);
|
||||
WC_Retailcrm_Plugin::$history_run = false;
|
||||
}
|
||||
}
|
||||
|
||||
if (empty($response)) {
|
||||
return;
|
||||
}
|
||||
|
||||
update_option('retailcrm_orders_history_since_id', $new_since_id);
|
||||
}
|
||||
|
||||
/**
|
||||
* Edit order in WC
|
||||
*
|
||||
* @param array $record
|
||||
* @param array $options
|
||||
* @return mixed
|
||||
*/
|
||||
protected function orderEdit($record, $options)
|
||||
{
|
||||
if ($record['field'] == 'status' && !empty($record['newValue']) && !empty($record['oldValue'])) {
|
||||
$newStatus = $record['newValue']['code'];
|
||||
if (!empty($options[$newStatus]) && !empty($record['order']['externalId'])) {
|
||||
$order = wc_get_order($record['order']['externalId']);
|
||||
$order->update_status($options[$newStatus]);
|
||||
}
|
||||
}
|
||||
|
||||
elseif ($record['field'] == 'order_product' && $record['newValue']) {
|
||||
$product = wc_get_product($record['item']['offer']['externalId']);
|
||||
$order = wc_get_order($record['order']['externalId']);
|
||||
$order->add_product($product, $record['item']['quantity']);
|
||||
}
|
||||
|
||||
elseif ($record['field'] == 'order_product.quantity' && $record['newValue']) {
|
||||
$order = wc_get_order($record['order']['externalId']);
|
||||
$items = $order->get_items();
|
||||
|
||||
foreach ($items as $order_item_id => $item) {
|
||||
if ($item['variation_id'] != 0 ) {
|
||||
$offer_id = $item['variation_id'];
|
||||
} else {
|
||||
$offer_id = $item['product_id'];
|
||||
}
|
||||
if ($offer_id == $record['item']['offer']['externalId']) {
|
||||
wc_delete_order_item($order_item_id);
|
||||
$product = wc_get_product($offer_id);
|
||||
$order->add_product($product, $record['newValue']);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
elseif ($record['field'] == 'order_product' && !$record['newValue']) {
|
||||
$order = wc_get_order($record['order']['externalId']);
|
||||
$items = $order->get_items();
|
||||
|
||||
foreach ($items as $order_item_id => $item) {
|
||||
if ($item['variation_id'] != 0 ) {
|
||||
$offer_id = $item['variation_id'];
|
||||
} else {
|
||||
$offer_id = $item['product_id'];
|
||||
}
|
||||
if ($offer_id == $record['item']['offer']['externalId']) {
|
||||
wc_delete_order_item($order_item_id);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
elseif ($record['field'] == 'delivery_type'
|
||||
|| $record['field'] == 'delivery_cost'
|
||||
|| $record['field'] == 'delivery_net_cost'
|
||||
|| $record['field'] == 'delivery_service'
|
||||
) {
|
||||
$newValue = isset($record['newValue']['code']) ? $record['newValue']['code'] : $record['newValue'];
|
||||
$this->updateShippingItemId($record['field'], $newValue, $record['order']['externalId'], $options);
|
||||
}
|
||||
|
||||
elseif ($record['field'] == 'delivery_address.region') {
|
||||
$order = wc_get_order($record['order']['externalId']);
|
||||
$order->set_shipping_state($record['newValue']);
|
||||
}
|
||||
|
||||
elseif ($record['field'] == 'delivery_address.city') {
|
||||
$order = wc_get_order($record['order']['externalId']);
|
||||
$order->set_shipping_city($record['newValue']);
|
||||
}
|
||||
|
||||
elseif ($record['field'] == 'delivery_address.street') {
|
||||
$order = wc_get_order($record['order']['externalId']);
|
||||
$order->set_shipping_address_1($record['newValue']);
|
||||
}
|
||||
|
||||
elseif ($record['field'] == 'delivery_address.building') {
|
||||
$order = wc_get_order($record['order']['externalId']);
|
||||
$order->set_shipping_address_2($record['newValue']);
|
||||
}
|
||||
|
||||
elseif ($record['field'] == 'payment_type') {
|
||||
$order = wc_get_order($record['order']['externalId']);
|
||||
$newValue = $record['newValue']['code'];
|
||||
if (!empty($options[$newValue]) && !empty($record['order']['externalId'])) {
|
||||
$payment = WC_Payment_Gateways::instance();
|
||||
$payment_types = $payment->payment_gateways();
|
||||
|
||||
if (isset($payment_types[$options[$newValue]])) {
|
||||
$order->set_payment_method($payment_types[$options[$newValue]]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
elseif ($record['field'] == 'payments') {
|
||||
$response = $this->retailcrm->ordersGet($record['order']['externalId']);
|
||||
|
||||
if ($response->isSuccessful()) {
|
||||
$order_data = $response['order'];
|
||||
$order = wc_get_order($record['order']['externalId']);
|
||||
$payment = WC_Payment_Gateways::instance();
|
||||
$payment_types = $payment->payment_gateways();
|
||||
|
||||
if (count($order_data['payments']) == 1) {
|
||||
$paymentType = end($order_data['payments']);
|
||||
if (isset($payment_types[$options[$paymentType['type']]])) {
|
||||
$payment_type = $payment_types[$options[$paymentType['type']]];
|
||||
$order->set_payment_method($payment_type);
|
||||
}
|
||||
} else {
|
||||
foreach ($order_data['payments'] as $payment_data) {
|
||||
if (isset($payment_data['externalId'])) {
|
||||
$paymentType = $payment_data;
|
||||
}
|
||||
}
|
||||
|
||||
if (!isset($paymentType)) {
|
||||
$paymentType = $order_data['payments'][0];
|
||||
}
|
||||
|
||||
if (isset($payment_types[$options[$paymentType['type']]])) {
|
||||
$order->set_payment_method($payment_types[$options[$paymentType['type']]]);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
elseif (isset($record['created'])
|
||||
&& $record['created'] == 1
|
||||
&& !isset($record['order']['externalId'])
|
||||
) {
|
||||
if (is_array($this->order_methods)
|
||||
&& $this->order_methods
|
||||
&& isset($record['order']['orderMethod'])
|
||||
&& !in_array($record['order']['orderMethod'], $this->order_methods)
|
||||
) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$args = array(
|
||||
'status' => isset($options[$record['order']['status']]) ?
|
||||
isset($options[$record['order']['status']]) :
|
||||
'processing',
|
||||
'customer_id' => isset($record['order']['customer']['externalId']) ?
|
||||
$record['order']['customer']['externalId'] :
|
||||
null
|
||||
);
|
||||
|
||||
$order_record = $record['order'];
|
||||
$order = wc_create_order($args);
|
||||
|
||||
$address_shipping = array(
|
||||
'first_name' => $order_record['firstName'],
|
||||
'last_name' => isset($order_record['lastName']) ? $order_record['lastName'] : '',
|
||||
'company' => '',
|
||||
'address_1' => isset($order_record['delivery']['address']['text']) ? $order_record['delivery']['address']['text'] : '',
|
||||
'address_2' => '',
|
||||
'city' => isset($order_record['delivery']['address']['city']) ? $order_record['delivery']['address']['city'] : '',
|
||||
'state' => isset($order_record['delivery']['address']['region']) ? $order_record['delivery']['address']['region'] : '',
|
||||
'postcode' => isset($order_record['delivery']['address']['index']) ? $order_record['delivery']['address']['index'] : '',
|
||||
'country' => $order_record['delivery']['address']['countryIso']
|
||||
);
|
||||
|
||||
$address_billing = array(
|
||||
'first_name' => $order_record['customer']['firstName'],
|
||||
'last_name' => isset($order_record['customer']['lastName']) ? $order_record['customer']['lastName'] : '',
|
||||
'company' => '',
|
||||
'email' => isset($order_record['customer']['email']) ? $order_record['customer']['email'] : '',
|
||||
'phone' => isset($order_record['customer']['phones'][0]['number']) ? $order_record['customer']['phones'][0]['number'] : '',
|
||||
'address_1' => isset($order_record['customer']['address']['text']) ? $order_record['customer']['address']['text'] : '',
|
||||
'address_2' => '',
|
||||
'city' => isset($order_record['customer']['address']['city']) ? $order_record['customer']['address']['city'] : '',
|
||||
'state' => isset($order_record['customer']['address']['region']) ? $order_record['customer']['address']['region'] : '',
|
||||
'postcode' => isset($order_record['customer']['address']['index']) ? $order_record['customer']['address']['index'] : '',
|
||||
'country' => $order_record['customer']['address']['countryIso']
|
||||
);
|
||||
|
||||
if ($this->retailcrm_settings['api_version'] == 'v5') {
|
||||
if (isset($order_record['payments']) && $order_record['payments']) {
|
||||
$payment = WC_Payment_Gateways::instance();
|
||||
|
||||
if (count($order_record['payments']) == 1) {
|
||||
$payment_types = $payment->payment_gateways();
|
||||
$payments = $order_record['payments'];
|
||||
$paymentType = end($payments);
|
||||
|
||||
if (isset($options[$paymentType['type']]) && isset($payment_types[$options[$paymentType['type']]])) {
|
||||
$order->set_payment_method($payment_types[$options[$paymentType['type']]]);
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
if (isset($order_record['paymentType']) && $order_record['paymentType']) {
|
||||
$payment = WC_Payment_Gateways::instance();
|
||||
$payment_types = $payment->payment_gateways();
|
||||
|
||||
if (isset($options[$order_record['paymentType']]) && isset($payment_types[$options[$order_record['paymentType']]])) {
|
||||
$order->set_payment_method($payment_types[$options[$order_record['paymentType']]]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$order->set_address($address_billing, 'billing');
|
||||
$order->set_address($address_shipping, 'shipping');
|
||||
$product_data = isset($order_record['items']) ? $order_record['items'] : array();
|
||||
|
||||
if ($product_data) {
|
||||
foreach ($product_data as $product) {
|
||||
$order->add_product(wc_get_product($product['offer']['externalId']), $product['quantity']);
|
||||
}
|
||||
}
|
||||
|
||||
if (array_key_exists('delivery', $order_record)) {
|
||||
$deliveryCode = isset($order_record['delivery']['code']) ? $order_record['delivery']['code'] : false;
|
||||
|
||||
if ($deliveryCode && isset($options[$deliveryCode])) {
|
||||
$shipping = new WC_Order_Item_Shipping();
|
||||
$shipping_methods = get_wc_shipping_methods();
|
||||
$shipping->set_method_title($shipping_methods[$options[$deliveryCode]]['name']);
|
||||
$shipping->set_method_id($options[$deliveryCode]);
|
||||
|
||||
if (isset($order_record['delivery']['service']['code'])) {
|
||||
$service = retailcrm_get_delivery_service(
|
||||
$shipping->get_method_id(),
|
||||
$order_record['delivery']['service']['code']
|
||||
);
|
||||
|
||||
if ($service) {
|
||||
$shipping->set_instance_id($order_record['delivery']['service']['code']);
|
||||
}
|
||||
}
|
||||
|
||||
if (!wc_tax_enabled()) {
|
||||
$shipping->set_total($order_record['delivery']['cost']);
|
||||
} else {
|
||||
$shipping->set_total($order_record['delivery']['netCost']);
|
||||
}
|
||||
|
||||
$shipping->set_order_id($order->get_id());
|
||||
|
||||
$shipping->save();
|
||||
$order->add_item($shipping);
|
||||
}
|
||||
}
|
||||
|
||||
$ids[] = array(
|
||||
'id' => (int)$order_record['id'],
|
||||
'externalId' => (int)$order->get_id()
|
||||
);
|
||||
|
||||
$this->retailcrm->ordersFixExternalIds($ids);
|
||||
}
|
||||
|
||||
if (isset($record['order']['externalId']) && !empty($record['order']['externalId'])) {
|
||||
$newOrder = wc_get_order($record['order']['externalId']);
|
||||
$this->update_total($newOrder);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Update shipping
|
||||
*
|
||||
* @param string $field
|
||||
* @param string $new_value
|
||||
* @param string $order_id
|
||||
* @param array $order
|
||||
* @param array $options
|
||||
* @param WC_Order $wc_order
|
||||
*
|
||||
* @return mixed
|
||||
* @return boolean
|
||||
*/
|
||||
protected function updateShippingItemId($field, $new_value, $order_id, $options)
|
||||
protected function updateShippingItemId($order, $options, $wc_order)
|
||||
{
|
||||
$order = wc_get_order($order_id);
|
||||
$shippings = $order->get_items('shipping');
|
||||
$shipping = reset($shippings);
|
||||
$create = false;
|
||||
|
||||
if ($field == 'delivery_type') {
|
||||
if (!isset($options[$new_value])) {
|
||||
$shippings = $wc_order->get_items('shipping');
|
||||
|
||||
if (!$shippings) {
|
||||
$shipping = new WC_Order_Item_Shipping();
|
||||
$create = true;
|
||||
} else {
|
||||
$shipping = reset($shippings);
|
||||
}
|
||||
|
||||
$data_store = $shipping->get_data_store();
|
||||
|
||||
if (isset($order['delivery']['code'])) {
|
||||
if (!isset($options[$order['delivery']['code']])) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$shipping_methods = get_wc_shipping_methods();
|
||||
$shipping->set_method_title($shipping_methods[$options[$new_value]]['name']);
|
||||
$shipping->set_method_id($options[$new_value]);
|
||||
$shipping->set_method_title($shipping_methods[$options[$order['delivery']['code']]]['name']);
|
||||
$shipping->set_method_id($options[$order['delivery']['code']]);
|
||||
}
|
||||
|
||||
if ($field == 'delivery_cost' && !wc_tax_enabled()) {
|
||||
$shipping->set_total($new_value);
|
||||
if (isset($order['delivery']['cost']) && !wc_tax_enabled()) {
|
||||
$shipping->set_total($order['delivery']['cost']);
|
||||
}
|
||||
|
||||
if ($field == 'delivery_net_cost' && wc_tax_enabled()) {
|
||||
$shipping->set_total($new_value);
|
||||
if (isset($order['delivery']['netCost']) && wc_tax_enabled()) {
|
||||
$shipping->set_total($order['delivery']['netCost']);
|
||||
}
|
||||
|
||||
if ($field == 'delivery_service') {
|
||||
$service = retailcrm_get_delivery_service($shipping->get_method_id(), $new_value);
|
||||
if (isset($order['delivery']['service']['code'])) {
|
||||
$service = retailcrm_get_delivery_service($shipping->get_method_id(), $order['delivery']['service']['code']);
|
||||
|
||||
if ($service) {
|
||||
$shipping->set_instance_id($new_value);
|
||||
$shipping->set_instance_id($order['delivery']['service']['code']);
|
||||
}
|
||||
}
|
||||
|
||||
$data_store = WC_Data_Store::load('order-item-shipping');
|
||||
if ($create === true) {
|
||||
$data_store->create($shipping);
|
||||
$shipping->set_order_id($wc_order->get_id());
|
||||
}
|
||||
|
||||
$data_store->update($shipping);
|
||||
$updateOrder = wc_get_order((int)$order->get_id());
|
||||
$this->update_total($updateOrder);
|
||||
|
||||
return true;
|
||||
}
|
||||
@ -536,5 +291,401 @@ if ( ! class_exists( 'WC_Retailcrm_History' ) ) :
|
||||
{
|
||||
$order->calculate_totals();
|
||||
}
|
||||
|
||||
/**
|
||||
* Update order in WC
|
||||
*
|
||||
* @param array $order
|
||||
* @param array $options
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
protected function orderUpdate($order, $options)
|
||||
{
|
||||
$wc_order = wc_get_order($order['externalId']);
|
||||
|
||||
if (!$wc_order instanceof WC_Order) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (isset($options[$order['status']])) {
|
||||
$wc_order->update_status($options[$order['status']]);
|
||||
}
|
||||
|
||||
if (array_key_exists('items', $order)) {
|
||||
foreach ($order['items'] as $item) {
|
||||
if (!isset($item['offer']['externalId'])) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (isset($item['create']) && $item['create'] == true) {
|
||||
$product = wc_get_product($item['offer']['externalId']);
|
||||
$wc_order->add_product($product, $item['quantity']);
|
||||
} else {
|
||||
foreach ($wc_order->get_items() as $order_item_id => $order_item) {
|
||||
if ($order_item['variation_id'] != 0 ) {
|
||||
$offer_id = $order_item['variation_id'];
|
||||
} else {
|
||||
$offer_id = $order_item['product_id'];
|
||||
}
|
||||
|
||||
if ($offer_id == $item['offer']['externalId']) {
|
||||
if (isset($item['delete']) && $item['delete'] == true) {
|
||||
wc_delete_order_item($order_item_id);
|
||||
} else {
|
||||
$order_item->set_quantity($item['quantity']);
|
||||
$product = wc_get_product($item['offer']['externalId']);
|
||||
$order_item->set_total($product->get_price() * $item['quantity']);
|
||||
$data_store = $order_item->get_data_store();
|
||||
$data_store->update($order_item);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (array_key_exists('delivery', $order)) {
|
||||
$this->updateShippingItemId($order, $options, $wc_order);
|
||||
|
||||
if (isset($order['delivery']['address'])) {
|
||||
$shipping_address = $order['delivery']['address'];
|
||||
|
||||
if (isset($shipping_address['region'])) {
|
||||
$wc_order->set_shipping_state($shipping_address['region']);
|
||||
}
|
||||
|
||||
if (isset($shipping_address['city'])) {
|
||||
$wc_order->set_shipping_city($shipping_address['city']);
|
||||
}
|
||||
|
||||
if (isset($shipping_address['street'])) {
|
||||
$wc_order->set_shipping_address_1($shipping_address['street']);
|
||||
}
|
||||
|
||||
if (isset($shipping_address['building'])) {
|
||||
$wc_order->set_shipping_address_2($shipping_address['building']);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (isset($order['paymentType'])) {
|
||||
if (!empty($options[$order['paymentType']])) {
|
||||
$payment = WC_Payment_Gateways::instance();
|
||||
$payment_types = $payment->payment_gateways();
|
||||
|
||||
if (isset($payment_types[$options[$order['paymentType']]])) {
|
||||
$order->set_payment_method($payment_types[$options[$order['paymentType']]]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (isset($order['payments']) && !empty($order['payments'])) {
|
||||
$payment = WC_Payment_Gateways::instance();
|
||||
$payment_types = $payment->payment_gateways();
|
||||
|
||||
if (count($order['payments']) == 1) {
|
||||
$paymentType = reset($order['payments']);
|
||||
|
||||
if (isset($paymentType['type'])
|
||||
&& isset($options[$paymentType['type']])
|
||||
&& isset($payment_types[$options[$paymentType['type']]])
|
||||
) {
|
||||
$payment_type = $payment_types[$options[$paymentType['type']]];
|
||||
$wc_order->set_payment_method($payment_type);
|
||||
}
|
||||
} else {
|
||||
foreach ($order['payments'] as $payment_data) {
|
||||
if (isset($payment_data['externalId'])) {
|
||||
$paymentType = $payment_data;
|
||||
}
|
||||
}
|
||||
|
||||
if (!isset($paymentType)) {
|
||||
$paymentType = $order['payments'][0];
|
||||
}
|
||||
|
||||
if (isset($payment_types[$options[$paymentType['type']]])) {
|
||||
$wc_order->set_payment_method($payment_types[$options[$paymentType['type']]]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $wc_order->get_id();
|
||||
}
|
||||
|
||||
/**
|
||||
* Create order in WC
|
||||
*
|
||||
* @param array $order
|
||||
* @param array $options
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
protected function orderCreate($order, $options)
|
||||
{
|
||||
if (!isset($order['create'])) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (is_array($this->order_methods)
|
||||
&& $this->order_methods
|
||||
&& isset($order['orderMethod'])
|
||||
&& !in_array($order['orderMethod'], $this->order_methods)
|
||||
) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$args = array(
|
||||
'status' => isset($options[$order['status']])
|
||||
? isset($options[$order['status']])
|
||||
: 'processing',
|
||||
'customer_id' => isset($order['customer']['externalId'])
|
||||
? $order['customer']['externalId']
|
||||
: null
|
||||
);
|
||||
|
||||
$wc_order = wc_create_order($args);
|
||||
|
||||
$address_shipping = array(
|
||||
'first_name' => isset($order['firstName']) ? $order['firstName'] : '',
|
||||
'last_name' => isset($order['lastName']) ? $order['lastName'] : '',
|
||||
'company' => '',
|
||||
'address_1' => isset($order['delivery']['address']['text']) ? $order['delivery']['address']['text'] : '',
|
||||
'address_2' => '',
|
||||
'city' => isset($order['delivery']['address']['city']) ? $order['delivery']['address']['city'] : '',
|
||||
'state' => isset($order['delivery']['address']['region']) ? $order['delivery']['address']['region'] : '',
|
||||
'postcode' => isset($order['delivery']['address']['index']) ? $order['delivery']['address']['index'] : '',
|
||||
'country' => isset($order['delivery']['address']['countryIso']) ? $order['delivery']['address']['countryIso'] : ''
|
||||
);
|
||||
|
||||
$address_billing = array(
|
||||
'first_name' => $order['customer']['firstName'],
|
||||
'last_name' => isset($order['customer']['lastName']) ? $order['customer']['lastName'] : '',
|
||||
'company' => '',
|
||||
'email' => isset($order['customer']['email']) ? $order['customer']['email'] : '',
|
||||
'phone' => isset($order['customer']['phones'][0]['number']) ? $order['customer']['phones'][0]['number'] : '',
|
||||
'address_1' => isset($order['customer']['address']['text']) ? $order['customer']['address']['text'] : '',
|
||||
'address_2' => '',
|
||||
'city' => isset($order['customer']['address']['city']) ? $order['customer']['address']['city'] : '',
|
||||
'state' => isset($order['customer']['address']['region']) ? $order['customer']['address']['region'] : '',
|
||||
'postcode' => isset($order['customer']['address']['index']) ? $order['customer']['address']['index'] : '',
|
||||
'country' => $order['customer']['address']['countryIso']
|
||||
);
|
||||
|
||||
if ($this->retailcrm_settings['api_version'] == 'v5') {
|
||||
if (isset($order['payments']) && $order['payments']) {
|
||||
$payment = WC_Payment_Gateways::instance();
|
||||
|
||||
if (count($order['payments']) == 1) {
|
||||
$payment_types = $payment->payment_gateways();
|
||||
$payments = $order['payments'];
|
||||
$paymentType = end($payments);
|
||||
|
||||
if (isset($options[$paymentType['type']]) && isset($payment_types[$options[$paymentType['type']]])) {
|
||||
$wc_order->set_payment_method($payment_types[$options[$paymentType['type']]]);
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
if (isset($order_record['paymentType']) && $order['paymentType']) {
|
||||
$payment = WC_Payment_Gateways::instance();
|
||||
$payment_types = $payment->payment_gateways();
|
||||
|
||||
if (isset($options[$order_record['paymentType']]) && isset($payment_types[$options[$order_record['paymentType']]])) {
|
||||
$wc_order->set_payment_method($payment_types[$options[$order['paymentType']]]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$wc_order->set_address($address_billing, 'billing');
|
||||
$wc_order->set_address($address_shipping, 'shipping');
|
||||
$product_data = isset($order['items']) ? $order['items'] : array();
|
||||
|
||||
if ($product_data) {
|
||||
foreach ($product_data as $product) {
|
||||
$wc_order->add_product(wc_get_product($product['offer']['externalId']), $product['quantity']);
|
||||
}
|
||||
}
|
||||
|
||||
if (array_key_exists('delivery', $order)) {
|
||||
$deliveryCode = isset($order['delivery']['code']) ? $order['delivery']['code'] : false;
|
||||
|
||||
if ($deliveryCode && isset($options[$deliveryCode])) {
|
||||
$shipping = new WC_Order_Item_Shipping();
|
||||
$shipping_methods = get_wc_shipping_methods();
|
||||
$shipping->set_method_title($shipping_methods[$options[$deliveryCode]]['name']);
|
||||
$shipping->set_method_id($options[$deliveryCode]);
|
||||
|
||||
if (isset($order['delivery']['service']['code'])) {
|
||||
$service = retailcrm_get_delivery_service(
|
||||
$shipping->get_method_id(),
|
||||
$order['delivery']['service']['code']
|
||||
);
|
||||
|
||||
if ($service) {
|
||||
$shipping->set_instance_id($order['delivery']['service']['code']);
|
||||
}
|
||||
}
|
||||
|
||||
if (!wc_tax_enabled()) {
|
||||
$shipping->set_total($order['delivery']['cost']);
|
||||
} else {
|
||||
$shipping->set_total($order['delivery']['netCost']);
|
||||
}
|
||||
|
||||
$shipping->set_order_id($wc_order->get_id());
|
||||
|
||||
$shipping->save();
|
||||
$wc_order->add_item($shipping);
|
||||
}
|
||||
}
|
||||
|
||||
$ids[] = array(
|
||||
'id' => (int) $order['id'],
|
||||
'externalId' => (int) $wc_order->get_id()
|
||||
);
|
||||
|
||||
$this->retailcrm->ordersFixExternalIds($ids);
|
||||
|
||||
return $wc_order->get_id();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $orderHistory
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public static function assemblyOrder($orderHistory)
|
||||
{
|
||||
if (file_exists(WP_CONTENT_DIR . '/plugins/woo-retailcrm/config/objects.xml')) {
|
||||
$objects = simplexml_load_file(WP_CONTENT_DIR . '/plugins/woo-retailcrm/config/objects.xml');
|
||||
foreach($objects->fields->field as $object) {
|
||||
$fields[(string)$object["group"]][(string)$object["id"]] = (string)$object;
|
||||
}
|
||||
}
|
||||
|
||||
$orders = array();
|
||||
|
||||
foreach ($orderHistory as $change) {
|
||||
$change['order'] = self::removeEmpty($change['order']);
|
||||
if(isset($change['order']['items']) && $change['order']['items']) {
|
||||
$items = array();
|
||||
foreach($change['order']['items'] as $item) {
|
||||
if(isset($change['created'])) {
|
||||
$item['create'] = 1;
|
||||
}
|
||||
$items[$item['id']] = $item;
|
||||
}
|
||||
$change['order']['items'] = $items;
|
||||
}
|
||||
|
||||
if(isset($change['order']['contragent']['contragentType']) && $change['order']['contragent']['contragentType']) {
|
||||
$change['order']['contragentType'] = $change['order']['contragent']['contragentType'];
|
||||
unset($change['order']['contragent']);
|
||||
}
|
||||
|
||||
if (!empty($orders) && isset($orders[$change['order']['id']])) {
|
||||
$orders[$change['order']['id']] = array_merge($orders[$change['order']['id']], $change['order']);
|
||||
} else {
|
||||
$orders[$change['order']['id']] = $change['order'];
|
||||
}
|
||||
|
||||
if (isset($change['item']) && $change['item']) {
|
||||
if(isset($orders[$change['order']['id']]['items'][$change['item']['id']])) {
|
||||
$orders[$change['order']['id']]['items'][$change['item']['id']] = array_merge($orders[$change['order']['id']]['items'][$change['item']['id']], $change['item']);
|
||||
} else {
|
||||
$orders[$change['order']['id']]['items'][$change['item']['id']] = $change['item'];
|
||||
}
|
||||
|
||||
if ($change['oldValue'] === null
|
||||
&& $change['field'] == 'order_product'
|
||||
) {
|
||||
$orders[$change['order']['id']]['items'][$change['item']['id']]['create'] = true;
|
||||
}
|
||||
|
||||
if ($change['newValue'] === null
|
||||
&& $change['field'] == 'order_product'
|
||||
) {
|
||||
$orders[$change['order']['id']]['items'][$change['item']['id']]['delete'] = true;
|
||||
}
|
||||
|
||||
if (!isset($orders[$change['order']['id']]['items'][$change['item']['id']]['create'])
|
||||
&& isset($fields['item'][$change['field']])
|
||||
&& $fields['item'][$change['field']]
|
||||
) {
|
||||
$orders[$change['order']['id']]['items'][$change['item']['id']][$fields['item'][$change['field']]] = $change['newValue'];
|
||||
}
|
||||
} else {
|
||||
if(isset($fields['delivery'][$change['field']]) && $fields['delivery'][$change['field']] == 'service'){
|
||||
$orders[$change['order']['id']]['delivery']['service']['code'] = self::newValue($change['newValue']);
|
||||
} elseif(isset($fields['delivery'][$change['field']]) && $fields['delivery'][$change['field']]) {
|
||||
$orders[$change['order']['id']]['delivery'][$fields['delivery'][$change['field']]] = self::newValue($change['newValue']);
|
||||
} elseif(isset($fields['orderAddress'][$change['field']]) && $fields['orderAddress'][$change['field']]){
|
||||
$orders[$change['order']['id']]['delivery']['address'][$fields['orderAddress'][$change['field']]] = $change['newValue'];
|
||||
} elseif(isset($fields['integrationDelivery'][$change['field']]) && $fields['integrationDelivery'][$change['field']]) {
|
||||
$orders[$change['order']['id']]['delivery']['service'][$fields['integrationDelivery'][$change['field']]] = self::newValue($change['newValue']);
|
||||
} elseif(isset($fields['customerContragent'][$change['field']]) && $fields['customerContragent'][$change['field']]) {
|
||||
$orders[$change['order']['id']][$fields['customerContragent'][$change['field']]] = self::newValue($change['newValue']);
|
||||
} elseif(strripos($change['field'], 'custom_') !== false) {
|
||||
$orders[$change['order']['id']]['customFields'][str_replace('custom_', '', $change['field'])] = self::newValue($change['newValue']);
|
||||
} elseif(isset($fields['order'][$change['field']]) && $fields['order'][$change['field']]) {
|
||||
$orders[$change['order']['id']][$fields['order'][$change['field']]] = self::newValue($change['newValue']);
|
||||
}
|
||||
|
||||
if(isset($change['created'])) {
|
||||
$orders[$change['order']['id']]['create'] = 1;
|
||||
}
|
||||
|
||||
if(isset($change['deleted'])) {
|
||||
$orders[$change['order']['id']]['deleted'] = 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $orders;
|
||||
}
|
||||
|
||||
public static function assemblyCustomer($customerHistory)
|
||||
{
|
||||
$customers = array();
|
||||
foreach ($customerHistory as $change) {
|
||||
$change['order'] = self::removeEmpty($change['customer']);
|
||||
|
||||
if (!empty($customers[$change['customer']['id']]) && $customers[$change['customer']['id']]) {
|
||||
$customers[$change['customer']['id']] = array_merge($customers[$change['customer']['id']], $change['customer']);
|
||||
} else {
|
||||
$customers[$change['customer']['id']] = $change['customer'];
|
||||
}
|
||||
}
|
||||
|
||||
return $customers;
|
||||
}
|
||||
|
||||
public static function newValue($value)
|
||||
{
|
||||
if (isset($value['code'])) {
|
||||
return $value['code'];
|
||||
} else {
|
||||
return $value;
|
||||
}
|
||||
}
|
||||
|
||||
public static function removeEmpty($inputArray)
|
||||
{
|
||||
$outputArray = array();
|
||||
if (!empty($inputArray)) {
|
||||
foreach ($inputArray as $key => $element) {
|
||||
if(!empty($element) || $element === 0 || $element === '0'){
|
||||
if (is_array($element)) {
|
||||
$element = self::removeEmpty($element);
|
||||
}
|
||||
$outputArray[$key] = $element;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $outputArray;
|
||||
}
|
||||
}
|
||||
endif;
|
||||
|
@ -271,6 +271,10 @@ if ( ! class_exists( 'WC_Retailcrm_Orders' ) ) :
|
||||
if (!empty($shipping_cost)) {
|
||||
$order_data['delivery']['cost'] = $shipping_cost;
|
||||
}
|
||||
|
||||
if ($shipping['total']) {
|
||||
$order_data['delivery']['netCost'] = $shipping['total'];
|
||||
}
|
||||
}
|
||||
|
||||
if ($this->retailcrm_settings['api_version'] != 'v5' && $order->is_paid()) {
|
||||
@ -379,7 +383,6 @@ if ( ! class_exists( 'WC_Retailcrm_Orders' ) ) :
|
||||
* Create payment in CRM
|
||||
*
|
||||
* @param WC_Order $order
|
||||
* @param int $order_id
|
||||
*
|
||||
* @return array $payment
|
||||
*/
|
||||
|
@ -45,6 +45,9 @@ API-ключ должен быть для отдельного магазина
|
||||
2. В появившихся списках справочников настройте соответствие способов доставки и оплаты, а так же статусов заказов. Отметьте галочку "Выгружать остатки", если хотите выгружать остатки из Retailcrm в магазин (подробнее смотрите в описании).
|
||||
|
||||
== Changelog ==
|
||||
= 3.3.0 =
|
||||
* Переработана механика обработки истории изменений
|
||||
|
||||
= 3.2.0 =
|
||||
* Улучшен метод выборки данных о доставках и оплатах в настройках плагина
|
||||
* Исправлены ошибки при обработке истории изменений
|
||||
@ -132,6 +135,9 @@ API-ключ должен быть для отдельного магазина
|
||||
|
||||
== Upgrade Notice ==
|
||||
|
||||
= 3.3.0 =
|
||||
Переработана механика обработки истории изменений
|
||||
|
||||
= 3.2.0 =
|
||||
Улучшен метод выборки данных о доставках и оплатах в настройках плагина
|
||||
Исправлены ошибки при обработке истории изменений
|
||||
|
@ -1,6 +1,6 @@
|
||||
<?php
|
||||
/**
|
||||
* Version: 3.2.0
|
||||
* Version: 3.3.0
|
||||
* WC requires at least: 3.0
|
||||
* WC tested up to: 3.4.3
|
||||
* Plugin Name: WooCommerce RetailCRM
|
||||
|
@ -15,7 +15,7 @@
|
||||
*
|
||||
*
|
||||
* @link https://wordpress.org/plugins/woo-retailcrm/
|
||||
* @version 3.2.0
|
||||
* @version 3.3.0
|
||||
*
|
||||
* @package RetailCRM
|
||||
*/
|
||||
|
Loading…
x
Reference in New Issue
Block a user