parent
2765e7ecb4
commit
80f266dcd0
2 changed files with 63 additions and 8 deletions
|
@ -384,7 +384,7 @@ if (!class_exists('WC_Retailcrm_Orders')) :
|
|||
* @return void
|
||||
* @throws \Exception
|
||||
*/
|
||||
protected function processOrder($order, $update = false)
|
||||
protected function processOrder($order, $update = false)//TODO Возможно ли по хуку передать доп данные? Например что это приминеение бонусов к новому заказу
|
||||
{
|
||||
if (!$order instanceof WC_Order) {
|
||||
return;
|
||||
|
@ -448,14 +448,50 @@ if (!class_exists('WC_Retailcrm_Orders')) :
|
|||
|
||||
$orderData['delivery']['address'] = $this->order_address->build($order)->getData();
|
||||
$orderItems = [];
|
||||
$crmItems = []; // необходимо для обновления торговой позиции (определения кол-ва списываемых бонусов)
|
||||
$loyaltyDiscountType = null; // вид скидки
|
||||
|
||||
if ($this->loyalty && $update) {
|
||||
$response = $this->retailcrm->ordersGet($order->get_id());
|
||||
|
||||
if (!$response instanceof WC_Retailcrm_Response || !$response->isSuccessful()) {
|
||||
writeBaseLogs('Process order: Error when receiving an order from the crm. Order Id: ' . $order->get_id());
|
||||
|
||||
$crmOrder = null;
|
||||
} else {
|
||||
$crmOrder = $response['order'] ?? null;
|
||||
}
|
||||
}
|
||||
|
||||
if ($crmOrder) {
|
||||
foreach ($crmOrder['items'] as $item) {
|
||||
$externalId = $item['externalids'][0]['value'];
|
||||
$externalId = preg_replace('/^\d+\_/m', '', $externalId);
|
||||
$crmItems[$externalId] = $item;
|
||||
|
||||
if (!$loyaltyDiscountType) {
|
||||
foreach ($item['discounts'] as $discount) {
|
||||
if (in_array($discount['type'], ['bonus_charge', 'loyalty_level'])) {
|
||||
$loyaltyDiscountType = $discount['type'];
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
unset($crmOrder);
|
||||
}
|
||||
|
||||
/** @var WC_Order_Item_Product $item */
|
||||
foreach ($order->get_items() as $item) {
|
||||
$orderItems[] = $this->order_item->build($item)->getData();
|
||||
foreach ($order->get_items() as $id => $item) {
|
||||
$crmItem = $crmItems[$id] ?? null;
|
||||
$orderItems[] = $this->order_item->build($item, $crmItem)->getData();
|
||||
|
||||
$this->order_item->resetData();
|
||||
}
|
||||
|
||||
unset($crmItems, $crmItem);
|
||||
|
||||
$orderData['items'] = $orderItems;
|
||||
$orderData['discountManualAmount'] = 0;
|
||||
$orderData['discountManualPercent'] = 0;
|
||||
|
|
|
@ -43,13 +43,13 @@ class WC_Retailcrm_Order_Item extends WC_Retailcrm_Abstracts_Data
|
|||
*
|
||||
* @return self
|
||||
*/
|
||||
public function build($item)
|
||||
public function build($item, $crmItem = null)
|
||||
{
|
||||
$decimalPlaces = wc_get_price_decimals();
|
||||
|
||||
// Calculate price and discount
|
||||
$price = $this->calculatePrice($item, $decimalPlaces);
|
||||
$discountPrice = $this->calculateDiscount($item, $price, $decimalPlaces);
|
||||
$discountPrice = $this->calculateDiscount($item, $price, $decimalPlaces, $crmItem);
|
||||
|
||||
$data['productName'] = $item['name'];
|
||||
$data['initialPrice'] = $price;
|
||||
|
@ -115,9 +115,28 @@ class WC_Retailcrm_Order_Item extends WC_Retailcrm_Abstracts_Data
|
|||
*
|
||||
* @return float|int
|
||||
*/
|
||||
private function calculateDiscount(WC_Order_Item_Product $item, $price, int $decimalPlaces)
|
||||
{
|
||||
$productPrice = $item->get_total() ? $item->get_total() / $item->get_quantity() : 0;
|
||||
private function calculateDiscount(
|
||||
WC_Order_Item_Product $item,
|
||||
$price,
|
||||
int $decimalPlaces,
|
||||
$crmItem = null,
|
||||
$loyaltyDiscountType = null // по идее не нужно, т.к. если новая позиция, автоматом придет ответ по истории со скидкой
|
||||
) {
|
||||
if ($crmItem) {
|
||||
$loyaltyDiscount = 0;
|
||||
|
||||
foreach ($item['discounts'] as $discount) {
|
||||
if (in_array($discount['type'], ['bonus_charge', 'loyalty_level'])) {
|
||||
$loyaltyDiscount += $discount['amount'];
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
$productPrice = $item->get_total() ? $item->get_total() + $loyaltyDiscount / $item->get_quantity() : 0;
|
||||
} else {
|
||||
$productPrice = $item->get_total() ? $item->get_total() / $item->get_quantity() : 0;
|
||||
}
|
||||
|
||||
$productTax = $item->get_total_tax() ? $item->get_total_tax() / $item->get_quantity() : 0;
|
||||
$itemPrice = $productPrice + $productTax;
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue