How would I go about making a custom dynamic field on a product page?
Like this (red box):
I know I have to change the file addtocart.phtml but don't know how to code it.
It looks rather simple but I haven't figured it out yet.
Any help would be appreciated!
Hello hotdog9,
Before changing the code, you may consider adding required attribute using Magento standard functionality.
In order to perform this go to: Admin Panel -> Catalog -> Attributes -> Manage Attributes -> Button "Add New Attribute".
Here you can create attribute (field) of required type and set it up. In order this attribute to be visible on product page - set "Yes" for "Visible on Product View Page on Front-end" in attribute settings.
After this you need to set which product groups this field should be assigned to. It can be set up here: Admin Panel -> Catalog -> Attributes -> Manage Attribute Sets
For example, go to Attribute Set "Default" and drag new attribute from right column to left, to the General section. After this in an appropriate section on the Product Edit Page this filed will appear.
Do not forget to make system re-index (System -> Index Management) and clear cache (System -> Cache Management).
Best Regards,
Plumrocket Team
Hey hotdog9,
In this case, you can go to:
app/design/frontend/base/default/template/catalog/product/view/addtocart.phtml
and add the following code:
<?php if ($product->getAttributeSetId() == ATTRIBUTE_SET_ID): ?> <?php echo $product->getAttributeText('YOUR_ATTRIBUTE_CODE'); ?> <?php endif; ?>
ATTRIBUTE_SET_ID - change to ID of "fruits" attribute set. If it is more comfortable for you to check by name, and not by ID you can make the following:
<?php if (Mage::getModel('eav/entity_attribute_set')->load($product->getAttributeSetId())->getAttributeSetName() == 'fruits'): ?>
Best Regards,
Plumrocket Team
Hi again,
So I notice this problem. Whenever I edit something in that .phtml file, the stuff below it goes missing.
Say I add your code above the code for the button, and the button goes missing and everything else beneath it.
Any thoughts? I’ve tried editing a bit before on this page and stumbled upon this same problem.
Cache is disabled if it makes any difference.