Commit b2ab0589 authored by Daniel Pepin's avatar Daniel Pepin
Browse files

Issue #2536510: Setup the shopify cart block.

parent b2dfc9b7
Loading
Loading
Loading
Loading
+25 −0
Changes for shopify.js: 25 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -25,6 +25,30 @@
    });
  };

  /**
   * Displays the Shopify cart total if available.
   */
  Drupal.shopify.displayCartTotal = function ($ctx) {
    var $cart_blocks = $ctx.find('.block-shopify-cart');
    if (!$cart_blocks.length) {
      // No carts on the page.
      return;
    }
    $cart_blocks.each(function (i, el) {
      $.ajax({
        type: 'GET',
        url: '//' + drupalSettings.shopify.shop.domain + '/cart.json',
        dataType: 'jsonp',
        success: function (data) {
          var total = 0;
          for (var i = 0; i < data.items.length; i++) {
            total += data.items[i].quantity;
          }
          $(el).find('.shopify-cart-total').text(total);
        }
      });
    });
  };

  /**
   * Behaviors for Shopify.
@@ -38,6 +62,7 @@
    attach: function (context) {
      var $context = $(context);
      Drupal.shopify.attachAddToCartMessage($context);
      Drupal.shopify.displayCartTotal($context);
    }
  };

+2 −0
Changes for shopify.libraries.yml: 2 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -8,3 +8,5 @@ shopify.js:
  version: 1.x
  js:
    shopify.js: {}
  dependencies:
    - core/jquery
 No newline at end of file
+24 −0
Changes for shopify.module: 24 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -9,6 +9,30 @@ use Drupal\shopify\Entity\ShopifyProductVariant;
use Shopify\Client;
use  Drupal\taxonomy\Entity\Term;

/**
 * Implements hook_theme().
 */
function shopify_theme($existing, $type, $theme, $path) {
  return [
    'shopify_cart' => [
      'template' => 'shopify_cart',
      'variables' => ['domain' => ''],
    ],
  ];
}

/**
 * Data that should be passed into drupalSettings.
 */
function shopify_drupal_js_data() {
  $info = shopify_shop_info();
  return [
    'shop' => [
      'domain' => $info->domain,
    ],
  ];
}

/**
 * Implements hook_entity_extra_field_info().
 */
+50 −0
Changes for src/Plugin/Block/ShopifyCartBlock.php: 50 added lines, 0 removed lines.
Original line number Diff line number Diff line
<?php

/**
 * @file
 * Contains \Drupal\shopify\Plugin\Block\ShopifyCartBlock.
 */

namespace Drupal\shopify\Plugin\Block;

use Drupal\Core\Block\BlockBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Render\Element;
use Drupal\Core\Url;

/**
 * Provides the shopping cart block.
 *
 * @Block(
 *  id = "shopify_cart",
 *  admin_label = @Translation("Cart")
 * )
 */
class ShopifyCartBlock extends BlockBase {

  /**
   * {@inheritdoc}
   */
  public function getCacheMaxAge() {
    // Contents of cart don't depend on the page or user or any other
    // cache context we have available.
    return 0;
  }

  /**
   * {@inheritdoc}
   */
  public function build() {
    $build[] = [
      '#theme' => 'shopify_cart',
      '#domain' => shopify_shop_info('domain'),
      '#url' => Url::fromUri('https://' . shopify_shop_info('domain') . '/cart'),
      '#attached' => [
        'library' => ['shopify/shopify.js'],
        'drupalSettings' => ['shopify' => shopify_drupal_js_data()],
      ],
    ];
    return $build;
  }

}
+5 −0
Changes for templates/shopify_cart.html.twig: 5 added lines, 0 removed lines.
Original line number Diff line number Diff line
<a href="https://{{ domain }}/cart" target="_blank">
    <i class="fa fa-shopping-cart"></i>
    <span class="shopify-cart-text">{{ 'Cart'|t }}</span>
    (<span class="shopify-cart-total">0</span>)
</a>
 No newline at end of file