Set the value on the fly.

Is there a way to set the attributes value programatically? ie. when i load the product display node, how do i set my attribute values.

I tried to update this variable but the attribute select boxes did not update.

$colornid = 75;

Posted: Nov 1, 2011


rfay Randy Fay on November 1, 2011

Attributes are on the *product* entity. The product display node is not the product entity. You could programmatically *load* the product based on the value in the product reference field, and then you could set values and re-save. Sounds like trouble to me.

Maybe you can describe what you're trying to accomplish...

marvzz on November 2, 2011

I need to preselect attributes when a user visits a product display page.

My product display looks like this:

Select Boxes (#ajax) of:

Product: Tshirt (T-shirt, XL, blue) <<<< possible values: Pants (Pants, XL, blue) / Jacket (Jacket, XL, blue) / and so on...
Size: XL <<<< possible values: L / M / S
Color: blue <<<< possible values: red, white, green

I want Red as default so that it will be:

Product: Tshirt (T-shirt, XL, red)
Size: XL
Color: red

So you mean to say I should be setting the Product reference?

johnlutz on July 20, 2012

I am trying to accomplish something similar. I am checking a user field on a particular product display page and if its set to one thing I want one product to be the default. If it is set to something else or not set at all, I want a different product to be the default. How do you set the default product reference programatically in the form alter?