You are here

mailchimp_ecommerce_ubercart.module in Mailchimp E-Commerce 8

Integrates Ubercart with Mailchimp eCommerce.

File

modules/mailchimp_ecommerce_ubercart/mailchimp_ecommerce_ubercart.module
View source
<?php

use Drupal\node\Entity\Node;
use Drupal\mailchimp_ecommerce\CartHandler;
use Drupal\mailchimp_ecommerce\CustomerHandler;
use Drupal\mailchimp_ecommerce\OrderHandler;
use Drupal\mailchimp_ecommerce\ProductHandler;

/**
 * @file
 * Integrates Ubercart with Mailchimp eCommerce.
 */

/**
 * Implements hook_node_insert().
 */
function mailchimp_ecommerce_ubercart_node_insert(Node $node) {
  if ($node
    ->getType() == 'product') {

    // Create a corresponding Mailchimp product.
    $product_handler = new ProductHandler();
    $product = $product_handler
      ->buildProductFromNode($node);
    $product_handler
      ->addProduct($product['id'], $product['title'], $product['url'], $product['title'], $product['description'], $product['type'], $product['variants']);
  }
}

/**
 * Implements hook_node_update().
 */
function mailchimp_ecommerce_ubercart_node_update(Node $node) {
  if ($node
    ->getType() == 'product') {
    $product_handler = new ProductHandler();
    $product = $product_handler
      ->buildProductFromNode($node);
    $product_handler
      ->updateProduct($product['id'], $product['title'], $product['url'], $product['title'], $product['description'], $product['type'], $product['variants']);
  }
}

/**
 * Implements hook_node_delete().
 */
function mailchimp_ecommerce_ubercart_node_delete(Node $node) {
  if ($node
    ->getType() == 'product') {
    $product_handler = new ProductHandler();
    $product = $product_handler
      ->buildProductFromNode($node);
    $product_handler
      ->deleteProductVariant($product['id'], $product['variant_id']);
  }
}

/**
 * Implements hook_mailchimp_ecommerce_add_store().
 */
function mailchimp_ecommerce_ubercart_mailchimp_ecommerce_add_store($store) {

  // Add existing Ubercart products to Mailchimp.
  $batch = [
    'title' => t('Adding products to Mailchimp'),
    'operations' => [],
  ];
  $products = \Drupal::entityManager()
    ->getStorage('node')
    ->loadByProperties([
    'type' => 'product',
  ]);
  if (!empty($products)) {
    $product_ids = array_keys($products);
    foreach ($product_ids as $product_id) {
      $batch['operations'][] = [
        'mailchimp_ecommerce_ubercart_batch_add_product',
        [
          $product_id,
        ],
      ];
    }
  }
  batch_set($batch);
}

/**
 * Batch callback used to add a product to Mailchimp.
 */
function mailchimp_ecommerce_ubercart_batch_add_product($product_id, &$context) {
  $node = Node::load($product_id);
  $product_handler = new ProductHandler();
  $product = $product_handler
    ->buildProductFromNode($node);
  $product_handler
    ->addProduct($product['id'], $product['title'], $product['description'], $product['type'], $product['variants']);
}

/**
 * Implements hook_uc_order_create().
 */
function mailchimp_ecommerce_ubercart_entity_insert(\Drupal\Core\Entity\EntityInterface $entity) {
}

/**
 * Implements hook_uc_order_create().
 */

/**
 * Implements hook_entity_update().
 */
function mailchimp_ecommerce_ubercart_entity_update(Drupal\Core\Entity\EntityInterface $entity) {
  $hi = 'huh?';
}

/**
 * Implements hook_uc_add_to_cart().
 */
function mailchimp_ecommerce_ubercart_uc_add_to_cart($nid, $qty, $data) {
  $hi = 'wat?';

  // this on cart add
}

/**
 * Implements hook_uc_add_to_cart_data().
 */
function mailchimp_ecommerce_ubercart_uc_add_to_cart_data($form_values) {
  $hi = 'wat?';

  // on cart add
}

/**
 * Implements hook_uc_cart_item_insert().
 */
function mailchimp_ecommerce_ubercart_uc_cart_item_insert($entity) {
  $hi = 'what';
}

/**
 * Implements hook_uc_cart_item_update().
 */
function mailchimp_ecommerce_ubercart_uc_cart_item_update($entity) {
  $qty = $entity->qty->value;
  $price = $entity->price->value;
  $data = $entity->data->value;

  // on cart update
}

/**
 * Implements hook_uc_cart_checkout_start().
 */
function mailchimp_ecommerce_ubercart_uc_cart_checkout_start($order) {

  // This on checkout start
  // This on set address
  // This on review order
  // Do nothing with no email.
  if (!$order
    ->getEmail()) {
    return;
  }
  $customer_handler = new customerHandler(\Drupal::database());
  $cart_handler = new cartHandler();
  $order_handler = new orderHandler();
  $mc_data = $order_handler
    ->buildUberOrder($order);
  $cart_handler
    ->addOrUpdateCart($order
    ->id(), $mc_data['customer'], $mc_data['order_data']);
}

/**
 * Implements hook_uc_checkout_complete().
 */
function mailchimp_ecommerce_ubercart_uc_checkout_complete($order, $account) {
  if (!$order
    ->getEmail()) {
    return;
  }
  $customer_handler = new customerHandler(\Drupal::database());
  $cart_handler = new cartHandler();
  $order_handler = new orderHandler();
  $cart_handler
    ->deleteCart($order
    ->id());
  $mc_data = $order_handler
    ->buildUberOrder($order);

  // Update the customer's total order count and total amount spent.
  $customer_handler
    ->incrementCustomerOrderTotal($mc_data['customer']['email_address'], $mc_data['order_data']['order_total']);
  $cart_handler
    ->addOrUpdateCart($order
    ->id(), $mc_data['customer'], $mc_data['order_data']);
  $order_handler
    ->addOrder($order
    ->id(), $mc_data['customer'], $mc_data['order_data']);
}