I am trying to wrap my head around how to use Magento's native functions in a new module. So for a simple example lets say I have a basic shell like :
app/code/local/Me/Test/Block/Container.php
<?php
class Me_Test_Block_Container extends Mage_Core_Block_Template
{
}
and in the layout.xml I am inserting design blocks unique to category and product page :
<catalog_category_layered>
<reference name="after_body_start">
<block type="test/container" name="test.container" template="test/category_container.phtml"/>
</reference>
</catalog_category_layered>
<catalog_product_view>
<reference name="after_body_start">
<block type="test/container" name="test.container" template="test/product_container.phtml"/>
</reference>
</catalog_product_view>
</catalog_category_layered>
In those phtml I am trying to use functions to get current category on category page, and get product sku on product page. So for the category page in my category_container.phtml I am trying to use the function
<?php $_category = $this->getCurrentCategory();?>
But it returns blank. Can someone please help me understand more about this? I copied the getCurrentCategory function into Container.php but that did not work. Should I be changing the block type in the layout.xml to be able to use that function or what is the proper way to do this?
you can get the category this way:
$_category = Mage::registry('current_category');
and the product like this:
$_product = Mage::registry('current_product');
Before using it check it their value is not null.
Related
I would like to add a shopping cart block a cms page, but whenever I try, nothing happens...not even an error.
I've tried following this tutorial, http://www.magento.cc/how-to-use-php-on-a-cms-page.html.
So I created the new folders in app/code/local and then a Test.php file, yet when I try to include
{{block type="YourModule_Custom/test" my_param1="value 1" another_param="value 2"}}
in the cms page, nothing appears.
Here's my code in the Test.php page:
<?php
class YourModule_Custom_Block_Test extends Mage_Core_Block_Abstract
{
protected function _toHtml()
{
echo 'TEST';
$this->getChildHtml('header');
return $html;
}
}
You can do a local.xml update. And place your content as .phtml file
<cms_index_index>
<reference name="content">
<block type="Your_custom/Block" name="home_main" as="home_main" template="cms/default/home.phtml">
</block>
</reference>
</cms_index_index>
Create a file named home.phtml in cms/default/ Then you can use your own block type to use your custom module / functions
Then add your home page content in there.
I don't know what are you trying to achieve. But from your question, I have a strong feeling that, you are trying to set a template of your own through CMS page. If that is the case, let us analyze why your block didn't show any output.
Your block definition is like this
{{block type="YourModule_Custom/test" my_param1="value 1" another_param="value 2"}}
There is no problem with this defintion. But it is good, if you add name to your block. If you set a template along with that, then you don't need any backend code for setting a template for your block. That is your block should look like this
{{block type="YourModule_Custom/test" name="test.block" template="test.phtml"}}
Now when magento encounter this, it will find your block, set described name to that block (for later reference this name will be used. However it is not relevant in this case), set template specified to your block and then render the content in that template.
So you should have a block of type that you specified. You had it right now(you dont need that _toHtml() inside that). Along with that you need a template file test.phtml and it should be in the location app/design/frontend/<your_package>/<your_theme>/template/test.phtml. You dont have that file right now. So create it and add this content inside that file
<p><?php echo "I am here. Can you see me ?"; ?></p>
Now check your CMS page output. You can see that content. Isn't it ?
So right now what you are trying to do is, instead of setting a template along with that block definition, you are trying to set it through your block. Is it wrong ? Obviously, NO. There are some cases, where we need to go with that way. I assume you really need it. So again let us redifine our block in this form.
{{block type="YourModule_Custom/test" name="test.block"}}
Hmm. Here we didn't set a template to this block right now. You can hence set it through your block definition. You used _toHtml() method.
<?php
class YourModule_Custom_Block_Test extends Mage_Core_Block_Abstract
{
protected function _toHtml()
{
echo 'TEST';
$this->getChildHtml('header');
return $html;
}
}
?>
This method is using to set a template and then renders the content. So you are in the right track. But the problem here is, you are not setting any template!! plus your method returns a variable $html which holds nothing. So what should we return through a _toHtml() ? The answer lies in Mage_Core_Block_Template. Let us look on _toHtml() definition
protected function _toHtml()
{
if (!$this->getTemplate()) {
return '';
}
$html = $this->renderView();
return $html;
}
Basically what this does is, it checks whether a template is set , if not return nothing. If it does renders it. That means, it is obvious that we need to set a template. So your block should look like this.
<?php
class YourModule_Custom_Block_Test extends Mage_Core_Block_Template
{
protected function _toHtml()
{
$this->setTemplate('test.phtml');
$html = parent::_toHtml();
return $html;
}
}
Note that your block extends from Mage_Core_Block_Template rather from Mage_Core_Bock_Abstract. This is because setTemplate() method is defined in Mage_Core_Block_Template class. In _toHtml(), we are setting our template and then leave rest to our parent block. Now check whether content in test.phtml is showing in your CMS page. It does right ?
I don't understand exact what do you want. but by your question my understanding is you want to display shopping cart block into any CMS page like Home page, About us page etc.
If my understanding is right then here is one solution for you.
You can insert this code into your cms page from admin.
Admin -> CMS -> Pages -> Select any page on which you want to display block -> Click on design tab from left navigation ->Under Page Layout section insert the below code in "Layout Update XML" field. Click on save.
<reference name="content">
<block type="checkout/cart" name="checkout.cart">
<action method="setCartTemplate"><value>checkout/cart.phtml</value></action>
<action method="setEmptyTemplate"><value>checkout/cart/noItems.phtml</value></action>
<action method="chooseTemplate"/>
<action method="addItemRender"><type>simple</type><block>checkout/cart_item_renderer</block><template>checkout/cart/item/default.phtml</template></action>
<action method="addItemRender"><type>grouped</type><block>checkout/cart_item_renderer_grouped</block><template>checkout/cart/item/default.phtml</template></action>
<action method="addItemRender"><type>configurable</type><block>checkout/cart_item_renderer_configurable</block><template>checkout/cart/item/default.phtml</template></action>
<block type="core/text_list" name="checkout.cart.top_methods" as="top_methods" translate="label">
<label>Payment Methods Before Checkout Button</label>
<block type="checkout/onepage_link" name="checkout.cart.methods.onepage" template="checkout/onepage/link.phtml"/>
</block>
<block type="page/html_wrapper" name="checkout.cart.form.before" as="form_before" translate="label">
<label>Shopping Cart Form Before</label>
</block>
<block type="core/text_list" name="checkout.cart.methods" as="methods" translate="label">
<label>Payment Methods After Checkout Button</label>
<block type="checkout/onepage_link" name="checkout.cart.methods.onepage" template="checkout/onepage/link.phtml"/>
<block type="checkout/multishipping_link" name="checkout.cart.methods.multishipping" template="checkout/multishipping/link.phtml"/>
</block>
<block type="checkout/cart_coupon" name="checkout.cart.coupon" as="coupon" template="checkout/cart/coupon.phtml"/>
<block type="checkout/cart_shipping" name="checkout.cart.shipping" as="shipping" template="checkout/cart/shipping.phtml"/>
<block type="checkout/cart_crosssell" name="checkout.cart.crosssell" as="crosssell" template="checkout/cart/crosssell.phtml"/>
<block type="checkout/cart_totals" name="checkout.cart.totals" as="totals" template="checkout/cart/totals.phtml"/>
</block>
</reference>
After that you will see shopping cart block on your CMS page. If you don't need any block from them you can remove that blck from the above code.
if you try to call add to cart from cms page then add url like
checkout/cart/add?product=$id&qty=$qty
Example-
<a href="{{store url='checkout/cart/add?product=5&qty=3'}}">
<img src="{{skin url='images/addtocart.jpg'
}}" alt="product5" /></a>
In my magento i want to create a Block in Product Page for this itried like this
Magento- How can i add a new custom block in product details page using module
But no use
Any thing wrong i did here
Any Ideas
Hello check below code may be help you
add into product template (view.phtml) where you want
<?php echo $this->getChildHtml('mynewblock');?>
call block into your custom template xml or catalog.xml
<catalog_product_view>
<reference name="product.info">
<block type="core/template" name="mynewblock" template="hello/hello.phtml" />
</reference>
</catalog_product_view>
Desired result:
On the order success page I want to show products that are related to the ones purchased by the user.
What I did so far:
product attribute that contains related products
added echo $this->getChildHtml('related_products_list'); in checkout/success.phtml
block that extends the product_list, and sets the appropriate collection (note: this is not a rewrite)
class Namespace_CustomersBought_Block_Product_List extends Mage_Catalog_Block_Product_List {
protected function _construct() {
$orderId = Mage::getSingleton('checkout/session')->getLastRealOrderId();
// some more code to get the products I need in $relatedProducts
$this->setCollection($relatedProducts);
}
}
added in my custom.xml the following (paths are correct):
<checkout_onepage_success>
<reference name="content">
<block type="namespace_customersbought/product_list" name="related_products_list"
template="module/product/related_list.phtml" after="-">
</block>
</reference>
</checkout_onepage_success>
Where it stopped working
It renders the div I added in checkout/success.phtml, but the getChildHtml() call is empty.
Also, I use Magneto Debug - and the layout update contains my XML.
What I need help with
I would like to understand why this is not working. If I replace <checkout_onepage_success> with <cms_index_index> I get the desired block on the homepage (without having getChildHtml()), so why do they have different behavior?
Also - ideally I wouldn't need to modify the checkout/success.phtml file, it should be output automatically.
I know I'm missing something very simple, but I can't figure out what.
Thank you.
I guess, there is a problem with the line
<reference name="content">
This sets your block a child to the content block. You, however have added the output to the checkout/success.phtml template, which belongs to the block checkout.success. I suggest you replace the xml update with the following
<checkout_onepage_success>
<reference name="checkout.success">
<block type="namespace_customersbought/product_list" name="related_products_list"
template="module/product/related_list.phtml" after="-">
</block>
</reference>
</checkout_onepage_success>
I am doing a magento customaization site, I need to add the products addtional attributes like it's type,version etc .I am new to magento , How can i add the new custom block to product details page. I have created a module , and i am using below coding.
app\code\local\SmartGrowth\CompatibleWith\Block\compatible.php
class SmartGrowth_CompatibleWith_Block_CompatibleWith extends Mage_Catalog_Block_Product_View
{
protected function _prepareLayout()
{
//$this->getProduct()->setName($this->getProduct()->getPrice());
$this->getProduct()->setName($this->getProduct()->getShortDescription());
parent::_prepareLayout();
}
}
I have used the below coding in _prepareLayout() but it seem to be repeat the block 5 times and the location of the block appeared is a probs
$block = $this->getLayout()->createBlock(
'Mage_Core_Block_Template',
'my_block_name_here',
array('template' => 'catalog/product/compatiblewith.phtml')
);
$this->getLayout()->getBlock('content')->append($block);
Please help how can i do this , I am new to magento , Any help will be appreciated.
There's no need to add the block in code, it should be done using config XML files.
Create an XML config for your module (plenty of tutorials on this).
check catalog.xml (app/design/frontend/base/default/layout/)
<catalog_product_view translate="label">
....
</catalog_product_view>
This is where the blocks are setup for display on the product view page. You can modify this using your own modules XML file, something like this:
<catalog_product_view translate="label">
<reference name="content">
<block type="compatiblewith/compatible" name="my.block" template="compatiblewith/compatible/template.phtml" />
</reference>
</catalog_product_view>
this will show your custom block on the product view page, inside the content area.
You also have an error with the naming of your block if it's called Compatible.php the class should be SmartGrowth_CompatibleWith_Block_Compatible
You can add custom template in product-shop (css class name for the section beside product image) below the Quick View area without modifying core files. in your module's layout file add this code for the required result (do replace "module" "block" with your actual module and block names):
<catalog_product_view>
<reference name="content">
<reference name="product.info">
<block type="module/block" name="module_block" as="other" template="module/block.phtml"/>
</reference>
</reference>
</catalog_product_view>
the target for custom block used is "other" which is a child html provided by default in view.phtml(/app/design/frontend/base/default/template/catalog/product/view.phtml) of magento.
Hopes it'll help.
How would you go about retrieving Product Attributes in the sidebar?
I have edited my Catalog.xml like so:
<default>
<reference name="right">
<block type="core/template" template="callouts/right_template.phtml"/>
</reference>
</default>
and with my product attribute named “sidebar”, i have placed this code inside of the above referenced template file:
<?php echo $_product->getSidebar() ?>
It is pulling the content into the sidebar fine, (tested by using plain text), but the code used to retrieve the attribute is giving me a "Fatal error: Call to a member function getAttributeName() on a non-object”. I’m assuming this is a scoping issue?
(This code worked fine in pulling the attribute when it was inside of “view.phtml")
The callout block doesn't have access to the product object, try changing:
<?php echo $_product->getSidebar() ?>
and use this instead:
<?php
$_product = Mage::registry('current_product');
if($_product){
echo $_product->getSidebar();
}
?>
Notice this will only work in the product page.
Cheers!