Create a Custom Block from Observer in Magento - php

I need to write an Observer to show a products block at the bottom of Product details page.
So, I have already written capturing the event 'catalog_product_load_after'. But I need to create a Custom Block which can be called through getChildHtml() in product details page.
Any help will be appreciated.

It sounds like you don't need an observer at all if the event you're waiting to fire is simply for the product details / view page to load? I might be wrong but please provide the context and gist of what you want to happen.
I assume you want to add a static block to the bottom of a product's view? If so, no need to add an observer.
To use getChildHtml('block_name'), you need to define it in your theme's catalog.xml. Add the following within <reference name="content"> of the layout handle catalog_product_view:
<block type="cms/block" name="block_name">
<action method="setBlockId"><block_id>block_identifier</block_id></action>
</block>
Then, in your product view.phtml, you can add:
<?php echo $this->getChildHtml('block_name'); ?>
in the place you want it to appear.

Related

Magento 2 About Us Layout

We want to edit the left part of the page layout of about us as seen in the photo, we want to remove the "compare" part and customize it. Where can we edit it?
For this functionality i see you're using layout 2columns-left
To edit this functionality in magento you can do it by updating the layout file for your page.
You can remove this part of the page by using this xml handle in your xml config:
<referenceBlock name="catalog.compare.sidebar" remove="true"/>
After you have removed compare from left sidebar you can create your own block and assign it to left sidebar using this command
<referenceBlock name="left">
<block name="your.custom.block.name" class="Vendor\Module\Block\YourBlock" template="Vendor\Module::template.phtml" />
</referenceBlock>
These two command layouts updates will do:
First one will remove the compare products from sidebar
Second one will add your custom block to the sidebar, where you can put code you need to
Only thing you should be carefull to put this is on right layout handle.
The handle for this will lay in your module: Vendor\YourModule\view\frontend\layout
In case that you showed here it would be
cms_tmt_about_us.xml
Please check this in your configuration that this is the right handle

Modifying product view without modfying the phtml file?

I've yet to find a proper answer for this, therefore I decided to ask it here.
I'm new to Magento and trying to develop an extension that'll modify the product and category pages.
I want to make it work in every template, meaning I'll need to find a way to modify these pages without editing the template files.
How can I achieve this functionality?
you can use the following observer to change the content in template(phtml) file with edit the core template file.
core_block_abstract_to_html_before
core_block_abstract_to_html_after
refer this
You could also use the given Child-Blocks within the respective view files. /catalog/product/view.phtml for example contains <?php echo $this->getChildHtml('other');?>. So you could use that and add a block via your layout.xml and refer to that, for example:
<catalog_product_view>
<reference name="product.info">
<block type="core/template" name="product.info.other" as="other" template="someTemplate.phtml"/>
</reference>
</catalog_product_view>
You could use any of the blocks echoed in the template and just wrap it in your own template file. Of course, you wouldn´t really change the whole page really, but you could add a lot without touching any core .phtml-files.

Magento, custom product list

I made my own product list page based on Mage_Catalog_Block_Product_List:
app/code/local/Mage/Catalog/Block/Product/Special.php:
class Mage_Catalog_Block_Product_Special extends Mage_Catalog_Block_Product_List
{
/* Original contents */
/* Here I call addAttributeToFilter on product collection, and then... */
return $this->_productCollection;
}
I include this in a CMS page on the center column:
<reference name="content">
<block type="catalog/product_special" template="catalog/product/list.phtml" />
</reference>
The problem is: The product list shows up just fine, but I get no layered navigation in my left column.
This is quite strange, since I am using exactly the same template as normal listings.
A couple of things I have checked:
Mage_Catalog_Block_Product_Special just being a proxy class. This doesn't work. Even if I use block type "catalog/product_list" on my CMS page I will not get a layered navigation.
There are no extensions that are overriding crucial core classes.
I have also tried to create my own module and list it under for example 'mycatalog'. This results in exactly the same problem.
I have a feeling this has to do with trying to include a product listing on a CMS page but I have not been able to track down the exact problem.
Any help on this would be very much appreciated.
You need to add the Block that renders the Layers into the left column.
If you look in catalog.xml, you will see that the catalog_category_layered node includes the following:
<reference name="left">
<block type="catalog/layer_view" name="catalog.leftnav" after="currency" template="catalog/layer/view.phtml"/>
...
</reference>
So, add that into your CMS xml layout update and that should display the Layer block on your CMS page.
If you have issues with the product list not being filtered when you click one of the filterable attributes, that is probably because the links rendered by the Layer_View Block assume that they are going to post back to a CategoryController, not a CmsController which will be rendering your CMS page. That may or may not be an issue, so report back here if it doesn't work and we can try to work through it.
Personally I would have a hidden category with all products in it and use a script to keep that updated to be all products. The script can be done with the API, slow yes, but it will work in a timely fashion for a certain level of SKU count.
layer navigation is include in category view.
You should include the part of navigation used by category, so try to insert
<reference name="left">
<block type="catalog/navigation" name="catalog.leftnav" after="currency" template="catalog/navigation/left.phtml"/>
</reference>
before
<reference name="content">
I'm not sure but if it's doesn't works it should help you to understand the problem anyway
check the following pages ( they came across the same problem )
http://www.exploremagento.com/magento/some-custom-blocks-to-help-you-show-products.php
http://www.chilipepperdesign.com/2009/03/23/magento-how-to-list-all-the-products-in-a-single-category-in-a-sidebar-block

Magento - > I want the product grid in admin to be displayed in frontend

I want the grid in admin to be displayed in frontend.
The grid that shows the list of products in admin-> catalog->manage products.
I want to show this on the frontend for each category?
What would be ideal approach to get this done?
Extending admin block classes in your module and copy widgets template files from adminhtml allowing adminhtml display in frontend, but you need to load required javascripts and CSS to allow grid works and you'd be pay attention to conflicts.
Probably it's more easy to using a thirdy part ajax grid in frontend.
You need to rewrite blocks
Mage_Adminhtml_Block_Catalog_Product_Grid
Mage_Adminhtml_Block_Widget_Container
to your own, because in this classes we have logic for editing, adding products.
also you need to redefine
adminhtml/default/default/template/catalog/product.phtml
adminhtml/default/default/template/widget/grid.phtml
and add templates to the frontend scope with your customizations.
add to the frontend catalog.xml blocks declaration for catalog_category_default node
it might be like this:
<catalog_category_default>
<reference name="content">
<block name="module_name/catalog_product_grid" template="module_name/catalog_product_grid.phtml" output="toHtml />
</reference>
</catalog_category_default>
I agree with emalstyle.
You can get the data from a collection, convert collection data to js object and implement a widget like:
https://plugins.jquery.com/tag/datagrid/

how can i add a new price template to magento

how can i add a new price template to the category view (template/catalog/product/list.phtml) without changing the price template that is used in (template/catalog/product/view.phtml)?
both files uses the template/catalog/product/price.phtml, but i need a separate price template in template/catalog/product/list.phtml that don't show the text "special price".
thank you very much for your help.
nafex
after a long search time on the internet i found a nice way:
in the file catalog.xml i just added the actions:
<action method="addPriceBlockType"><type>simple</type><block>catalog/product_price</block><template>catalog/product/new_price.phtml</template></action>
<action method="addPriceBlockType"><type>grouped</type><block>catalog/product_price</block><template>catalog/product/new_price.phtml</template></action>
<action method="addPriceBlockType"><type>configurable</type><block>catalog/product_price</block><template>catalog/product/new_price.phtml</template></action>
to the block:
<block type="catalog/product_list" name="product_list" template="catalog/product/list.phtml">
You could always override the Mage_Catalog_Block_Product_List block with a block in your own module and then reimplement the _getPriceBlockTemplate method which is used by the call to getPriceHtml for the product list. This should allow you to specify your own price templates for this block specifically, giving you control over not only the catalog page lists, but the ones for related and upsell products as well.
Hope that helps!
Thanks,
Joe

Categories