Trying to exclude Media Mail when a T-shirt is in the cart

Solved

Trying to exclude Media Mail when a T-shirt is in the cart

FaithCoder
Tourist
6 1 0

Looking for some direction here with scripts.  All of our products are eligible for media mail shipping except our t-shirts, so we're trying to remove 'media mail' from the shipping options if a t-shirt is in the cart.

 

I'm not a ruby expert but here is the code so far:

Input.cart.line_items.each do |item|
 if item.sku.upcase.include?("MS-SHIRT") || item.sku.upcase.include?("MS-TS")
  Output.shipping_rates = Input.shipping_rates.delete_if do |shipping_rate|
   shipping_rate.name.upcase.include?("MEDIA MAIL")
  end
 end
end

Output.shipping_rates = Input.shipping_rates

It is throwing this error:

[Error] undefined method 'sku' for #<Variant:0x7f7eb251fa20>
Hide media mail shipping rates:2:in Object.call
shopify/std_lib/core/list.rb:41:in List.each
Hide media mail shipping rates:1

 

sku appears to be correct for line items from the documentation that I've seen.  Any help as to what's wrong with this code would be greatly appreciated.  

Accepted Solution (1)

FaithCoder
Tourist
6 1 0

This is an accepted solution.

This code is working. Just have to set all shirts as product type 'Clothing':

 

def cartHasShirt?
 for item in Input.cart.line_items
  product = item.variant.product
   if product.product_type.upcase.include?("CLOTHING")
    return true
   end
 end
 return false
end

if cartHasShirt?
 Output.shipping_rates = Input.shipping_rates.delete_if do |shipping_rate|
  shipping_rate.name.upcase.include?("MEDIA MAIL")
 end
end

Output.shipping_rates = Input.shipping_rates

View solution in original post

Replies 5 (5)

FaithCoder
Tourist
6 1 0

This is an accepted solution.

This code is working. Just have to set all shirts as product type 'Clothing':

 

def cartHasShirt?
 for item in Input.cart.line_items
  product = item.variant.product
   if product.product_type.upcase.include?("CLOTHING")
    return true
   end
 end
 return false
end

if cartHasShirt?
 Output.shipping_rates = Input.shipping_rates.delete_if do |shipping_rate|
  shipping_rate.name.upcase.include?("MEDIA MAIL")
 end
end

Output.shipping_rates = Input.shipping_rates
Robert_MAS
Tourist
11 0 1

Hi,

 

Thanks for this code, but where does it go?

 

Thanks,

Robert

 

afbookstore
Visitor
2 0 1

It goes into the script editor. I believe that the script editor is only available to plus customers ... 

DIY_Retail
Visitor
1 0 0

Which script?

BooksR
Visitor
2 0 3

Hello Everyone,

I seem to found the solution to this. I created a new profile under Setting/Shipping and Delivery. After that, I added the products that are non-media and named those products as NON-MEDIA, but you can name anything you like. For shipping services, I choose different rates other than media-mail and saved it. 

When a customer adds any of the "non-media" items, media mail disappears as a shipping option. 

Good luck!