How to load Page Attribute into variable for use in a form ?

Permalink
I have created custom attribute e.g. product_price for Portfolio items
I can display the attribute easily using the Page Attribute Display block
And I can add a HTML block with a form to add to externally hosted shopping cart
But I have to manually set (i.e. not read the attribute) the price value of the form which is not efficient.

How can I read the Page Attribute value, assign to a variable and use this in the Add to Cart form ?

I have installed the PHP block but this fails on leaving edit mode with error message :
Call to a member function getCollectionAttributeValue() on null

timconsidine
 
siton replied on at Permalink Reply
siton
In general you can get the value of custom attributes like this:
$price  = $c->getAttribute('attribute_handle');// $c is global varibale for the current page
echo $price

C5 docs :: Getting Data about a Page -
http://documentation.concrete5.org/developers/working-with-pages/ge...

***Most of time you need to format the view of the price with some function - The value 9999 need to be --> "9,999$". or euro and so on.
Basic example:
http://www.w3schools.com/php/func_string_money_format.asp...

Now for you main Q: Its really hard to know from you post what cart and/or what block or form you talking about. Maybe add more details.

"cart form" for example - this is really wide term (Cart is list or table of summary data - not "form")
timconsidine replied on at Permalink Reply
timconsidine
Many thanks

I added this into a PHP block, and in edit mode, it works fine.
<?php 
    $price  = $c->getAttribute('product_price');
    echo $price
?>

But when I save or publish changes, I get a fatal error :
Call to a member function getAttribute() on null
I then have to delete the latest version to get back to working version.

As regards form, it is a standard html form.
I use external Foxycart shopping cart (form values are encoded when page rendered)
<form action="https://xxxx.foxycart.com/cart" method="post" accept-charset="utf-8">
<input type="hidden" name="name" value="6 Deck Bundle" />
<input type="hidden" name="price" value="64.99" />
<input type="hidden" name="code" value="CRD-SIXBUN" />
<input name="weight" value="0.9" type="hidden" />
<input name="category" value="CardDecks" type="hidden" />
<div>
   <label for="form1_Format">Format</label>
   <select name="Format"><option value="CardDeck">CardDeck</option></select>
</div>
<input type="submit" value="Add to Cart" class="submit" />
</form>

Form and shopping cart submission all working when populated with manual values.
But I want to read these values from the page attributes, rather than manually configure on each product, and make updates easier/more consistent

Many thanks for your prompt help.

[Using C5 5.7 and Framework theme]
siton replied on at Permalink Reply
siton
Part One:
You need to create this attribute + match the handle name + set value (in the composer form - set this to mandatory value its better because price is mandatory in store)...In your code in general its better to add "If"s - to avoid this errors.

This is separate issue - try at first create custom "hello-world" attribute and print "hello-world" on the some page (than try to create more complicated code).

Part two:
Static:
<input type="hidden" name="price" value="64.99" />

Change to:
<input type="hidden" name="price" value="<?php echo $price ?>" />

Now you value is dynamic.
timconsidine replied on at Permalink Reply
timconsidine
Many thanks, @siton

Totally understand Part 2 - thank you

About Part 1 : the attribute is already set with handle "product_price" in a Set with handle "portfolio". Values are populated in the Attributes for each portfolio page

using php echo hello world, the page saves and publishes fine. But adding the attribute lookup, it fails.

Is there something else I should be referencing ?
siton replied on at Permalink Reply
siton
Again:
1. Check handle name (Nothing to do with set name. the set is more for organize your data in the admin UI).
2. Set the value (check again if in your specific page you set this value).

Look for example on post about this error:
https://www.concrete5.org/community/forums/usage/conditional-image-t...
siton replied on at Permalink Reply
siton
"HOW TO CREATE HELLO WORLD ATTRIBUTE" its more for tutorial not for forum post.

I hope this will help you - because i do this really fast and short:
From composer/admin panel side (controller consept):
1. Create page att : handle name "hello_world_handle" (Select type "text")
2. Page type ---> edit form --> add this attribute (or in edit gear mode manually to specific page add this attribute)
3. Set this attribute to "i am new hello world attribute"

From theme side (view consept):
In you root folder go to some test file (full.php) for example and get the value:
<php
$hello-world = $c->getAttribute('hello_world_handle');
<h1>This is my test attribute</h1>
<p> <?php echo $hello-world ?>
</p>


The HTML should look like this:
<h1>This is my test attribute</h1>
<p>i am new hello world attribute</p>
timconsidine replied on at Permalink Reply
timconsidine
Appreciate your help and patience

Have confirmed that attribute handle name is specified correctly, and that a value is assigned.

Have tried with one of the existing attributes, and same behaviour, so not my custom attribute

I'm wondering if the theme is lacking support for this core C5 functionality
siton replied on at Permalink Reply
siton
This is not theme issue. You create this attribute manually. Look at elemental theme - you find in portfolio page some custom attributes, mabye this will help you.

And of course the "page_attribute_display" use this "getters" idea but in more dynamic way (so if you can print values from this block you also can do this by code manually).

You treat build in attribute difference again look at this link i added above:
http://documentation.concrete5.org/developers/working-with-pages/ge...

If my tut dont work again look for some deep tutorial about this issue (this is really hard to explain this without images, screenshots and so on). The main idea is to create attribute - set this attribute - display this attribute (3 parts)
timconsidine replied on at Permalink Reply 2 Attachments
timconsidine
Thank you

I created new attribute and tested again - all works fine in edit mode - see screenshot
1st instance of text value is by using a Page Attribute Display block to confirm the value is set.
Then the H1 text and text value is from using a PHP block with the code.
In edit mode, this is fine.

As soon as I publish the page, it generates the error - 2nd screenshot

I cannot think why the attribute retrieval/display works totally fine in edit mode, literally 'out the box', but in published mode, it fails.
siton replied on at Permalink Reply
siton
Try to use this:
Dashboard > System & Settings > Environment > Debug Settings

Than select the option:
() Show the debug error output

Now you will get the specific line of the php code mistake (mabye than add screenshot):
timconsidine replied on at Permalink Reply 2 Attachments
timconsidine
Thank you - not needed Debug until now and was wondering where it was !

It is complaining about the add-on PHP Block

Still I don't understand why it works in page edit mode but not after publishing, but maybe I am becoming obsessive ;-)
Gondwana replied on at Permalink Reply
Gondwana
I don't know what I'm talking about, but in edit mode, I suspect a few things are loaded/included automatically, whereas they may not be when not in edit mode (or not logged in). The 'null' dereference suggests this. Perhaps you need to explicitly include/require/use some other file/package/code.
timconsidine replied on at Permalink Reply
timconsidine
Thank you @Gondwana.
Yes, I suspect this is right, but tracking it down is beyond me.
I have messaged the add-on developer, who says core framework may have been updated, even though add-on says compatible with 5.7+. Not sure if he will fix it or not
timconsidine replied on at Permalink Reply
timconsidine
Thanks again for your help @siton

After more researching and testing, I have concluded that the PHP block add-on does not integrate well. So I have to do this outside of the concrete5 user interface. Which is a shame, but I guess not a major problem.

So I have created the php code to grab the product attributes and stored this in the page template (e.g. left_sidebar.php) at the start of the file (above layout code)

Then I have removed the HTML block with the shopping cart call/values from the concrete5 page editor, and inserted in the left_sidebar at the preferred location. This time using php echo variables for the form values.

All works sweetly !! Yippeee, thanks again

One task remains :
- how to read some variable extra product attributes in a select box format (attributes created easy - done) and output the correct number of options into the HTML form

Will have to think about this overnight
timconsidine replied on at Permalink Reply
timconsidine
OK, in case it helps anyone else ...

Now displaying Page Attributes from a Select attribute inside form code as :
<div>
   <label for="form1_Format">Choose Format (price may vary) : </label>
   <select name="Format">
   <?php
    foreach ($prod_formats as $opt) {
             echo '<option value="'.$opt.'">'.$opt.'</option>';
        }
   ?>
   </select>
</div>


Works fine, so this problem solved - thanks to all for help