Ad

How To Remove/replace Smart Collection Rules From Shopify With Ruby

I have created a script to add conditional rules to a Shopify Smart Collection but before it does that I need it to remove all the current conditions so that the new condition is the only one there. I am running into an issue that kicks back an undefined error when trying to remove them.

Script:

@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"

Error Output:

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

I've tried to remove each attribute separately which is not the correct way I am sure and it removes the attribute but not the entire rule and results in an error upon saving.

Script:

@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"

Error Output:

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

Error lookup:

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

"rules"=>[#, #"tag", "relation"=>"equals", "condition"=>"test12"}, @prefix_options={}, @persisted=false>]}, @prefix_options={}, @persisted=true, @remote_errors=#, @validation_context=nil, @errors=#>, @messages={:conditions=>["are not valid"]}, @details={:conditions=>[{:error=>"are not valid"}]}>

I tried .destroy and got the following error:

Script:

@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>

I am not sure what I am missing or doing wrong. Any point in the right direction would be greatly appreciated!

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

Ad

Answer

I just wanted to note that I've created a bandaid for our issue, although for multiple collections it may not be the correct way to write a script and loop through the problematic collections. For future individuals who are trying the same thing I've included the script that seems to work.

@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
Ad
source: stackoverflow.com
Ad