Skip to content

Commit

Permalink
Merge pull request #114 from Evgeniy-Goroh/master
Browse files Browse the repository at this point in the history
Поддержка передачи одинаковых товаров в заказе как разных товарных по…
  • Loading branch information
gwinn authored Oct 17, 2019
2 parents af86ba2 + 8518b43 commit 6fa5e3d
Show file tree
Hide file tree
Showing 7 changed files with 172 additions and 15 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
## 2019-10-07 3.5.4
* Добавлена возможность обработки одинаковых товарных позиций

## 2019-04-22 3.5.2
* Исправлен баг с выгрузкой заказов в retailCRM
* Исправлена ошибка переводов
Expand Down
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
3.5.3
3.5.4
134 changes: 128 additions & 6 deletions src/include/class-wc-retailcrm-history.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ class WC_Retailcrm_History
protected $order_methods = array();
protected $bind_field = 'externalId';

/** @var WC_Retailcrm_Order_Item */
protected $order_item;

/**
* WC_Retailcrm_History constructor.
* @param $retailcrm (default = false)
Expand Down Expand Up @@ -328,7 +331,8 @@ protected function orderUpdate($order, $options)
}

if (array_key_exists('items', $order)) {
foreach ($order['items'] as $item) {
foreach ($order['items'] as $key => $item) {

if (!isset($item['offer'][$this->bind_field])) {
continue;
}
Expand All @@ -339,18 +343,45 @@ protected function orderUpdate($order, $options)
$this->retailcrm_settings
);

foreach ($wc_order->get_items() as $order_item_id => $order_item) {
$arItemsOld[$order_item_id] = $order_item_id;
}

$wc_order->add_product($product, $item['quantity']);

foreach ($wc_order->get_items() as $order_item_id => $order_item) {
$arItemsNew[$order_item_id] = $order_item_id;
}

$result = end(array_diff($arItemsNew, $arItemsOld));
$order['items'][$key]['woocomerceId'] = $result;
} 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'][$this->bind_field]) {
$this->deleteOrUpdateOrderItem($item, $order_item, $order_item_id);
if (isset($item['externalIds'])) {
foreach ($item['externalIds'] as $externalId) {
if ($externalId['code'] == 'woocomerce') {
$itemExternalId = explode('_', $externalId['value']);
}
}
} else {
$itemExternalId = explode('_', $item['externalId']);
}

if ($offer_id == $item['offer'][$this->bind_field]
&& $itemExternalId[1] == $order_item->get_id()
) {
$this->deleteOrUpdateOrderItem($item, $order_item, $itemExternalId[1]);
}

}
}
}
Expand Down Expand Up @@ -424,6 +455,19 @@ protected function orderUpdate($order, $options)

$wc_order->save();

$checkNewItem = false;
foreach ($order['items'] as $item) {
if (!empty($item['externalIds'])) {
continue;
} else {
$checkNewItem = true;
}
}

if ($checkNewItem == true) {
$this->editOrder($this->retailcrm_settings, $wc_order, $order,'update');
}

return $wc_order->get_id();
}

Expand All @@ -440,11 +484,16 @@ private function deleteOrUpdateOrderItem($item, $order_item, $order_item_id)
if (isset($item['quantity']) && $item['quantity']) {
$order_item->set_quantity($item['quantity']);
$product = retailcrm_get_wc_product($item['offer'][$this->bind_field], $this->retailcrm_settings);
$order_item->set_total($product->get_price() * $item['quantity']);
$order_item->set_subtotal($product->get_price());
$data_store = $order_item->get_data_store();
$data_store->update($order_item);
}

if (isset($item['summ']) && $item['summ']) {
$order_item->set_total($item['summ']);
$data_store = $order_item->get_data_store();
$data_store->update($order_item);
}
}
}

Expand Down Expand Up @@ -536,11 +585,33 @@ protected function orderCreate($order, $options)
$product_data = isset($order['items']) ? $order['items'] : array();

if ($product_data) {
foreach ($product_data as $product) {
foreach ($product_data as $key => $product) {
$item = retailcrm_get_wc_product($product['offer'][$this->bind_field], $this->retailcrm_settings);
if ($product['discountTotal'] > 0) {
$item->set_price($product['initialPrice'] - $product['discountTotal']);
}

foreach ($wc_order->get_items() as $order_item_id => $order_item) {
$arItemsOld[$order_item_id] = $order_item_id;
}

$wc_order->add_product(
retailcrm_get_wc_product($product['offer'][$this->bind_field], $this->retailcrm_settings),
$item,
$product['quantity']
);

foreach ($wc_order->get_items() as $order_item_id => $order_item) {
$arItemsNew[$order_item_id] = $order_item_id;
}

if (!empty($arItemsOld)) {
$result = end(array_diff($arItemsNew, $arItemsOld));
} else {
$result = end($arItemsNew);
}

$order['items'][$key]['woocomerceId'] = $result;

}
}

Expand Down Expand Up @@ -586,9 +657,60 @@ protected function orderCreate($order, $options)

$this->retailcrm->ordersFixExternalIds($ids);

$this->editOrder($this->retailcrm_settings, $wc_order, $order);

return $wc_order->get_id();
}

/**
* @param $settings
* @param $wc_order
* @param $order
* @param string $event
*/
protected function editOrder($settings, $wc_order, $order, $event = 'create')
{
$order_items = [];
if ($event == 'update') {
$result = $this->retailcrm->ordersGet($order['externalId']);
if ($result->isSuccessful()) {
$orderCrm = $result['order'];
}
$data = $orderCrm;
}

if ($event == 'create') {
$data = $order;
}

foreach ($data['items'] as $id => $item) {
$order_items[$id]['id'] = $item['id'];
$order_items[$id]['offer'] = array('id' => $item['offer']['id']);
$externalIds = array(
array(
'code' => 'woocomerce',
'value' => $item['offer']['externalId'] . '_' . $order['items'][$item['id']]['woocomerceId'],
)
);

if ($item['externalIds']) {
$order_items[$id]['externalIds'] = $item['externalIds'];
$order_items[$id]['externalIds'][] = $externalIds;
} else {
$order_items[$id]['externalIds'] = $externalIds;
}
}

if (!empty($order_items)) {
$orderEdit = [
'id' => $order['id'],
'items' => $order_items,
];

$this->retailcrm->ordersEdit($orderEdit, 'id');
}
}

/**
* @param array $orderHistory
*
Expand Down
8 changes: 8 additions & 0 deletions src/include/order/class-wc-retailcrm-order-item.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,14 @@ public function build($item)
$data['initialPrice'] = (float)$price;
$data['quantity'] = (double)$item['qty'];

$itemId = ($item['variation_id'] > 0) ? $item['variation_id'] : $item['product_id'];
$data['externalIds'] = array(
array(
'code' =>'woocomerce',
'value' => $itemId . '_' . $item->get_id(),
)
);

$this->set_data_fields($data);
$this->set_offer($item);

Expand Down
2 changes: 1 addition & 1 deletion src/retailcrm.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php
/**
* Version: 3.5.2
* Version: 3.5.4
* WC requires at least: 3.0
* WC tested up to: 3.5.5
* Plugin Name: WooCommerce RetailCRM
Expand Down
2 changes: 1 addition & 1 deletion src/uninstall.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
*
*
* @link https://wordpress.org/plugins/woo-retailcrm/
* @version 3.5.2
* @version 3.5.4
*
* @package RetailCRM
*/
Expand Down
36 changes: 30 additions & 6 deletions tests/phpunit/test-wc-retailcrm-history.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,26 +72,38 @@ public function test_history_order_create($api_version)
$order_added_item = reset($order_added_items);
$shipping_address = $order_added->get_address('shipping');
$billing_address = $order_added->get_address('billing');

$options = get_option(\WC_Retailcrm_Base::$option_key);

$this->assertEquals(self::STATUS_1, $options[$order_added->get_status()]);
$this->assertEquals($product->get_id(), $order_added_item->get_product()->get_id());

if (is_object($order_added_item)) {
$this->assertEquals($product->get_id(), $order_added_item->get_product()->get_id());
}

$this->assertNotEmpty($shipping_address['first_name']);
$this->assertNotEmpty($shipping_address['last_name']);
$this->assertNotEmpty($shipping_address['postcode']);
$this->assertNotEmpty($shipping_address['city']);
$this->assertNotEmpty($shipping_address['country']);
$this->assertNotEmpty($shipping_address['state']);
$this->assertNotEmpty($billing_address['phone']);
$this->assertNotEmpty($billing_address['email']);

if (isset($billing_address['phone'])) {
$this->assertNotEmpty($billing_address['phone']);
}

if (isset($billing_address['email'])) {
$this->assertNotEmpty($billing_address['email']);
}

$this->assertNotEmpty($billing_address['first_name']);
$this->assertNotEmpty($billing_address['last_name']);
$this->assertNotEmpty($billing_address['postcode']);
$this->assertNotEmpty($billing_address['city']);
$this->assertNotEmpty($billing_address['country']);
$this->assertNotEmpty($billing_address['state']);
$this->assertEquals('payment4', $options[$order_added->get_payment_method()]);

if ($order_added->get_payment_method()) {
$this->assertEquals('payment4', $options[$order_added->get_payment_method()]);
}
}

/**
Expand Down Expand Up @@ -281,6 +293,12 @@ private function get_history_data_new_order($product_create_id)
'createdAt' => '2018-01-01 00:00:00',
'quantity' => 1,
'status' => 'new',
'externalIds' =>array(
array(
'code' =>'woocomerce',
'value' =>"160_".$product_create_id
)
),
'offer' => array(
'id' => 1,
'externalId' => $product_create_id,
Expand Down Expand Up @@ -348,6 +366,12 @@ private function get_history_data_product_add($product_add_id, $order_id)
'createdAt' => '2018-01-01 00:02:00',
'quantity' => 2,
'status' => self::STATUS_1,
'externalIds' =>array(
array(
'code' =>'woocomerce',
'value' =>"160_".$product_add_id
)
),
'offer' => array(
'id' => 2,
'externalId' => $product_add_id,
Expand Down

0 comments on commit 6fa5e3d

Please sign in to comment.