首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何更改商业令牌的值?

如何更改商业令牌的值?
EN

Drupal用户
提问于 2019-10-15 14:51:42
回答 2查看 52关注 0票数 0

当订单在我们的D7网站上提交时,会向我们发送一封电子邮件,其中包含订单的所有细节,包括付款方式。

我在商业电子邮件配置页面中使用令牌[commerce-order:payment-method],它工作得很好,但我希望将该令牌的值更改为更具可读性的内容,如“此订单已付款”。

这是我到目前为止在一个定制模块中所拥有的内容:

代码语言:javascript
复制
    function mymodule_tokens_alter(array &$replacements, array $context) {

  if ($context['type'] == 'commerce-order') {
    // Find token starting with commerce-order:payment-method.
    if ($value_tokens = token_find_with_prefix($context['tokens'], 'payment-method')) {
      if (!empty($value_tokens['payment-method']) && $value_tokens['payment-method'] === 'card_payment') {
        $replacements[$value_tokens['payment-method']] = 'This order is paid';
      }
    }
  }
}

当电子邮件到达时,令牌的价值仍然是card_payment,所以我可能误解了什么。有没有人?

EN

回答 2

Drupal用户

发布于 2019-10-15 21:01:28

在您的示例中,token_find_with_prefix()不返回令牌,而是返回类似或(如果它们存在)的令牌。

您应该使用的正确代码如下。

代码语言:javascript
复制
function mymodule_tokens_alter(array &$replacements, array $context) {
  if ($context['type'] == 'commerce-order') {
    if (!empty($replacements['payment-method']) && $replacements['payment-method'] == 'card_payment') {
      $replacements['payment-method'] = 'This order is paid';
    }
  }
}

另外请注意,更改令牌返回的值并不会更改类似令牌的返回值。如果您需要更改返回的值,代码需要与下面的代码类似。

代码语言:javascript
复制
function mymodule_tokens_alter(array &$replacements, array $context) {
  if ($context['type'] == 'commerce-order') {
    if (!empty($replacements['payment-method-title']) && $replacements['payment-method-title'] == 'the payment title you want to change') {
      $replacements['payment-method-title'] = 'This order is paid';
    }
  }
}

请记住使用需要更改的有效支付标题更改'the payment title you want to change',这可能不是'card_payment' (但这是您需要验证的内容)。

票数 1
EN

Drupal用户

发布于 2019-10-16 11:18:21

我确信kiamlaluno提供的答案是正确的,我也不知道为什么它对我不起作用。也许它确实起作用了,而另一个商业模块再次改变了这个值,但是如果其他人正在为类似的问题挣扎,我想分享一个非常简单的解决方法。

付款方法表单是用钩子hook_commerce_payment_method_info()创建的。作为这个钩子的一个选项,可以使用一个名为display_title的额外字段,该字段将用于向用户显示表单的标题。如果未指定此字段,则默认为title字段。更多信息,这里

因此,在本例中,字段title可以用于其他目的,而且由于该字段的值也可以“开箱即用”作为admin/commerce/config/email页面中的替换令牌([commerce-order:payment-method-title]),因此我在电子邮件中使用了该字段,该字段立即生效。

代码语言:javascript
复制
/**
 * Implements hook_commerce_payment_method_info().
 */
function invoice_payments_commerce_payment_method_info() {
  $payment_method = array();

  $payment_method['card_payment'] = array(
    'base' => 'card_payment',
    'weight' => 0,
    'title' => t('This order is paid'),
    'display_title' => t('Card payment'),
    'description' => t('Pay your order with card.'),
    'active' => TRUE,
    'offsite' => FALSE,
    'offsite_autoredirect' => FALSE,
  );

  return $payment_method;
}

然后在Order电子邮件配置页面中:

N ny best har gjorts p hemsidan.

代码语言:javascript
复制
Payment:

[commerce-order:payment-method-title] // will show "This order is paid" when this payment method is used
etc....
票数 0
EN
页面原文内容由Drupal提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://drupal.stackexchange.com/questions/287334

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档