1
0
Fork 0
mirror of synced 2025-04-03 22:03:34 +03:00
Passing a discount when creating an order
This commit is contained in:
Ivan Chaplygin 2024-05-06 17:48:32 +03:00
parent 5bc1819aa5
commit f62deb87cb

View file

@ -87,8 +87,28 @@ if (!class_exists('WC_Retailcrm_Orders')) :
$this->order_payment->resetData();
$wcOrder = wc_get_order($orderId);
$coupons = $wcOrder->get_coupons();
$discountLp = 0;
foreach ($coupons as $coupon) {
$code = $coupon->get_code();
if (preg_match('/^pl\d+$/m', $code) !== 1) {
continue;
}
$discountLp = $coupon->get_discount();
$wcOrder->remove_coupon($code);
$objectCoupon = new WC_Coupon($code);
$objectCoupon->delete(true);
break;
}
$wcOrder->recalculate_coupons();
$this->processOrder($wcOrder);
$this->order['privilegeType'] = 'loyalty_level';
$response = $this->retailcrm->ordersCreate($this->order);
@ -98,6 +118,37 @@ if (!class_exists('WC_Retailcrm_Orders')) :
if (!$response instanceof WC_Retailcrm_Response || !$response->isSuccessful()) {
return $response->getErrorString();
}
$response = $this->retailcrm->applyBonusToOrder('woo', ['externalId' => $this->order['externalId']], (float) $discountLp);
if (!$response instanceof WC_Retailcrm_Response || !$response->isSuccessful()) {
return $response->getErrorString();
}
$wcItems = $wcOrder->get_items();
foreach ($response['order']['items'] as $item) {
$externalId = $item['externalIds'][0]['value'];
$externalId = preg_replace('/^\d+\_/m', '', $externalId);
if (isset($wcItems[(int) $externalId])) {
$discountLoyaltyTotal = 0;
foreach ($item['discounts'] as $discount) {
if ($discount['type'] === 'bonus_charge') {
$discountLoyaltyTotal += $discount['amount'];
}
}
$wcItem = $wcItems[(int) $externalId];
$wcItem->set_total($wcItem->get_total() - $discountLoyaltyTotal);
$wcItem->calculate_taxes();
$wcItem->save();
}
}
$wcOrder->calculate_totals();
} catch (Throwable $exception) {
writeBaseLogs(
sprintf(