Commit de85ba20 authored by tonytheferg's avatar tonytheferg
Browse files

Issue #3263409 by tonytheferg: Add support for Commerce Vado

parent b49922a6
Loading
Loading
Loading
Loading
+34 −3
Original line number Diff line number Diff line
@@ -55,7 +55,8 @@ function commerce_ajax_atc_alter_submit(&$form, $variation_id) {
function commerce_ajax_atc_field_formatter_settings_summary_alter(&$summary, $context) {
  // Append a message to the summary when commerce_add_to_cart has
  // enable_ajax set to TRUE for the current view mode.
  if ($context['formatter']->getPluginId() == 'commerce_add_to_cart') {
  $plugin_id = $context['formatter']->getPluginId();
  if ($plugin_id == 'commerce_add_to_cart' || $plugin_id == 'commerce_vado_group_add_to_cart') {
    if ($context['formatter']->getThirdPartySetting('commerce_ajax_atc', 'enable_ajax')) {
      $summary[] = t('Ajax is enabled for this add to cart form.');
    }
@@ -69,8 +70,8 @@ function commerce_ajax_atc_field_formatter_settings_summary_alter(&$summary, $co
function commerce_ajax_atc_field_formatter_third_party_settings_form($plugin, $field_definition, $view_mode, $form, $form_state) {
  $element = [];
  // Add a 'enable_ajax' checkbox to the'commerce_add_to_cart' field formatters.
  if ($plugin
    ->getPluginId() == 'commerce_add_to_cart') {
  $plugin_id = $plugin->getPluginId();
  if ($plugin_id == 'commerce_add_to_cart' || $plugin_id == 'commerce_vado_group_add_to_cart') {
    $element['enable_ajax'] = [
      '#type' => 'checkbox',
      '#title' => t('Enable Ajax'),
@@ -105,10 +106,40 @@ function commerce_ajax_atc_form_commerce_order_item_add_to_cart_form_alter(&$for

}

/**
 * Implements hook_form_FORM_ID_alter().
 */
function commerce_ajax_atc_form_commerce_order_item_vado_group_add_to_cart_form_alter(&$form, FormStateInterface $form_state, $form_id) {
  $module_exists = \Drupal::moduleHandler()->moduleExists('commerce_vado');
  if (!$module_exists) {
    return;
  }
  // Get the product entity in the form storage.
  $form_data = $form_state->getStorage();
  /** @var \Drupal\commerce_product\Entity\ProductInterface $product */
  $product = $form_data['product'];
  // Grab the view mode third party settings.
  $display = EntityViewDisplay::collectRenderDisplay($product, $form_data['view_mode']);
  $component = $display->getComponent('variations');
  if ($component && !empty($component['third_party_settings']['commerce_ajax_atc'])) {
    $settings = $component['third_party_settings']['commerce_ajax_atc'];
    // Add ajax to the add to cart form.
    if (!empty($settings['enable_ajax'])) {
      $variation_id = $form_state->getFormObject()->getEntity()->getPurchasedEntity()->id();
      commerce_ajax_atc_alter_submit($form, $variation_id);
    }
  }

}

/**
 * Implements hook_form_FORM_ID_alter().
 */
function commerce_ajax_atc_form_commerce_order_item_variation_cart_form_form_alter(&$form, FormStateInterface $form_state, $form_id) {
  $module_exists = \Drupal::moduleHandler()->moduleExists('commerce_variation_cart_form');
  if (!$module_exists) {
    return;
  }
  $config = \Drupal::config('commerce_ajax_atc.settings');
  if ($config->get('enable_variation_cart_form_ajax')) {
    $variation_id = $form_state->getFormObject()->getEntity()->getPurchasedEntity()->id();
+2 −1
Original line number Diff line number Diff line
@@ -9,7 +9,8 @@
    },
    "require-dev": {
        "drupal/colorbox_load": "^1.2",
        "drupal/commerce_variation_cart_form": "^1.2"
        "drupal/commerce_variation_cart_form": "^1.2",
        "drupal/commerce_vado": "^2.0"
    },
    "minimum-stability": "dev"
}
+150 −0
Original line number Diff line number Diff line
<?php

namespace Drupal\Tests\commerce_ajax_atc\FunctionalJavascript;

use Drupal\Core\Entity\Entity\EntityFormDisplay;
use Drupal\Tests\commerce_ajax_atc\Functional\AjaxAddToCartTestBase;
use Drupal\Core\Entity\Entity\EntityViewDisplay;

/**
 * Ajax add to cart tests for the vado add to cart form implementation.
 *
 * @ingroup commerce_ajax_atc
 *
 * @group commerce_ajax_atc
 */
class AjaxVadoAddToCartFormTest extends AjaxAddToCartTestBase {

  /**
   * Modules to enable.
   *
   * @var array
   */
  public static $modules = [
    'commerce_vado',
  ];

  /**
   * {@inheritdoc}
   */
  protected function getAdministratorPermissions() {
    return array_merge([
      'administer commerce_order',
      'administer commerce_order_type',
      'access commerce_order overview',
      'access vado administration pages',
      'administer commerce_vado_group',
    ], parent::getAdministratorPermissions());
  }

  /**
   * {@inheritdoc}
   */
  protected function setUp(): void {
    parent::setUp();
    // Switch to the Group add to cart form.
    $this->product_view_display = EntityViewDisplay::load('commerce_product.default.default');
    if (!$this->product_view_display) {
      $this->product_view_display = EntityViewDisplay::create([
        'targetEntityType' => 'commerce_product',
        'bundle' => 'default',
        'mode' => 'default',
        'status' => TRUE,
      ]);
    }
    // Ensure ajax is disabled on the variations vado add to cart form.
    $this->product_view_display->setComponent('variations', [
      'type' => 'commerce_vado_group_add_to_cart',
      'region' => 'content',
      'label' => 'hidden',
      'settings' => [
        'combine' => TRUE,
      ],
      'third_party_settings' => [
        'commerce_ajax_atc' => [
          'enable_ajax' => '0',
        ],
      ],
    ]);
    $this->product_view_display->save();
    // Create the vado add to cart form display.
    $order_item_form_display = EntityFormDisplay::load('commerce_order_item.default.vado_group_add_to_cart');
    if (!$order_item_form_display) {
      $order_item_form_display = EntityFormDisplay::create([
        'targetEntityType' => 'commerce_order_item',
        'bundle' => 'default',
        'mode' => 'vado_group_add_to_cart',
        'status' => TRUE,
      ]);
    }
    // Use the variation title for the vado add to cart form.
    $order_item_form_display->setComponent('purchased_entity', [
      'type' => 'commerce_product_variation_title',
    ]);
    $order_item_form_display->save();

  }

  /**
   * Tests ajax with vado add to cart form.
   *
   * @todo get cart quantity to show on page so we can check that.
   */
  public function testAjaxVadoAddToCartForm() {
    // Check the vado add to cart form without ajax.
    $this->drupalGet('/admin/commerce/config/ajax-settings');
    $this->getSession()->getPage()->selectFieldOption('pop_up_type', 'non_modal');
    $this->getSession()->getPage()->fillField('non_modal_message', 'Successfully Added!');
    $this->getSession()->getPage()->pressButton('Save configuration');
    $this->drupalGet('/admin/commerce/config/ajax-settings');
    $this->assertSession()->fieldValueEquals('non_modal_message', 'Successfully Added!');
    // Add the product to the cart.
    $this->drupalGet($this->product_1->toUrl());
    $this->assertSession()->pageTextContains('0 items');
    $this->getSession()->getPage()->pressButton('Add to cart');
    // Test the custom message does NOT appear.
    $this->assertSession()->assertWaitOnAjaxRequest();
    $this->assertSession()->pageTextNotContains('Successfully Added!');
    // Test the cart quantity in storage and on the page.
    $this->orderStorage->resetCache([$this->cart->id()]);
    $this->cart = $this->orderStorage->load($this->cart->id());
    $this->assertCount(1, $this->cart->getItems());
    $this->drupalGet('cart');
    $this->assertSession()->fieldValueEquals('edit-edit-quantity-0', 1);

    // Enable Ajax on the variations vado add to cart form.
    $this->product_view_display->setComponent('variations', [
      'type' => 'commerce_vado_group_add_to_cart',
      'region' => 'content',
      'label' => 'hidden',
      'settings' => [
        'combine' => TRUE,
      ],
      'third_party_settings' => [
        'commerce_ajax_atc' => [
          'enable_ajax' => '1',
        ],
      ],
    ]);
    $this->product_view_display->save();
    // Add the product to the cart and check the ajax requests.
    $this->drupalGet($this->product_2->toUrl());
    $this->assertSession()->pageTextContains('1 item');
    $this->getSession()->getPage()->pressButton('Add to cart');
    // Test the message, and that the cart block refreshed.
    $this->assertSession()->assertWaitOnAjaxRequest();
    $this->assertSession()->pageTextContains('Successfully Added!');
    $this->assertSession()->pageTextContains('2 items');
    $this->assertSession()->pageTextContains($this->variation_1->getOrderItemTitle());
    $this->assertSession()->pageTextContains($this->variation_2->getOrderItemTitle());
    // Test the cart quantity in sorage and on the page.
    $this->orderStorage->resetCache([$this->cart->id()]);
    $this->cart = $this->orderStorage->load($this->cart->id());
    $this->assertCount(2, $this->cart->getItems());
    // Test the cart page.
    $this->drupalGet('cart');
    $this->assertSession()->fieldValueEquals('edit-edit-quantity-0', 1);
    $this->assertSession()->fieldValueEquals('edit-edit-quantity-1', 1);
  }

}