繁体   English   中英

如何使用Ruby从Shopify中删除/替换Smart Collection规则

[英]How to remove/replace Smart Collection rules from Shopify with Ruby

我创建了一个脚本来向Shopify Smart Collection添加条件规则,但是在此之前,我需要它删除所有当前条件,以便新条件是其中的唯一条件。 我遇到一个问题,尝试删除它们时会踢出未定义的错误。

脚本:

@update_collection = ShopifyAPI::SmartCollection.find(411011140)
@a = @update_collection.rules[0].attributes
@a.delete_all
@update_collection.rules << ShopifyAPI::Rule.new(:column => 'tag', :relation => 'equals', :condition => 'test12')
@update_collection.save
puts "#{update_collection.title} Updated"

错误输出:

NoMethodError: undefined method `delete_all' for #<ActiveSupport::HashWithIndifferentAccess:0x007fc2cbafd008>

我试图分别删除每个属性,但这不是我确定的正确方法,它删除了属性,但没有删除整个规则,并且在保存时导致错误。

脚本:

@update_collection = ShopifyAPI::SmartCollection.find(411011140)
@a = @update_collection.rules[0].attributes
@a.delete("column")
@a.delete("relation")
@a.delete("condition")
@update_collection.rules << ShopifyAPI::Rule.new(:column => 'tag', :relation => 'equals', :condition => 'test12')
@update_collection.save
puts "#{update_collection.title} Updated"

错误输出:

irb(main):1806:0> @update_collection.save
=> false

错误查找:

irb(main):1818:0> @ update_collection.errors

“规则” => [#,#“标签”,“关系” =>“等于”,“条件” =>“ test12”},@ prefix_options = {},@ persisted = false>]},@ prefix_options = { },@ persisted = true,@ remote_errors =#,@ validation_context = nil,@ errors =#>,@ messages = {:conditions => [“无效”]}},@ details = {:conditions => [{ :error =>“无效”}}}>

我尝试.destroy并出现以下错误:

脚本:

@update_collection = ShopifyAPI::SmartCollection.find(411011140)
@a = @update_collection.rules[0].attributes
@a.destroy
@update_collection.rules << ShopifyAPI::Rule.new(:column => 'tag', :relation => 'equals', :condition => 'test12')
@update_collection.save
puts "#{update_collection.title} Updated"

NameError: undefined local variable or method `params' for #<ActiveSupport::HashWithIndifferentAccess:0x007fc2cc168280>

我不确定我缺少什么或做错了什么。 朝正确方向的任何观点将不胜感激!

Shopify API文档: https ://help.shopify.com/api/reference/smartcollection

我只是想指出,我为我们的问题创建了一个创可贴,尽管对于多个集合而言,这可能不是编写脚本并遍历有问题的集合的正确方法。 对于将来尝试相同操作的个人,我提供了似乎有用的脚本。

@a = ShopifyAPI::SmartCollection.find(COLLECTIONID)
# The line above finds the collection via ID
@a.rules = nil
# The line above changes all of the rules to a "null" value
@a.save
# After changing the condition rules to null we save
@b = ShopifyAPI::SmartCollection.find(COLLECTIONID)
# We go back into the collection and add a new rule (this will be the only existing rule we have now)
@b.rules << ShopifyAPI::Rule.new(:column => 'tag', :relation => 'equals', :condition => 'YOURTAG')
# The above line will add a new rule
@b.save
# We save it again to add the new rule
# If you'd like to test it you can add "@b.reload" to reload the collection information that currently exists and you should see your older rules removed and the new one in place

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM