1
0
mirror of synced 2025-03-27 10:33:53 +03:00
woocommerce-module/src/include/class-wc-retailcrm-cart.php

106 lines
3.8 KiB
PHP

<?php
if (!class_exists('WC_Retailcrm_Carts')) :
/**
* PHP version 7.0
*
* Class WC_Retailcrm_Cart - Allows transfer data carts with CMS.
*
* @category Integration
* @author RetailCRM <integration@retailcrm.ru>
* @license http://retailcrm.ru Proprietary
* @link http://retailcrm.ru
* @see http://help.retailcrm.ru
*/
class WC_Retailcrm_Cart
{
protected $apiClient;
protected $dateFormat;
protected $settings;
public function __construct($apiClient, $settings)
{
$this->apiClient = $apiClient;
$this->settings = $settings;
$this->dateFormat = 'Y-m-d H:i:sP';
}
public function isCartExist($customerId, $site): bool
{
$getCart = $this->apiClient->cartGet($customerId, $site);
return !empty($getCart['cart']['externalId']);
}
public function processCart($customerId, $cartItems, $site, $isCartExist): bool
{
$isSuccessful = false;
try {
$crmCart = [
'customer' => ['externalId' => $customerId],
'clearAt' => null,
'updatedAt' => date($this->dateFormat),
'droppedAt' => date($this->dateFormat)
];
// If new cart, need set createdAt and externalId
if (!$isCartExist) {
$crmCart['createdAt'] = date($this->dateFormat);
$crmCart['externalId'] = $customerId . uniqid('_', true);
}
// If you delete one by one
if (empty($cartItems)) {
return $this->clearCart($customerId, $site, $isCartExist);
}
$useXmlId = isset($this->settings['bind_by_sku']) && $this->settings['bind_by_sku'] === WC_Retailcrm_Base::YES;
foreach ($cartItems as $item) {
$product = $item['data'];
$crmCart['items'][] = [
'offer' => $useXmlId ? ['xmlId' => $product->get_sku()] : ['externalId' => $product->get_id()],
'quantity' => $item['quantity'],
'createdAt' => $product->get_date_created()->date($this->dateFormat) ?? date($this->dateFormat),
'updatedAt' => $product->get_date_modified()->date($this->dateFormat) ?? date($this->dateFormat),
'price' => wc_get_price_including_tax($product),
];
}
$crmCart = apply_filters(
'retailcrm_process_cart',
WC_Retailcrm_Plugin::clearArray($crmCart),
$cartItems
);
$setResponse = $this->apiClient->cartSet($crmCart, $site);
$isSuccessful = $setResponse->isSuccessful() && !empty($setResponse['success']);
} catch (Throwable $exception) {
writeBaseLogs('Error process cart: ' . $exception->getMessage());
}
return $isSuccessful;
}
public function clearCart($customerId, $site, $isCartExist): bool
{
$isSuccessful = false;
try {
if ($isCartExist) {
$crmCart = ['customer' => ['externalId' => $customerId], 'clearedAt' => date($this->dateFormat)];
$clearResponse = $this->apiClient->cartClear($crmCart, $site);
$isSuccessful = $clearResponse->isSuccessful() && !empty($clearResponse['success']);
}
} catch (Throwable $exception) {
writeBaseLogs('Error clear cart: ' . $exception->getMessage());
}
return $isSuccessful;
}
}
endif;