Commit 62dcd169 authored by Daniel Pepin's avatar Daniel Pepin
Browse files

Issue #2536510: See details.

- Created extra fields for the add to cart form in the Shopify product display.
- Created view builder for products to handle displaying of forms.
- Created two forms... One for handling variant options, the other for POSTing to the Shopify cart.
- Setup a controller to display 'added to your cart' messaging.
- Setup shopify.js to display message and POST the form.
- Created additional methods within the Entity classes for loading by product/variant IDs.
parent f105a116
Loading
Loading
Loading
Loading
+22 −16
Changes for shopify.js: 22 added lines, 16 removed lines.
Original line number Diff line number Diff line
/**
 * @file
 * Contains javascript specific to Shopify module functionality.
 * Defines Javascript behaviors for the Shopify module.
 */
(function ($) {
(function ($, Drupal, drupalSettings) {

  Drupal.shopify = {
    ctx: {},
    settings: {}
  };
  'use strict';

  Drupal.shopify = {};

  /**
   * Display an "Added to cart" message by sending a POST request to the backend.
   */
  Drupal.shopify.display_add_to_cart_message = function () {
    var $forms = $('form.shopify-add-to-cart-form');
  Drupal.shopify.attachAddToCartMessage = function ($ctx) {
    var $forms = $ctx.find('form.shopify-add-to-cart-form');
    $forms.unbind('submit').submit(function (e) {
      var $form = $(this);
      e.preventDefault();
      $.post(Drupal.settings.basePath + '?q=shopify/added-to-cart', {
        product_id: $form.data('product-id'),
      $.post(drupalSettings.path.baseUrl + 'shopify/added-to-cart', {
        variant_id: $form.data('variant-id'),
        quantity: $form.find('input[name="quantity"]').val()
      }, function (data) {
@@ -27,12 +25,20 @@
    });
  };


  /**
   * Behaviors for Shopify.
   *
   * @type {Drupal~behavior}
   *
   * @prop {Drupal~behaviorAttach} attach
   *   Attaches Shopify events.
   */
  Drupal.behaviors.shopify = {
    attach: function (context, settings) {
      Drupal.shopify.ctx = context;
      Drupal.shopify.settings = settings;
      Drupal.shopify.display_add_to_cart_message();
    }
    attach: function (context) {
      var $context = $(context);
      Drupal.shopify.attachAddToCartMessage($context);
    }
  };

}(jQuery));
 No newline at end of file
})(jQuery, Drupal, drupalSettings);
+5 −0
Changes for shopify.libraries.yml: 5 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -3,3 +3,8 @@ shopify.toolbar.css:
  css:
    theme:
      css/shopify.toolbar.css: {}

shopify.js:
  version: 1.x
  js:
    shopify.js: {}
+17 −40
Changes for shopify.module: 17 added lines, 40 removed lines.
Original line number Diff line number Diff line
@@ -9,6 +9,21 @@ use Drupal\shopify\Entity\ShopifyProductVariant;
use Shopify\Client;
use  Drupal\taxonomy\Entity\Term;

/**
 * Implements hook_entity_extra_field_info().
 */
function shopify_entity_extra_field_info() {
  $extra = [];
  $extra['shopify_product']['shopify_product']['display']['add_to_cart_form'] = [
    'label' => t('Add to cart form'),
    'description' => t('Display variant options, quantity field, and "Add to Cart" button.'),
    'visible' => TRUE,
    'weight' => 100,
    'edit' => '/test',
  ];
  return $extra;
}

/**
 * Implements hook_cron().
 */
@@ -202,7 +217,7 @@ function shopify_sync_products(array $opts = []) {
  $result = $client->get('products', $opts);
  $synced_products = [];
  foreach ($result->products as $product) {
    $entity = shopify_product_load_by_product_id($product->id);
    $entity = ShopifyProduct::loadByProductId($product->id);
    if ($entity instanceof ShopifyProduct) {
      $entity->update((array) $product);
      $entity->save();
@@ -242,44 +257,6 @@ function shopify_sync_collections(array $opts = []) {
  return $synced_collections;
}

/**
 * Loads a Shopify product by it's product_id.
 *
 * @param int $product_id
 *   Shopify product ID.
 *
 * @return ShopifyProduct
 */
function shopify_product_load_by_product_id($product_id) {
  $query = \Drupal::entityQuery('shopify_product');
  $query->condition('product_id', $product_id);
  $ids = $query->execute();
  if ($ids) {
    $products = ShopifyProduct::loadMultiple($ids);
    return reset($products);
  }
  return FALSE;
}

/**
 * Loads a Shopify variant by it's variant_id.
 *
 * @param int $variant_id
 *   Shopify product variant ID.
 *
 * @return ShopifyProductVariant
 */
function shopify_variant_load_by_variant_id($variant_id) {
  $query = \Drupal::entityQuery('shopify_product_variant');
  $query->condition('variant_id', $variant_id);
  $ids = $query->execute();
  if ($ids) {
    $variants = ShopifyProductVariant::loadMultiple($ids);
    return reset($variants);
  }
  return FALSE;
}

/**
 * Deletes all Shopify products.
 */
@@ -409,7 +386,7 @@ function shopify_collection_sync_products(\stdClass $collection) {
  $collects = $client->getResources('collects', ['query' => ['collection_id' => $collection->id]]);
  foreach ($collects as $c) {
    // Update this product information.
    $product = shopify_product_load_by_product_id($c->product_id);
    $product = ShopifyProduct::loadByProductId($c->product_id);
    if (!$product) {
      continue;
    }
+7 −0
Changes for shopify.routing.yml: 7 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -6,6 +6,13 @@ shopify.webhook:
  requirements:
    _permission: 'access content'

shopify.added-to-cart:
  path: '/shopify/added-to-cart'
  defaults:
    _controller: '\Drupal\shopify\Controller\ShopifyAddedToCart::displayMessage'
  requirements:
    _permission: 'access content'

shopify.admin:
  path: '/admin/config/system/shopify'
  defaults:
+1 −1
Changes for src/Batch/ShopifyProductBatch.php: 1 added line, 1 removed line.
Original line number Diff line number Diff line
@@ -101,7 +101,7 @@ class ShopifyProductBatch {
    $result = $client->get('products', ['query' => $settings]);
    if (isset($result->products) && !empty($result->products)) {
      foreach ($result->products as $product) {
        $entity = shopify_product_load_by_product_id($product->id);
        $entity = ShopifyProduct::loadByProductId($product->id);
        if (!$entity) {
          // Need to create this product.
          $entity = ShopifyProduct::create((array) $product);
Loading