Magento: placing block on custom template - magento

We are creating a custom page in which we are making something of a product wizard and the final step, we want to pull in the product page, really the catalog/product_view block. Obviously we can create the block and set it in the page with the renderview method but the problem we are having is its only that block and none of its children, and since the view.phtml pulls in a ton of getChildHtml blocks nothing works.
I am thinking there has to be a way to place the product view block with all of its children in a custom non product page. Without having to create every single child block and their child blocks, is there a way to programmatically pull in all the blocks that are in an xml file like the catalog_product_view handle in catalog.xml and place it on another page?
The page we are trying to put it on is a category page using a custom template where we are doing a lot of PHP in the template file to do the wizard.

Have you tried something like this?
//save the product in the registry so the block knows which product to display
$product = Mage::getModel('catalog/product')->load($productId);
Mage::register('product', $product);
//get the block from the layout and render it to html
$Block = Mage::getSingleton('core/layout');
$product_view = $Block->createBlock('catalog/product_view');
$product_view->setTemplate('catalog/product/view.phtml');
$product_view->toHtml();
I've had partial success with this code (part of the product view block is displayed and then I get an error). Hopefully it will point you in the right direction.

Related

Adding an image in place of product in listing page

I am looking to add one image in place of a product on a category listing page - one image occupying all the space which would normally be taken up by the product image, name, price etc. on the listing page. I am able to add a static block no problem, but can't quite figure this out. Is this possible with Magento's native functionality?
You can achieve this by just using css property background-image. if you want to add image in whole content in listing page then it parent div use background-image, if you want to give image in each product block in listing page then also use css.
You can simply override the phtml template and in the loop where the products are being shown, check for the id/sku of the product, for which you want to put a custom image. Then simply echo your custom image and continue with the loop.
Pseudo Code:
foreach (products as product)
{
if(product->sku == 'YOUR_PRODUCT')
{
echo CUSTOM_IMAGE;
continue;
}
//LEAVE THE OTHER PARTS OF TEMPLATE AS IT IS
echo product->name;
echo product->image; // and others
}

Product attribute to show inside of static block on product page?

I have a magento template which has placed some static blocks on the product view pages. I have created some additional product attributes such as "product compatibility" for example, and would like to display this product attribute within a static block on the product page. That is, for each product you look at, the product attribute content will be different, but the position on the page will be the same because its within the static block.
I have tried using a tag such as
{{block type="core/template" template="catalog/product/compatibility.phtml"}}
within my static block, but it does not seem to do anything all and just displays that code on the frontend. I have turned off the wysiwyg for the editor(s), and have created a file named compatibility.phtml and just filled it with the test php code of:
<?php echo 'I am working!' ?>
I dont know if getting it to look t a file like this is even needed at all in order to grab the product attribute data though.
Is this even possible?
Is possible but is a little bit complicated, if you are not a developer, besides any custom attribute can be displayed on product view pages without any static block in Additional Information section. You only need to create attribute, set "Visible on Product View Page on Front-end" to Yes and assign that attribute to an "Attribute sets".

Magento: Include a specific product price in a CMS block w/o customization

Is there a way to include the price of a specific product in a static CMS block without writing an own Magento module?
I tried:
{{block type="catalog/product_view" product_id="1234" template="catalog/product/price.phtml"}}
This uses the fact that Mage_Catalog_Block_Product_View loads the product from the product_idattribute. But this is only working if no product is in the registry. So on the product detail page the product of the detail page is used.
I also had a look at Mage_Catalog_Block_Product::getProduct. This can load from a product id, but only if it is wrapped in the product field.
Something like
{{block type="catalog/product" product.product_id="1234" template="catalog/product/price.phtml"}}
is not working, as it will set the property product.product_id, not something nested like product->product_id.
An other option would be to add an own template file that loads the product from the productId param, but I think that is not clean.
Any cool ideas?
I've not attempted to load the price in the way you suggest above before, however the immediate problem I can see is that the price.phtml file is expecting a product object, rather than just an ID.
What I'd do instead is simply make a custom .phtml file which accepts an ID from the layout and returns the desired HTML. You can then use this anywhere you like, regardless of what product is loaded by the registry or otherwise.
The key methods for your .phtml file would be:
$id = $this->getProductId(); to load the ID from the layout, then $product = Mage::getModel('catalog/product')->load($id); to get the product object.
Now that you have the loaded product you can output any attributes and formatting you desire - e.g. $product->getFinalPrice() would give you the final sale price of the product.
Now to include your custom block, change your XML code to {{block type="core/template" template="path/to/your/file.phtml" name="someReferenceName" product_id="1234"}}.
The equivalent in XML is:
<block type="core/template" template="path/to/your/file.phtml" name="someReferenceName"><action method="setProductId"><product_id>1234</product_id></action></block>
Note: it is important to set a name for the block, because any arguments passed to the block - i.e. product_id - will be stored against this reference name. If you fail to provide a reference name, at least in the XML equivalent, your block will not receive the passed ID. I have not tested whether the same is true of the smarty-style syntax, but I expect it will be.
Hope that helps!
There is an extension from IcebergCommerce, called Single product Embed Widget. This seems to have the feature you need, configurable to show only the product price: http://icebergcommerce.com/software/magento-product-embed-widget.html

Managing Magento Footer Content

I have a portion of page in footer.phtml whose content I want to change on each new page, according to different categories. How can I get this done, or can I access product attribute outside of the product page? Is this possible?
regards
You can do this by defining your own block, though the specifics depend on more detail about the requirement. Define a block that will output the footer content you want whenever you are on a page with the current category defined:
$category = Mage::registry("current_category");
if($category) {
// write your output here
}
$product = Mage::registry("current_product");
if($product) {
// write your output here
}
Note that it is generally bad practice to have your global footer change based on each page. At that point, it isn't a global footer any longer.
Thanks,
Joe

Magento: How to tell if you're on a category page, or product page in .phtml file(s)

I am trying to program into my .phtml files an if statement if the guest is on a category list page, or on a product page.
For example this code:
<?= Mage::app()->getFrontController()->getRequest()->getRouteName(); ?>
Returns "catalog" whenever l'm on a page other than a CMS page.
Is there a way l can use a similar method to know if the user is looking at a root category, sub category or an individual product page?
Any help would be greatly appreciated!
It's been a while since I've dealt with frontend catalog pages, but give this a try.
Current versions of Magento register certain global variables (not PHP globals, but things global to the Magento system) on certain pages.
Calling the following
$category = Mage::registry('current_category');
$product = Mage::registry('current_product');
$product = Mage::registry('product');
will either return null if the objects haven't been set (i.e. you're on a page without a category or a product), or return category and product objects.
If a product object is returned you're on a product page.
If no product object is returned but a category object is, you're on a category page. Category objects have a method to getting the parent id
$category->getParentId()
A category without a parent id should be a top level category, categories with parent ids should be sub-categories.
That should give you what you need to identify where the current request is.
UPDATE: Coming back to this almost a decade later -- I likely would not rely on the contents of the registry alone to determine the page I'm on. Instead I'd use the full action name in combination with looking for the above the objects.
While Alan's answer will work, there is a more direct option, and you were actually on the right track with your code snippet... you just need to inspect the controller name rather than the module name:
<?php Mage::app()->getFrontController()->getRequest()->getControllerName(); ?>
That will return category or product based on their controllers being CategoryController.php and ProductController.php respectively.
This does assume that you've not installed any third-party modules that rewrite those controllers with their own.
I am not a big fan of checking if the current_category registry exists, because basically any controller could do this and it wouldn't necessarily mean it's a category. My way of doing it is a bit more robust:
$fullActionName = Mage::app()->getFrontController()->getAction()->getFullActionName();
if ($fullActionName == 'catalog_category_view') {
... //Category
}
elseif ($fullActionName == 'catalog_product_view') {
... //Product
}
I am afraid you are trying to do it the wrong way. I might be wrong, because you have not explained what is it exactly that you want to achieve, but I would use the layout xml to include your block on a product page with a parameter (say product-page="1") and similiarly on a category page (category-page="1").
Then you would be able to tell if you are on a product page or category page by examining those parameters inside your block:
if($this->getProductPage()) {
//this is a product page, do some stuff
}
elseif($this->getCategoryPage()) {
//this is a category page, do some stuff
}
Differentiating between main and subcategory pages might be more difficult, the first thing that comes to mind is analysis of the request variables, but that is certainly not the best approach.

Resources