Shopify 插件脚本编辑器 - 未定义的方法

tq

我在Shopify 中使用了一个名为Script Editor的插件,但它给出了一个错误:
undefined method 'end_with?' for nil.

我的 Ruby 语法不太好,如果来自客户的电子邮件为空或不存在,请寻求有关如何退出命令的帮助:if customer && customer.email.end_with?("@mycompany.com")

这是Ruby 中的代码

Input.cart.line_items.each do |line_item|
  next if line_item.variant.product.gift_card?
  discount = 1

  customer = Input.cart.customer
  if customer && customer.email.end_with?("@mycompany.com") //<< needs a better condition
    message = "Lorem Ipsum"
    discount = 0.2
  end
  next unless discount < 1
  
  line_item.change_line_price(
    line_item.line_price * discount,
    message: message,
  )
end

Output.cart = Input.cart
旁观者

您可以使用 ruby​​ 的安全导航运算符“&.”。

Input.cart.line_items.each do |line_item|
  next if line_item.variant.product.gift_card?
  discount = 1

  customer = Input.cart.customer
  if customer&.email&.end_with?("@mycompany.com")
    message = "Lorem Ipsum"
    discount = 0.2
  end
  next unless discount < 1
  
  line_item.change_line_price(
    line_item.line_price * discount,
    message: message,
  )
end

Output.cart = Input.cart

来自文档

&.,称为“安全导航操作符”,允许在接收器为零时跳过方法调用。如果调用被跳过,它返回 nil 并且不评估方法的参数。

参考:https : //ruby-doc.org/core-2.6/doc/syntax/calling_methods_rdoc.html

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

Shopify:如何使自定义模板在主题编辑器中可编辑?

Google 标签管理器数据层变量未定义 - Shopify

谷歌脚本编辑器错误“记录器”未定义

Shopify的新主题编辑器中未显示自定义部分

使用CK编辑器上传图像为nil:NilClass提供了未定义的方法'size'

Shopify液体:未定义购物车模板变体

Shopify-Bridge 调用未定义的函数 getShopifyConfig()

Trumboqyg所见即所得编辑器不适用于Jquery.Validate插件,“未捕获的TypeError:无法读取未定义的属性'nodeName'”

去编译器未定义的方法

rails link_to控制器方法未定义

查询构建器上的未定义关系方法 - Laravel

控制器操作中的未定义方法new

打字稿方法装饰器:这是未定义的

后台控制器的未定义方法

Angularjs 控制器方法 arg 未定义

解析器的未定义方法`deprecation_reason'

累加器在使`reduce`方法中返回未定义

调用未定义的方法ActiveRecord \ Config :: initialise()编写器

控制器的未定义方法“get”

无法读取未定义Liferay CK编辑器内容的'getHTML'属性

在 dojo 中集成 orion 编辑器时未定义“orion”

jQuery验证与Summernote编辑器错误:无法读取未定义的属性“替换”

Wordpress 编辑器 JS 错误“TypeError:无法读取未定义的属性‘get’”

SUBLIME TEXT编辑器的控制台中未定义窗口

导入 Ace 编辑器时未定义窗口 - 已解决

Cordova插件未定义

如何正确加载ACE编辑器脚本并添加自定义突出显示规则脚本?

Eclipse插件开发-自定义编辑器首选项

使用 C# 插件在 Word 2010 中制作自定义书签编辑器