How to call specific Static Blocks inside product descriptions in Magento? - magento-1.7

I tried adding a static block to my product description by adding the following code to my description page:
<?php echo $this->getLayout()->createBlock('cms/block')->setBlockId('description02')->toHtml() ?>
This works. But what can I do if I want to use different static blocks for different products? I don't want the same static block to show on all products. Is there way that I can choose which static block shows?
Thanks!

You can proceed as follows :
Create a new text type product attribute from Catalog -> Attributes and add it in used Attribute set from Catalog -> Attribute sets
After this you can add different static block names into this field while adding/editing products.
On product details page use the same you are using above, just make the static block to fetch name from the above created attribute.
Hope this will help.
UPDATE
Lets suppose you created a new attribute named "static_block" then go to Attribute sets, click on the set you are using for the product you want to show the block into.
Drag and drop the static_block from the unassigned to assigned attribute and save Attribute set.
After this create multiple static block block for ex : block_1, block_2
Edit product and in the field assigned above enter block_1 or block_2
After this on product description template i.e catalog/product/view.phtml add code as per following example :
<?php
$blockId = $_product->getData('static_block');
OR
$blockId = $_product->getStatic_block();
Whichever works
echo $this->getLayout()->createBlock('cms/block')->setBlockId($blockId)->toHtml() ?>
you may need to tweak in above code as i have not tested this.
Hope this helps.

1) Create Attribute caller for example static_block_description_code
2) Attach this attribute to proper Attributes Set in Admin section too (set, which You use for you're desired product)
3) go to (in my example) app/design/frontend/PACKAGE/default/template/catalog/product/view/description.phtml and search line responsible to display standard description
I have:`
<div class="std">
<?php echo $this->helper('catalog/output')->productAttribute($this->getProduct(), $_description, 'description') ?>
</div>`
and i've changed it to `
<div class="std block-description">
<?php echo $this->helper('catalog/output')->productAttribute($this->getProduct(), $_description, 'description') ?>
<?php echo $this->getLayout()->createBlock('cms/block')->setBlockId($block_description)->toHtml() ?>
</div>`
On top of this file I've added:
<?php $_product = $this->getProduct(); ?>
<?
$block_description = $_product->getData('static_block_description_code');
?>

Related

Magento 2 : How to generate Add to Cart URL

I am looping some specific products on the home page but unable to generate the ADD TO CART URL in magento 2
How to generate ADD TO CART URL when displaying products in loop.
I know it's been a year since this has been touched on, but as I've just done what has been requested, I thought that I may post my solution to help others.
The other answers will work, but only for simple products, where no further input is required (e.g. selecting options). When using getAddToCartUrl(), Magento first checks if the products requires any options to be selected; if it does, then it will simply supply the URL to the product page instead.
To skip all of these checks and directly obtain the add to cart URL, then make use of the Magento\Checkout\Helper\Cart helper instead. If you are within a .phtml file, then this can be utilised simply by calling $this->helper:
$cartHelper = $this->helper('Magento\Checkout\Helper\Cart');
From there, you can generate the add to cart URL via getAddUrl(), ensuring you pass the product object as the parameter:
echo $cartHelper->getAddUrl($product)
For it to fully work, you must have a hidden field for the form key as described in the other answers, and if the product in question has compulsory options to choose from (e.g. a configurable product), then make sure you include those fields as well, otherwise you will get redirected to the product page itself, with a message informing the user that options are required.
The excellent solution is to use
$cartHelper = $this->helper('Magento\Checkout\Helper\Cart');
and after that
echo $cartHelper->getAddUrl($product);
this give add to cart URL every time
(for a simple product, for a simple product with custom options etc)
use following to generate add to cart URL in magento2:
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$listBlock = $objectManager->get('\Magento\Catalog\Block\Product\ListProduct');
$addToCartUrl = $listBlock->getAddToCartUrl($product);
Add your button code into form with form key.. It will work
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$listBlock = $objectManager->get('\Magento\Catalog\Block\Product\ListProduct');
$addToCartUrl = $listBlock->getAddToCartUrl($product);
<form data-role="tocart-form" action="<?php echo $addToCartUrl; ?>" method="post">
<?php echo $block->getBlockHtml('formkey')?>
<button type="submit"
title="Add to Cart"
class="action tocart primary">
<span>Add to Cart</span>
</button>
</form>
You can generate "add to cart" url by following code:
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$listBlock = $objectManager->get('\Magento\Catalog\Block\Product\ListProduct');
$addToCartUrl = $listBlock->getAddToCartUrl($_product);

Joomla Component Formatting Form Inputs

I want to be able to format a field in Joomla. I'm creating a form that has a number of inputs and I want to format the inputs to have a yellow background and be of various lengths. Currently my form is produced in the standard Joomla way:
<div class="tablecol1">
<?php echo $this->form->getLabel('dob'); ?>:
</div>
<div class="tablecol2">
<?php echo $this->form->getInput('dob'); ?>
</div>
I've looked through the JForm stuff but I can't figure out how to control the format of the generated input box?
help would be great thanks
Setting a class for the inputs would be the quickest and easiest way as mentioned by MasterAM. You can then style it however you wish using CSS.
If you need to change the HTML or particular attributes that are not possible to set through the default parameters, then the next option is to create your own field type.
You can either override the existing ones or create ones with new names. For example you could copy the checkbox field (/library/joomla/form/fields/checkbox.php) into your own folder (/components/com_mycomponent/form/fields).
If you leave it as JFormFieldCheckbox it will override the default one. If you rename it - e.g. JFormFieldCustomCheckbox then you can have your own one.
The primary function you will want to look at is getInput(). This generates the HTML and will let you create your own input html with whatever attributes you wish.
To use custom attributes/settings from your form xml file, in your getInput() function you will use something like:
$fieldsize = $this->element['field_size'];

Best practice: Zend View: Load content from database and render PHP-code included in content

Let's say I load a value from a database which return something like:
<?php
//Zend_Controller_Action
public function indexAction()
{
$dbContent = "<p>Hello <?php echo $user?>!</p>";
$this->view->paragraph = $dbContent;
}
?>
How is it possible, that
<?php echo $user?>
will be rendered?
What precaution need to be taken (safety issuses, XXS)?
Thanks so much indeed!
== Edit: ==
Sorry, I obviously formulated my question misunderstandingly. What I actually ment:
I would like to avoid implementing a template engine like smarty.
In my project, there will be content that has PHP-Code within a string and that needs to be parsed.
Example:
<?php
//Zend_Controller_Action
public function indexAction()
{
$dbContent = "<p>Hello <?php echo $user?>!</p>";
$this->view->paragraph = $dbContent;
}
<?php
//viewscript.phtml
$user = 'John Doe';
echo $this->paragraph;
?>
is supposed to output:
Hello John Doe!
Is there any safe solution to do this without an external template engine?
Thanks once more... :-)
If found a solution here, which seems to perfectly fill my needs.
Thanks to all who answered here,
==UPDATE==
Unfortunately the posted link is dead. However, the solution was pretty simple. As far as i Remember, it went through the following steps:
Fetch content from database and save it in a file
Use Zend_Cache to check, whether this file exists
If file exists, simply render it. If not, go to step 1.
==UPDATE II ==
Found a copy of the page:
archive.org
In zend framework you will be always be able to print string (or whatever you want) from a controller but it's a very bad practice.
You should give the $user value from the controller to the view in this way:
$this->view->paragraph = $user;
and then, in the view, have:
<p>Hello <?php echo $this->paragraph; ?>!</p>
To ensure this code from XSS you should do some check before (before you print the value) like this:
$user = strip_tags($user);
Zend Framework doesn't support automatic output escaping , but you can prevent XSS in many ways.
First of all push all values into view layer and then print them with a View Helper like Zend\View\Escape , by default it returns string under htmlspecialchars() but you can set a callback function simple with :
//view.phtml
$this->setEscape('yourClass','methodName');
$this->setEscape('functionName');
echo $this->escape($this->myGreatDbValue);
Sure you can create your custom View Helper for all your need.
Another way is to create a custom View class extending Zend\View\Abstract , override __get() magic method and filtering output .
Read documentations for Zend View Helper and Zend Filter: http://framework.zend.com/manual/en/zend.filter.html
http://framework.zend.com/manual/en/zend.view.helpers.html

Symfony dynamic model form template

I am developing symfony based web application. I have many Models (Laptop, Netbook, Ipad,Tablet.... all these models inherited from Product model).Based on these models I also have Forms (LaptopForm, NetbookForm...so on).
In my action class I get Model name and assign it to template :
$modelForm = $this->modelName.'Form';
$this->form = new $modelForm();
Then in my template I do that <?php echo $form ?> ..There is no problem it prints all fields and labels in html table.
But my problem is that I want to divide template in to 2 parts. General and special fieldset.In general fields set i want to display Product model fields(name,price...).But Special field set changes according to product type. How I can handle this special fields set?Can someone give a clue or source please?
Thanks in advance!
You can manage it manually, in your specialized form class (not alter the base class).
Perhaps, with the use of sfWidgetFormSchema :
http://www.symfony-project.org/forms/1_4/en/A-Widgets#chapter_a_sfwidgetformschema
You have to name the widget 'general' and 'special', for a stanhdard re-use in form template, like this :
<?php echo $form['general'] ?>
<?php echo $form['special'] ?>

enclosing custom form element with form tag (drupal 6.x)

I've created custom form using FAPI for my site. And I place each control at specific location base on template provided by the designer. For instance -
<div id="myform">
<span>Enter Your Name : </span> <?php print drupal_render($form['name']); ?>
<span>Gender : </span><?php print drupal_render($form['gender_radio']); ?>
....
</div>
<?php print drupal_render($form['submit']); ?>
Here's my question - How do I enclose all the elements inside form tag? Is hardcoding the form tag inside the template file right way to do in drupal? or is it better to create in hook_form? But doing so would require me to add closing form tag at the end manually. Any suggestion would be highly appreciated.
Drupal - 6.x
It sounds like maybe you read about building individual fields, but skipped over some basic concepts of FAPI. In short, if you call the form with drupal_get_form(), you get the form container (and many of the benefits of using FAPI, e.g. tokens, validation, etc.) automatically. To handle the markup that goes around your form elements, you can then use #prefix, #suffix, and markup elements.
You can assemble the whole form from the outside in like you're doing, but there are few cases in which that would really be worthwhile. If you really want to do that, you basically want to copy what drupal_get_form() does to get the form wrapper added in a way that will work with FAPI.