1
0
Fork 0
mirror of synced 2025-04-06 07:13:33 +03:00
Loyalty program validator edit
Loyalty program request api edit
Creating a method to get the maximum discount on loyalty program
Created event for coupon creation
Created events for changing the cart
This commit is contained in:
Ivan Chaplygin 2024-05-02 18:02:39 +03:00
parent a5462e0b2a
commit 806e7c583c
4 changed files with 108 additions and 3 deletions

View file

@ -2992,12 +2992,12 @@ class WC_Retailcrm_Client_V5
}
/** Maximum discount calculation */
public function calculateDiscountLoyalty(string $site, array $order, float $bonuses)
public function calculateDiscountLoyalty(string $site, array $order)
{
return $this->client->makeRequest(
"/loyalty/calculate",
WC_Retailcrm_Request::METHOD_POST,
['site' => $site, 'order' => json_encode($order), 'bonuses' => $bonuses]
['site' => $site, 'order' => json_encode($order)]
);
}

View file

@ -110,6 +110,13 @@ if (!class_exists('WC_Retailcrm_Base')) {
add_action('init', [$this, 'add_loyalty_endpoint'], 11, 1);
add_action('woocommerce_account_menu_items', [$this, 'add_loyalty_item'], 11, 1);
add_action('woocommerce_account_loyalty_endpoint', [$this, 'show_loyalty'], 11, 1);
// Add coupon hooks for loyalty program
add_action('woocommerce_cart_coupon', [$this, 'coupon_info'], 11, 1);
add_action('woocommerce_add_to_cart', [$this, 'set_cart_loyalty'], 11, 1);
add_action('woocommerce_after_cart_item_quantity_update', [$this, 'set_cart_loyalty'], 11, 1);
add_action('woocommerce_cart_item_removed', [$this, 'set_cart_loyalty'], 11, 1);
add_action('woocommerce_cart_emptied', [$this, 'clear_cart_loyalty'], 11, 1);
}
// Subscribed hooks
@ -680,6 +687,65 @@ if (!class_exists('WC_Retailcrm_Base')) {
wp_die();
}
public function coupon_info()
{
global $woocommerce;
try {
$site = $this->apiClient->getSingleSiteForKey();
$cartItems = $woocommerce->cart->get_cart();
$customerId = $woocommerce->customer->get_id();
if (!$customerId || !$cartItems) {
return;
}
$validator = new WC_Retailcrm_Loyalty_Validator($this->apiClient, $this->settings['corporate_enabled'] ?? static::NO);
if (!$validator->checkAccount($customerId)) {
return;
}
$lpDiscountSum = $this->loyalty->getDiscountLp($cartItems, $site, $customerId);
if ($lpDiscountSum === 0) {
return;
}
$coupon = new WC_Coupon();
//$coupon->set_individual_use(true); // запрещает использование других купонов одноврменно с этим
$coupon->set_usage_limit(0);
$coupon->set_amount($lpDiscountSum);
$coupon->set_email_restrictions($woocommerce->customer->get_email());
$coupon->set_code('pl' . mt_rand());
$coupon->save();
echo '<div style="background: #05ff13;">' . 'Your coupon: ' . $coupon->get_code() . '</div>';
} catch (Throwable $exception) {
writeBaseLogs($exception->getMessage());
}
}
public function set_cart_loyalty()
{
global $woocommerce;
try {
//$woocommerce->cart;
foreach ($woocommerce->cart->get_coupons() as $code => $coupon) {
if (strpos($code, 'pl') !== false) { //заменить на регулярное выражение ^lp\d+$
$woocommerce->cart->remove_coupon($code);
}
}
return;
} catch (Throwable $exception) {
writeBaseLogs($exception->getMessage());
}
}
/**
* In this method we include CSS file
*

View file

@ -116,6 +116,45 @@ if (!class_exists('WC_Retailcrm_Loyalty')) :
return false;
}
}
public function getDiscountLp($cartItems, $site, $customerId)
{
$order = [
'site' => $site,
'customer' => ['externalId' => $customerId],
'privilegeType' => 'loyalty_level'
];
$useXmlId = isset($this->settings['bind_by_sku']) && $this->settings['bind_by_sku'] === WC_Retailcrm_Base::YES;
foreach ($cartItems as $item) {
$product = $item['data'];
$order['items'][] = [
'offer' => $useXmlId ? ['xmlId' => $product->get_sku()] : ['externalId' => $product->get_id()],
'quantity' => $item['quantity'],
'initialPrice' => wc_get_price_including_tax($product)
];
}
$response = $this->apiClient->calculateDiscountLoyalty($site, $order);
if (!$response->isSuccessful() || !isset($response['calculations'])) {
return 0;
}
$discount = 0;
foreach ($response['calculations'] as $calculate) {
if ($calculate['privilegeType'] !== 'loyalty_level') {
continue;
}
$discount = $calculate['discount'] !== 0 ? $calculate['discount'] : $calculate['maxChargeBonuses'];
}
return $discount;
}
}
endif;

View file

@ -56,7 +56,7 @@ if (!class_exists('WC_Retailcrm_Loyalty_Validator')) :
$customer = new WC_Customer($userId);
if ($this->isActiveCorp && !empty($customer->get_shipping_company())) {
if ($this->isActiveCorp === 'yes' && !empty($customer->get_shipping_company())) {
throw new ValidatorException($this->isCorporateUser, 400);
}