View source
<?php
namespace Drupal\mailchimp_ecommerce;
use Drupal\Core\DependencyInjection\ContainerInjectionInterface;
use Drupal\Core\Messenger\MessengerInterface;
use Mailchimp\MailchimpEcommerce;
use Symfony\Component\DependencyInjection\ContainerInterface;
class CartHandler implements CartHandlerInterface, ContainerInjectionInterface {
protected $mcEcommerce;
protected $helper;
protected $messenger;
public function __construct(MailchimpEcommerce $mc_ecommerce, MailchimpEcommerceHelper $helper, MessengerInterface $messenger) {
$this->mcEcommerce = $mc_ecommerce;
$this->helper = $helper;
$this->messenger = $messenger;
}
public static function create(ContainerInterface $container) {
return new static(\mailchimp_get_api_object('MailchimpEcommerce'), $container
->get('mailchimp_ecommerce.helper'), $container
->get('messenger'));
}
public function cartExists($cart_id) {
return !empty($this
->getCart($cart_id));
}
public function getCart($cart_id) {
$cart = NULL;
try {
$store_id = $this->helper
->getStoreId();
if (empty($store_id)) {
throw new \Exception('Cannot get the requested cart without a store ID.');
}
try {
$cart = $this->mcEcommerce
->getCart($store_id, $cart_id);
} catch (\Exception $e) {
if ($e
->getCode() == 404) {
}
else {
throw new \Exception($e
->getMessage(), $e
->getCode(), $e);
}
}
} catch (\Exception $e) {
$this->messenger
->addError($e
->getMessage());
}
return $cart;
}
public function addOrUpdateCart($cart_id, array $customer, array $cart) {
try {
$store_id = $this->helper
->getStoreId();
if (empty($store_id)) {
throw new \Exception('Cannot add a cart without a store ID.');
}
if (!$this->helper
->validateCustomer($customer)) {
return;
}
$campaign_id = $this->helper
->getCampaignId();
if (!empty($campaign_id)) {
$cart['campaign_id'] = $campaign_id;
$cart['landing_site'] = isset($_SESSION['mc_landing_site']) ? $_SESSION['mc_landing_site'] : '';
}
try {
if (!empty($this->mcEcommerce
->getCart($store_id, $cart_id))) {
$this->mcEcommerce
->updateCart($store_id, $cart_id, $customer, $cart);
}
} catch (\Exception $e) {
if ($e
->getCode() == 404) {
$this->mcEcommerce
->addCart($store_id, $cart_id, $customer, $cart);
}
else {
throw new \Exception($e
->getMessage(), $e
->getCode(), $e);
}
}
} catch (\Exception $e) {
$this->messenger
->addError($e
->getMessage());
}
}
public function deleteCart($cart_id) {
try {
$store_id = $this->helper
->getStoreId();
if (empty($store_id)) {
throw new \Exception('Cannot delete a cart without a store ID.');
}
$this->mcEcommerce
->deleteCart($store_id, $cart_id);
} catch (\Exception $e) {
if ($e
->getCode() == 404) {
}
else {
$this->messenger
->addError($e
->getMessage());
}
}
}
public function addCartLine($cart_id, $line_id, $product) {
try {
$store_id = $this->helper
->getStoreId();
if (empty($store_id)) {
throw new \Exception('Cannot add a cart line without a store ID.');
}
$this->mcEcommerce
->addCartLine($store_id, $cart_id, $line_id, $product);
} catch (\Exception $e) {
$this->messenger
->addError($e
->getMessage());
}
}
public function updateCartLine($cart_id, $line_id, $product) {
try {
$store_id = $this->helper
->getStoreId();
if (empty($store_id)) {
throw new \Exception('Cannot update a cart line without a store ID.');
}
$this->mcEcommerce
->updateCartLine($store_id, $cart_id, $line_id, $product);
} catch (\Exception $e) {
if ($e
->getCode() == 404) {
$this->mcEcommerce
->addCartLine($store_id, $cart_id, $line_id, $product);
}
else {
$this->messenger
->addError($e
->getMessage());
}
}
}
public function deleteCartLine($cart_id, $line_id) {
try {
$store_id = $this->helper
->getStoreId();
if (empty($store_id)) {
throw new \Exception('Cannot delete a cart line without a store ID.');
}
$this->mcEcommerce
->deleteCartLine($store_id, $cart_id, $line_id);
} catch (\Exception $e) {
$this->messenger
->addError($e
->getMessage());
}
}
}