Solved

Can't access line item's handle

RobertFXR
Shopify Partner
20 0 1

Hi, there; I'm doing some minor modifications to an example script (buy x get y) because I'd like to match one of the 'get' products by handle (this script has to work across 5 similar plus stores, so matching by id is not ideal)

Currently this line: line_item.variant.product.handle.downcase.strip

is giving me this error: [Error] undefined method 'handle' for #<Product:0x7f8f2f16d5f0>

Is it really not possible to access the product's handle, or is there something else causing this error maybe? Thanks for any info!

 

 

Accepted Solution (1)
PaulNewton
Shopify Partner
6274 573 1319

This is an accepted solution.

Let the stores MSM know your use case for why this feature is needed

 

If your only using the online store channel you could update the theme to use Line Item Properties to pass the product handle to cart|checkout so it's more automatic though you may need to update theme cart and notifications not to show LIP's ( use single underscore _ prefix , or double underscore convention to hide LIPS during checkout) 

 

You can see some of what's available using the .methods method, other inspection features( .attributes, pp )  are disabled since scripts are a locked down version of ruby also without things like active record

puts line_item.variant.methods(false)
puts product.methods

pass false to not show inherited methods

Save time & money ,Ask Questions The Smart Way


Confused? Busy? Get the solution you need paull.newton+shopifyforum@gmail.com


Problem Solved? ✔Accept and Like solutions to help future merchants

Answers powered by coffee Buy Paul a Coffee for more answers or donate to eff.org


View solution in original post

Replies 5 (5)

RobertFXR
Shopify Partner
20 0 1

Also, is there somewhere I can access a full list of what properties I have access to? 

There's this link, but doesn't seem like it's totally relevant to shopify scripts? https://shopify.dev/docs/admin-api/rest/reference/products/product

 

PaulNewton
Shopify Partner
6274 573 1319

 

Hi, there; I'm doing some minor modifications to an example script (buy x get y) because I'd like to match one of the 'get' products by handle (this script has to work across 5 similar plus stores, so matching by id is not ideal)

If need be a work around is to go through product.tags method, though this can be a hassle if you need to create this convention on lots of products and keep everything in sync when product names change.


@RobertFXR wrote:

Also, is there somewhere I can access a full list of what properties I have access to? 

 


https://help.shopify.com/en/manual/checkout-settings/script-editor/shopify-scripts

 

While most methods and properties you may see in scripts are simply standard, not all shopify methods or properties are documented fully and clearly.

I've had issues with just getting the full variant.title and that's in the list of properties!

Save time & money ,Ask Questions The Smart Way


Confused? Busy? Get the solution you need paull.newton+shopifyforum@gmail.com


Problem Solved? ✔Accept and Like solutions to help future merchants

Answers powered by coffee Buy Paul a Coffee for more answers or donate to eff.org


RobertFXR
Shopify Partner
20 0 1

Thanks!

Hmm. Seems like a given that you'd be able to access the handle. Seems very weird to have to replicate your handle as a tag.

For now I've created a few temporary tags to get around this, but not ideal if we had to do this for many many products.

PaulNewton
Shopify Partner
6274 573 1319

This is an accepted solution.

Let the stores MSM know your use case for why this feature is needed

 

If your only using the online store channel you could update the theme to use Line Item Properties to pass the product handle to cart|checkout so it's more automatic though you may need to update theme cart and notifications not to show LIP's ( use single underscore _ prefix , or double underscore convention to hide LIPS during checkout) 

 

You can see some of what's available using the .methods method, other inspection features( .attributes, pp )  are disabled since scripts are a locked down version of ruby also without things like active record

puts line_item.variant.methods(false)
puts product.methods

pass false to not show inherited methods

Save time & money ,Ask Questions The Smart Way


Confused? Busy? Get the solution you need paull.newton+shopifyforum@gmail.com


Problem Solved? ✔Accept and Like solutions to help future merchants

Answers powered by coffee Buy Paul a Coffee for more answers or donate to eff.org


RobertFXR
Shopify Partner
20 0 1

Super helpful, Paul. Thanks!