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
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
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
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
Hi,
Why not used Groups ?
When a user is registering, put him in Group "Type 1".
Why not used Groups ?
When a user is registering, put him in Group "Type 1".
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 ?
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 ?
Yes.
You can use
$u->enterGroup($g);
where $g is Group object
You can use
$u->enterGroup($g);
where $g is Group object
I tried this in the do_register function :
And it doesn't work... Do you see a problem ?
thanks
// 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
You need to change the group names...
example :
$group = Group::getByName('type1');
example :
$group = Group::getByName('type1');
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.
Is there a conflict ? It seems not because C5 lets me create the "users" group with no problem.
} 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); }
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 ?
But it still doesnt work :(.
thanks for help. I don't understand why. Perhaps is it not the good place to put it ?
Add in finishLogin function after $u = new User(); (line 194) :
$group = Group::getByName("users");
$u->enterGroup($group);
$group = Group::getByName("users");
$u->enterGroup($group);
it was a good idea but still not working !
do you try it on your side ?
do you try it on your side ?
Forget what i have said before ^^
Solution here :
http://www.concrete5.org/community/forums/customizing_c5/place-new-...
Solution here :
http://www.concrete5.org/community/forums/customizing_c5/place-new-...
thanks !!!
finally, it was not so easy to find.
finally, it was not so easy to find.
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" />
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...
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...
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.
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.
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:
and I'm trying to single out the customer choice attribute 'storename' with
but it's having none of it...
$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...
try
$product->setAttribute('storename', $storetitle);
$product->setAttribute('storename', $storetitle);
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
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
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
Yes you can.
Which version of C5 ?
Which attribute type ?
Which version of C5 ?
Which attribute type ?
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:
and I want to populate my text box with a string which I can grab from page variables
$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
Finally, I have got it!
On the product page, replace:
with:
This now grabs the CoreCommerceProductOptionAttributeKey and populates the text field with the $value....
Yes!
thanks everyone :D
On the product page, replace:
echo $at->render('form', $image_path, true)
with:
This now grabs the CoreCommerceProductOptionAttributeKey and populates the text field with the $value....
Yes!
thanks everyone :D
where $c is a page or collection object, $akHandle is the attribute handle and $value is the value to set.