Set attribute value?

Permalink
How do I set an attribute value in code? I see how to get attribute values (http://www.concrete5.org/community/forums/customizing_c5/get-ecomme... ), but I cannot for the life of me figure out how to set them (it's just a text value).

In case it helps, this is a custom attribute I created for the eCommerce module. I am trying to output a hidden form field that has the attribute key as the name and the attribute value as the value (so the attribute key I can retrieve from the database, but the value is different for each product I'm outputting and is determined through other means -- but if someone clicks the "Add to Cart" link, I need this value to be saved with that product and that order).

Thanks!

-Jordan

jordanlev
 
cgrauer replied on at Permalink Reply
cgrauer
$c->setAttribute($akHandle, $value );


where $c is a page or collection object, $akHandle is the attribute handle and $value is the value to set.
bastien31 replied on at Permalink Reply
Hi,

I have a problem to save a hidden attribute value at the registration. I explain.

I have two kinds of users for my website. So, I want to differenciate them when they register. I have two different kinds of registration form and login form (one for each type).

So, I want that when a user of type 1 register on my website, I automatically put the attribute called 'usertype' (I have already added it for all users) to 1. I know it's a type 1 user because he uses the type 1 user's form registration.

Is it clear ?

I find some informations about saveAttribute, but it's not clear for me.

thanks
moosh replied on at Permalink Reply
moosh
Hi,

Why not used Groups ?

When a user is registering, put him in Group "Type 1".
bastien31 replied on at Permalink Reply
Yes ! good idea.

Now, I have create two groups. So, how can I set the group automatically when they register ?

Is there a method setGroup() or something like that ?
moosh replied on at Permalink Reply
moosh
Yes.

You can use

$u->enterGroup($g);

where $g is Group object
bastien31 replied on at Permalink Reply
I tried this in the do_register function :
// now we log the user in
            if (USER_REGISTRATION_WITH_EMAIL_ADDRESS) {
               $u = new User($_POST['uEmail'], $_POST['uPassword']);
               $group = Group::getByName("users");
               $u->enterGroup($group);                  
            } else {
               $u = new User($_POST['uName'], $_POST['uPassword']);
               $u = new User($_POST['uEmail'], $_POST['uPassword']);
               $group = Group::getByName("users");
               $u->enterGroup($group);   
            }


And it doesn't work... Do you see a problem ?
thanks
moosh replied on at Permalink Reply
moosh
You need to change the group names...

example :
$group = Group::getByName('type1');
bastien31 replied on at Permalink Reply
But "users" is the group name. The other group name is 'super_users'.

Is there a conflict ? It seems not because C5 lets me create the "users" group with no problem.
moosh replied on at Permalink Reply
moosh
} else {
    $u = new User($_POST['uName'], $_POST['uPassword']);
    $u = new User($_POST['uEmail'], $_POST['uPassword']); // delete this line
    $group = Group::getByName("users");
    $u->enterGroup($group);   
}
bastien31 replied on at Permalink Reply
Yes sorry a bad copy/paste.

But it still doesnt work :(.

thanks for help. I don't understand why. Perhaps is it not the good place to put it ?
moosh replied on at Permalink Reply
moosh
Add in finishLogin function after $u = new User(); (line 194) :

$group = Group::getByName("users");
$u->enterGroup($group);
bastien31 replied on at Permalink Reply
it was a good idea but still not working !

do you try it on your side ?
moosh replied on at Permalink Reply
moosh
Forget what i have said before ^^

Solution here :
http://www.concrete5.org/community/forums/customizing_c5/place-new-...
bastien31 replied on at Permalink Reply
thanks !!!
finally, it was not so easy to find.
jordanlev replied on at Permalink Reply
jordanlev
Sounds like you found an alternate solution. But for future reference, here's how to add a custom attribute value to your reg forms:
<input type="hidden" name="akID[<?php echo UserAttributeKey::getByHandle('your_attribute_handle')->getAttributeKeyID(); ?>][value]" value="Whatever value you want this to have" />
rc255 replied on at Permalink Reply
rc255
Hi Jordan, I know this thread is old, but I am trying to do what your initial question asks.

How to set a value to a particular "product 'text' attribute" in ecommerce? My attribute is $storename and I want to set it with the variable $storetitle which is atored as a page attribute

I'm stuck with $at->render('form');

$at->setAttribute($storename, $storetitle ); doesn't work and just brings up Fatal error: Call to a member function getAttributeValueObject() on a non-object

I bet it's just staring me in the face. I have searched everywhere for a solution and this thread is the only one I can find asking the exact same question, but there is no answer...
JohntheFish replied on at Permalink Reply
JohntheFish
The best reference on setting attributes is
http://www.webli.us/cheatsheet/doku.php...

At a guess, it looks like you are trying to set a value on an attribute type, where you need to set it on an attribute key or the object (product) the attribute is attached to.

$product_obj->setAttribute('Name', $value);

If its a product option attribute (I don't think it is), a further complication is that the handle has the pID appended, so it only affects the one product.
rc255 replied on at Permalink Reply
rc255
The attribute is a customer choice attribute, which I want to use as a hidden field and set it's value from a page attribute variable. The original code is:

$attribs = $product->getProductConfigurableAttributes();         
         foreach($attribs as $at) { ?>
         <div>
            <div class="ccm-core-commerce-add-to-cart-product-option-attributes-label"><?  echo $at->render("label")?><? if ($at->isProductOptionAttributeKeyRequired()) { ?> <span class="ccm-required">*</span><?  } ?></div>
            <div class="ccm-core-commerce-add-to-cart-product-option-attributes-value"><?  echo $at->render('form'); ?></div>
         </div>
         <?  }  ?>

and I'm trying to single out the customer choice attribute 'storename' with
if ($at->akHandle == 'storename') { // SET STORENAME TO STORE TITLE ?>         
            <div class="ccm-core-commerce-add-to-cart-product-option-attributes-label">
               <? echo $at->render("label")?>
                </div>
            <div class="ccm-core-commerce-add-to-cart-product-option-attributes-value">
               <? $at->setAttribute('storename', $storetitle );
and somehow pre-set the value here ^^^

but it's having none of it...
JohntheFish replied on at Permalink Reply
JohntheFish
try
$product->setAttribute('storename', $storetitle);
rc255 replied on at Permalink Reply
rc255
Unfortunately no

Fatal error: Call to a member function setAttribute() on a non-object in /packages/core_commerce/models/product/model.php on line 724

model.php (line 724) reads

public function setAttribute($ak, $value) {
      Loader::model('attribute/categories/core_commerce_product', 'core_commerce');
      if (!is_object($ak)) {
         $ak = CoreCommerceProductAttributeKey::getByHandle($ak);
      }
      $ak->setAttribute($this, $value);
      $this->reindex();
   }


I'm just wanting to populate the 'empty' text box with a variable which is grabbed from a page attribute. I've been wracking my mind as for a way to do it, but I've hit a brick wall
rc255 replied on at Permalink Reply
rc255
Is it possible to populate the form field (commerce customer choice) by changing:

echo $at->render('form')


to something like:

echo $at->render('form', $storetitle, true)


I've seen examples of this in other bits of code, but I can't get it to work here :(
moosh replied on at Permalink Reply
moosh
Yes you can.

Which version of C5 ?
Which attribute type ?
rc255 replied on at Permalink Reply
rc255
It's 5.6 and I'm trying to set the value for a text field which is a 'customer choice' attribute in the CoreCommerce package

$product->setAttribute('storename', $storetitle ); didn't work and neither is echo $at->render('form', $storetitle, true)

for each 'customer choice' attribute, the product page renders the required form usig:

$attribs = $product->getProductConfigurableAttributes();         
         foreach($attribs as $at) { ?>
         <div class="ccm-core-commerce-add-to-cart-product-attributes">
            <div class="ccm-core-commerce-add-to-cart-product-option-attributes-label"><?php   echo $at->render("label")?><?php    if ($at->isProductOptionAttributeKeyRequired()) { ?> <span class="ccm-required">*</span><?php    } ?></div>
            <div class="ccm-core-commerce-add-to-cart-product-option-attributes-value"><?php   echo $at->render('form'); ?></div>
         </div>

and I want to populate my text box with a string which I can grab from page variables
rc255 replied on at Permalink Reply
rc255
Finally, I have got it!

On the product page, replace:

echo $at->render('form', $image_path, true)

with:
<input name="akID[<?php echo CoreCommerceProductOptionAttributeKey::getByHandle('storename')->getAttributeKeyID(); ?>][value]" id="akID[<?php echo CoreCommerceProductOptionAttributeKey::getByHandle('storename')->getAttributeKeyID(); ?>][value]" type="text" value="<? echo $storetitle; ?>" />

This now grabs the CoreCommerceProductOptionAttributeKey and populates the text field with the $value....

Yes!

thanks everyone :D