当订单在我们的D7网站上提交时,会向我们发送一封电子邮件,其中包含订单的所有细节,包括付款方式。
我在商业电子邮件配置页面中使用令牌[commerce-order:payment-method],它工作得很好,但我希望将该令牌的值更改为更具可读性的内容,如“此订单已付款”。
这是我到目前为止在一个定制模块中所拥有的内容:
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,所以我可能误解了什么。有没有人?
发布于 2019-10-15 21:01:28
在您的示例中,token_find_with_prefix()不返回令牌,而是返回类似或(如果它们存在)的令牌。
您应该使用的正确代码如下。
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';
}
}
}另外请注意,更改令牌返回的值并不会更改类似令牌的返回值。如果您需要更改返回的值,代码需要与下面的代码类似。
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' (但这是您需要验证的内容)。
发布于 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]),因此我在电子邮件中使用了该字段,该字段立即生效。
/**
* 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.
Payment:
[commerce-order:payment-method-title] // will show "This order is paid" when this payment method is used
etc....https://drupal.stackexchange.com/questions/287334
复制相似问题