Commit 3ec0454b authored by kensae's avatar kensae
Browse files

Issue #3312009: Standard coding issue

parent 25ded419
Loading
Loading
Loading
Loading
+8 −3
Original line number Diff line number Diff line
Module to fill the gap between Commerce Gift Card and Commerce Email.

It allows you to send E-mails with the Giftcard details to a customer when the order containing the giftcard has being paid.
It allows you to send E-mails with the Giftcard details to a customer 
when the order containing the giftcard has being paid.

INSTALLATION

Install the module

IMPORTANT: You need to install the patch on this issue: https://www.drupal.org/project/commerce_giftcard/issues/3181301 which includes the 'commerce_giftcard.giftcard_activated' event, dispatched when the order has being paid.
IMPORTANT: You need to install the patch on this issue: 
https://www.drupal.org/project/commerce_giftcard/issues/3181301 
which includes the 'commerce_giftcard.giftcard_activated' event, 
dispatched when the order has being paid.

CREATE AN E-MAIL

Go to the Commerce Email confiugration page: /admin/commerce/config/emails and create a new Email entity with the Giftcard activated event.
Go to the Commerce Email confiugration page: /admin/commerce/config/emails 
and create a new Email entity with the Giftcard activated event.
In the e-mail you can use the giftcard entity tokens.
+2 −2
Original line number Diff line number Diff line
@@ -4,5 +4,5 @@ description: Sends out giftcard mails upon creation from an order
package: Commerce (contrib)
core_version_requirement: ^8.8 || ^9
dependencies:
  - commerce_giftcard
  - commerce_email
  - drupal:commerce_giftcard
  - drupal:commerce_email
+9 −3
Original line number Diff line number Diff line
<?php

/**
 * @file
 * Sends out giftcard mails upon creation from an order.
 */

use Drupal\Core\Render\BubbleableMetadata;
@@ -8,7 +10,6 @@ use Drupal\Core\Render\BubbleableMetadata;
/**
 * Implements hook_token_info().
 */

function commerce_giftcard_mail_token_info_alter(&$data) {
  $data['tokens']['commerce_giftcard']['email'] = [
    'name' => t("Email"),
@@ -24,13 +25,18 @@ function commerce_giftcard_mail_token_info_alter(&$data) {
/**
 * Implements hook_tokens().
 */
function commerce_giftcard_mail_tokens_alter(array &$replacements, array $context, \Drupal\Core\Render\BubbleableMetadata $bubbleable_metadata) {
function commerce_giftcard_mail_tokens_alter(array &$replacements, array $context, BubbleableMetadata $bubbleable_metadata) {
  $state = \Drupal::state();
  $bubbleable_metadata->addCacheableDependency($state);
  if ($context['type'] == 'commerce_giftcard' && !empty($context['data']['commerce_giftcard'])) {
    $boughtTransaction = NULL;
    $giftcard = $context['data']['commerce_giftcard'];
    $giftcard_transactions = \Drupal::entityTypeManager()->getStorage('commerce_giftcard_transaction')->loadByProperties(['giftcard' => $giftcard->id(), 'reference_type' => 'commerce_order_item']);
    $giftcard_transactions = \Drupal::entityTypeManager()
      ->getStorage('commerce_giftcard_transaction')
      ->loadByProperties([
        'giftcard' => $giftcard->id(),
        'reference_type' => 'commerce_order_item',
      ]);

    if (count($giftcard_transactions) > 0) {
      $boughtTransaction = reset($giftcard_transactions);