1
0
mirror of synced 2025-01-30 23:01:43 +03:00

небольшие правки

This commit is contained in:
gorokh 2019-10-15 16:14:42 +03:00
parent 4eecba2d86
commit 8518b43d03

View File

@ -331,7 +331,7 @@ if ( ! class_exists( 'WC_Retailcrm_History' ) ) :
}
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;
@ -343,9 +343,23 @@ if ( ! class_exists( 'WC_Retailcrm_History' ) ) :
$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 {
@ -451,7 +465,7 @@ if ( ! class_exists( 'WC_Retailcrm_History' ) ) :
}
if ($checkNewItem == true) {
$this->editOrder($this->retailcrm_settings, $wc_order, $order);
$this->editOrder($this->retailcrm_settings, $wc_order, $order,'update');
}
return $wc_order->get_id();
@ -571,16 +585,33 @@ if ( ! class_exists( 'WC_Retailcrm_History' ) ) :
$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(
$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;
}
}
@ -624,10 +655,10 @@ if ( ! class_exists( 'WC_Retailcrm_History' ) ) :
$wc_order->save();
$this->editOrder($this->retailcrm_settings, $wc_order, $order, 'update');
$this->retailcrm->ordersFixExternalIds($ids);
$this->editOrder($this->retailcrm_settings, $wc_order, $order);
return $wc_order->get_id();
}
@ -640,26 +671,36 @@ if ( ! class_exists( 'WC_Retailcrm_History' ) ) :
protected function editOrder($settings, $wc_order, $order, $event = 'create')
{
$order_items = [];
$retailcrmOrderItem = new WC_Retailcrm_Order_Item($settings);
foreach ($wc_order->get_items() as $key => $item) {
$order_items[$key] = $retailcrmOrderItem->build($item)->get_data();
$retailcrmOrderItem->reset_data();
if ($event == 'update') {
foreach ($order['items'] as $itemCrm) {
if (isset($itemCrm['externalIds']) && !empty($itemCrm['externalIds'])) {
foreach ($itemCrm['externalIds'] as $externalId) {
if ($externalId['code'] == 'woocomerce') {
$test = explode('_', $externalId['value']);
$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 ($order_items[$test[1]]) {
unset($order_items[$test[1]]);
}
}
}
}
}
if (!empty($order_items)) {
$orderEdit = [
'id' => $order['id'],
@ -667,7 +708,6 @@ if ( ! class_exists( 'WC_Retailcrm_History' ) ) :
];
$this->retailcrm->ordersEdit($orderEdit, 'id');
}
}