Is there a way to get cart information into a Shipping Script

Shopify Partner
223 1 14

Hello,

I really hate the new change that Shopify made to the Scripts separating them into three types of scripts D: Now the scripts I did before wont work. I was wondering if anyone knew a way to automatically put free shipping on a cart if the cart has a certain product. Is this possible?

Thanks for the help in advance :D

Entrepreneur, Developer, Geek, Gamer and very passionate about WEB development! :P

Need help with your Shopify Store? Contact me: jfeuchter@gmail.com :D
1 Like
Explorer
66 0 25

Hey Jurgen - you can still access the cart and products as you would in a line item script in your shipping script code.

Author of http://gavinballard.com/mastering-shopify-themes/ and http://bootstrapforshopify.com.
0 Likes
Shopify Partner
223 1 14

When I use the "Input.cart" call on the shipping script, I get an error that it is nil. How would I call the cart and the customer on a shipping script? Do you have a example for that ? Thanks for the coments :D

Entrepreneur, Developer, Geek, Gamer and very passionate about WEB development! :P

Need help with your Shopify Store? Contact me: jfeuchter@gmail.com :D
0 Likes
Shopify Partner
223 1 14
def cartHasNido?
  for item in Input.cart.line_items
    if  item.variant.product.id == 9085489217
      return true
    end
  end
  return false
end

def freeShippinng
  Input.shipping_rates.each do |shipping_rate|
    next unless shipping_rate.source.include?('SkyDrop')
    shipping_rate.apply_discount(shipping_rate.price, message: "¡Envio Gratis!")
  end
  
  Output.shipping_rates = Input.shipping_rates
end

puts cartHasNido?

customer = Input.cart.customer

unless customer && customer.orders_count > 0
  puts "flag no customer or new customer"
  if cartHasNido?
    if (Input.cart.discount_code)
      Input.cart.discount_code.reject(message: "Tu carrito ya cuenta con descuento de envio gratis.")
      freeShippinng
    end
  end
end

This is the code I have, and I get his error: 

[Error] undefined method 'map' for nil
  shopify/std_lib/cart_shipping_rates/output.rb:7:in #<Class:0x7fef37ab4f20>.to_hash
  shopify/std_lib/core/script_kernel.rb:12:in Object.prepare_output
  (prepare_output):1
--

Im trying to see which object is nil but Im not sure which one it is D:

 

Entrepreneur, Developer, Geek, Gamer and very passionate about WEB development! :P

Need help with your Shopify Store? Contact me: jfeuchter@gmail.com :D
0 Likes
Shopify Partner
223 1 14

Ok I found the problem haha. The problem was that you cant call for an object which is not in the cart. So if the cart does not have a customer you cant call that method cause it will tell you its nil. Also the script must allways end with the output = input line of code. This is the final code I had:

 

def cartHasNido?
  for item in Input.cart.line_items
    if  item.variant.product.id == 9085489217
      return true
    end
  end
  return false
end

def freeShippinng
  Input.shipping_rates.each do |shipping_rate|
    if shipping_rate.source.include?('shopify')
      shipping_rate.apply_discount(shipping_rate.price, message: "¡Envio Gratis!")
    end
  end
end

#puts cartHasNido?

customer = Input.cart.customer

#puts customer.orders_count

if  customer
  order_count = customer.orders_count
else
  order_count = 0
end
unless order_count != 0
  #puts "flag no customer or new customer"
  if cartHasNido?
    if (Input.cart.discount_code)
      #Input.cart.discount_code.reject(message: "Tu carrito ya cuenta con descuento de envio gratis.")
      #freeShippinng
    else
      freeShippinng
    end
  end
end

Output.shipping_rates = Input.shipping_rates

This code is pretty simple, it cheks if a certain product is in the cart. If it is and the customer is his first time buying, the code will set Free Shipping on all Shopify shippings we have. Pretty neat :D Thanks gavin for point out that it is possible to use cart and other objects on shipping scripts! :D Hope this code helps someone :P 

Cheers! :D 

Entrepreneur, Developer, Geek, Gamer and very passionate about WEB development! :P

Need help with your Shopify Store? Contact me: jfeuchter@gmail.com :D
0 Likes
Highlighted
Shopify Expert
704 0 68

It helped me, thanks doods

I'm a million different people
0 Likes
Excursionist
39 1 9

Hey @Jurgen_Feuchter ,  I am working on the discount script but it shows me errors like given below screencasts. I took that code from here https://help.shopify.com/en/manual/apps/apps-by-shopify/script-editor/examples#percentage-off-all-it... so please let me know how can I resolve the issue I need to complete this asap.

 

Thank you in Advance!!

 

2019-04-06_1159.png2019-04-06_1200.png

0 Likes
Excursionist
39 1 9

Hey @Jurgen_Feuchter ,

 

I also wanted to set script like it will allow to provide free shipping and % discount both together and i found a script for that https://ethercycle.com/shopify-scripts/ from here but there also I am having issue, I will attach the screencasts of issues below. Please check and let me know how can i resolve. Please Help!!

 

Thank you.

 

1. Script i am referring on that page 

2019-04-06_1213.png

 

2. Issue One 

2019-04-06_1215.png

 

3. Second Issue

2019-04-06_1216.png

0 Likes