You are here

class CartHandler in Mailchimp E-Commerce 8

Cart handler.

Hierarchy

Expanded class hierarchy of CartHandler

4 files declare their use of CartHandler
CartEventSubscriber.php in modules/mailchimp_ecommerce_commerce/src/EventSubscriber/CartEventSubscriber.php
CartHandlerTest.php in tests/src/Unit/CartHandlerTest.php
mailchimp_ecommerce_ubercart.module in modules/mailchimp_ecommerce_ubercart/mailchimp_ecommerce_ubercart.module
Integrates Ubercart with Mailchimp eCommerce.
OrderEventSubscriber.php in modules/mailchimp_ecommerce_commerce/src/EventSubscriber/OrderEventSubscriber.php
1 string reference to 'CartHandler'
mailchimp_ecommerce.services.yml in ./mailchimp_ecommerce.services.yml
mailchimp_ecommerce.services.yml
1 service uses CartHandler
mailchimp_ecommerce.cart_handler in ./mailchimp_ecommerce.services.yml
\Drupal\mailchimp_ecommerce\CartHandler

File

src/CartHandler.php, line 13

Namespace

Drupal\mailchimp_ecommerce
View source
class CartHandler implements CartHandlerInterface, ContainerInjectionInterface {

  /**
   * The ecommerce service.
   *
   * @var \Mailchimp\MailchimpEcommerce
   */
  protected $mcEcommerce;

  /**
   * The ecommerce helper.
   *
   * @var \Drupal\mailchimp_ecommerce\MailchimpEcommerceHelper
   */
  protected $helper;

  /**
   * The messenger service.
   *
   * @var \Drupal\Core\Messenger\MessengerInterface
   */
  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'));
  }

  /**
   * @inheritdoc
   */
  public function cartExists($cart_id) {
    return !empty($this
      ->getCart($cart_id));
  }

  /**
   * @inheritdoc
   */
  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) {

          // Cart doesn't exist.
        }
        else {

          // An actual error occurred; pass on the exception.
          throw new \Exception($e
            ->getMessage(), $e
            ->getCode(), $e);
        }
      }
    } catch (\Exception $e) {

      //mailchimp_ecommerce_log_error_message('Unable to get the requested cart: ' . $e->getMessage());
      $this->messenger
        ->addError($e
        ->getMessage());
    }
    return $cart;
  }

  /**
   * @inheritdoc
   */
  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)) {

        // A user not existing in the store's Mailchimp list/audience is not an error, so
        // don't throw an exception.
        return;
      }

      // Get the Mailchimp campaign ID, if available.
      $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) {

          // Cart doesn't exist; add a new cart.
          $this->mcEcommerce
            ->addCart($store_id, $cart_id, $customer, $cart);
        }
        else {

          // An actual error occurred; pass on the exception.
          throw new \Exception($e
            ->getMessage(), $e
            ->getCode(), $e);
        }
      }
    } catch (\Exception $e) {

      //mailchimp_ecommerce_log_error_message('Unable to add a cart: ' . $e->getMessage());
      $this->messenger
        ->addError($e
        ->getMessage());
    }
  }

  /**
   * @inheritdoc
   */
  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) {

        // Cart doesn't exist; no need to log an error.
      }
      else {

        //mailchimp_ecommerce_log_error_message('Unable to delete a cart: ' . $e->getMessage());
        $this->messenger
          ->addError($e
          ->getMessage());
      }
    }
  }

  /**
   * @inheritdoc
   */
  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) {

      //mailchimp_ecommerce_log_error_message('Unable to add a cart line: ' . $e->getMessage());
      $this->messenger
        ->addError($e
        ->getMessage());
    }
  }

  /**
   * @inheritdoc
   */
  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 {

        //mailchimp_ecommerce_log_error_message('Unable to update a cart line: ' . $e->getMessage());
        $this->messenger
          ->addError($e
          ->getMessage());
      }
    }
  }

  /**
   * @inheritdoc
   */
  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) {

      //mailchimp_ecommerce_log_error_message('Unable to delete a cart line: ' . $e->getMessage());
      $this->messenger
        ->addError($e
        ->getMessage());
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
CartHandler::$helper protected property The ecommerce helper.
CartHandler::$mcEcommerce protected property The ecommerce service.
CartHandler::$messenger protected property The messenger service.
CartHandler::addCartLine public function @inheritdoc Overrides CartHandlerInterface::addCartLine
CartHandler::addOrUpdateCart public function @inheritdoc Overrides CartHandlerInterface::addOrUpdateCart
CartHandler::cartExists public function @inheritdoc Overrides CartHandlerInterface::cartExists
CartHandler::create public static function Instantiates a new instance of this class. Overrides ContainerInjectionInterface::create
CartHandler::deleteCart public function @inheritdoc Overrides CartHandlerInterface::deleteCart
CartHandler::deleteCartLine public function @inheritdoc Overrides CartHandlerInterface::deleteCartLine
CartHandler::getCart public function @inheritdoc Overrides CartHandlerInterface::getCart
CartHandler::updateCartLine public function @inheritdoc Overrides CartHandlerInterface::updateCartLine
CartHandler::__construct public function