d
Topic
LCreek Shopify Partner
Posts:
33
October 24, 2012

How to assign an object in Liquid This post is outdated

Hi- just trying to figure out how to create an assign a value in an object.

 

So if I have an object "foo" and a property "bar", how do I assign foo.bar to a value?

 

I tried {% assign foo.bar = "Yes" %} but that does not seem to work. What am I missing?

i
Replies
Posts:
3768
October 24, 2012

Assign is more for your own variables. You cannot write to Shopify built-in Objects either, Liquid is read-only from that perspective. So when you say you have an object foo, you're really stuck with just a variable, not an object. You cannot just glue on bar to foo.

 

Custom Shopify Apps built just for you! hunkybill@gmail.com http://www.resistorsoftware.com
LCreek Shopify Partner
Posts:
33
October 24, 2012

Wow, at almost every turn I am being stymied by some really odd Shopify choices. No discount API (although it's been "in the works" for several years). No way to hide a product, and put it in a cart dynamically (seriously? that's like a one-field flag to add).  No way to create a basic object in their programming language. No way to modify the built in objects (so you could add one line of code at the top of a page and it would allow any template to work with a modified object).

 

I like Shopify and I like the app environment, but some of the things they've done seem just plain ridiculous from a developer's point of view.

 

Thanks for the response HB (saves me yet more hours of fruitless googling). Since there is an app out there accomplishing the "adding hidden product to a cart" trick, I know it can be done. I'll just have to find another way. But I keep finding myself saying "It should not be this difficult".

Posts:
3768
October 24, 2012

I thought you were clear... adding a hidden product to the cart is not the problem.. but checking out a hidden product is the problem.. no App can circumvent the fact that if a hidden product cannot be checked out... you cannot check out a hidden product. 

Once you deliver a few successful working shops, hack a few Apps into shape, master the client-side magic you can do, I think you'll change your tune. You are stuck thinking Liquid is PHP or some such nonsense. It is not. It is templating, and I think it's done well. 

You can push themes well beyond the basics with Javascript and some clear and deep understanding of Shopify. Have some patience. You're at the entry level still... a long way to go to enlightenment.

Custom Shopify Apps built just for you! hunkybill@gmail.com http://www.resistorsoftware.com
LCreek Shopify Partner
Posts:
33
October 24, 2012

Thanks for the help, but I don't think you really got what I was saying.

I fully understand that "no App can circumvent the fact that if a hidden product cannot be checked out." My question is why not? This was a decision by Shopify that has HUGE implications. If you could at least CHOOSE if you wanted that to be blocked or not (which is the matter of adding one piddly flag into the product setup), you could make products that would not be found by the consumer (such as discounted or free products), and yet allow apps to dynamically add them to the cart as desired. So you would be able to, easily, create "buy 4 get one free" or "buy a camera, get a case free" type offers that happened automatically based on cart activity.

That is a lot of functionality to toss away, because Shopify simply decided "you should never add a hidden product to a cart". That is an arbitrary decision (one many competing products do NOT employ), and it greatly limits what apps can do out of the box. (I have finally figured out how the other app gets around this, and it is exactly as convoluted as the solution I came up with). Similarly, I am dismayed that we can't edit the built-in object values once they are sent to the page. This would allow apps to change a value at the top of a page and still have the rest of ANY theme still work correctly. Put it this way, which is a more elegant solution for not showing a search result (of something you want hidden but still need to be able to add to the cart):

  1. Try to describe to someone whose template may have any number of ways of displaying search results, how to find and modify the liquid they need to prevent that item from showing (and probably mess up pagination in the process)

    OR
  2. Modify the object by removing the search result that shouldn't show.

I am not bashing Shopify. I am not trying to turn their programming into anything else. I am just tired of dealing with all of these roadblocks they chose to put in, that could be easily solved. And allowing me to create "foo.bar" seems trivial considering they already use built-in objects. There is just a lot of stuff here that seems poorly planned, despite this being a great product and a great app environment.

But I do appreciate your help. I would not have been able to figure out how to hide a product and add it to the cart dynamically (which I have) if not for your help. But it isn't enlightenment I need- it's just Shopify not putting up hurdles when they don't need to.