Passing variable to Magento widget - magento

I'm trying to pass a variable using setData to a box containing a widget with this:
$this->getChild('my_box')->setData('myvar', '123');
echo $this->getChildHtml('my_box');
or this:
echo $this->getChild('my_box')->setData('myvar', '123')->toHtml();
The "my_box" is the block linked with the widget, it's positioned in the footer and defined in local.xml:
<reference name="footer">
<block type="core/text_list" name="my_box" as="my_box" translate="label">
<label>My Box</label>
</block>
</reference>
But if I try to retrieve the value in the widget with any of these methods:
echo $this->getData('myvar');
echo $this->getMyVar();
echo $this->myvar;
there is no return value, any suggestion?

Outside of a rewrite, "core/text_list" is an instance of Mage_Core_Block_Text_List (link) and would not have a template for you to call your code.
You can verify that the basic functionality is working, though:
$my_box = $this->getChild('my_box')->setData('myvar','123'); //if no error, my_box exists!
echo get_class($my_box); //Mage_Core_Block_Text_List
var_dump($my_box->debug()); //array() including 'myvar' => '123'
echo $my_box->getData('myvar')` //correct
echo $my_box->myvar //works, but unconventional
echo $my_box->getMyVar() //will not access the property you set; rather...
echo $my_box->getMyvar() //will work
For other fun, you can set properties via layout XML:
<reference name="footer">
<block type="core/text_list" name="my_box" as="my_box" translate="label">
<label>My Box</label>
<action method="setMyvar">
<arbitrary>123</arbitrary>
</action>
<!-- or <action method="setDatar">
<name>myvar</name>
<val>123</arbitrary>
</action> -->
</block>
</reference>
Also, do be aware the the footer block is permanently cached in the block_html cache by default.

Related

Magento second header

I have a Magento store with two separate headers. Let's say out of 20 pages 15 use header.phtml and 5 use headerGreen.phtml. I've already created my custom layout module however I can't seem to get the new header to appear on select pages. The new headerGreen.phtml file has been created as well. The issue lies when I try to call the new header in the newly created layout page.
<?php echo $this->getChildHtml('headerGreen') ?>
I dont think I've added it correctly to the local.xml and was hoping someone could help.
<default>
<reference name="header">
<block type="core/template" name="header" template="page/headerGreen.phtml" />
</reference>
</default>
The function getChildHtml is expecting you to send a name of a child in the layout as first parameter
app/code/core/Mage/Core/Block/Abstract.php defines it :
/**
* Retrieve child block HTML
*
* #param string $name
* #param boolean $useCache
* #param boolean $sorted
* #return string
*/
public function getChildHtml($name = '', $useCache = true, $sorted = false)
{
// the code is does not really matter here
}
So based on your layout you should actually do
<?php echo $this->getChildHtml('header') ?>
Because header is the name attribute in this xml node of your layout :
<block type="core/template" name="header" template="page/headerGreen.phtml" />
As said in the comments, your layout is also looking strange because you are actually telling that your new header is going to be a child of the current header.
Two thinks you can do :
Use the powerfulness of the action nodes :
<default>
<reference name="header">
<action method="setTemplate">
<template>page/headerGreen.phtml</template>
</action>
</reference>
</default>
Or redefine the whole header block
<default>
<reference name="root">
<block type="core/template" name="header" as="header" template="page/headerGreen.phtml" />
</reference>
</default>

Magento 1.9.2.1: Color, size attribute of product not showing on onepage checkout

Recently I have added attributes to the product color and size. (configurable products)
When we add to cart, and go to view cart then we see the products with attributes color and size.
I added shopping cart block in onepage checkout in app/design/frontend/smartwave/porto/layout/iwd_opc.xml
<reference name="content">
<block type="checkout/cart" name="checkout.cart" template="checkout/cart.phtml">
<block type="checkout/cart_coupon" name="checkout.cart.coupon" as="coupon" template="checkout/cart/coupon.phtml"/>
<block type="checkout/cart_totals" name="checkout.cart.totals" as="totals" template="checkout/cart/totals.phtml"/>
<block type="checkout/cart_sidebar" name="checkout.cart.sidebar" as="sidebar" template="checkout/cart/sidebar.phtml"/>
</block>
</reference>
I get the following page after adding shopping cart layout to onepage cart
Now I am stuck here, how should I get the attributes name here?
I have set attributes to be visible on frontend from backend as well.
Code to display products in the cart is on app/design/frontend/smartwave/porto/template/checkout/cart.phtml
<?php foreach($this->getItems() as $_item): ?>
<?php echo $this->getItemHtml($_item) ?>
<?php endforeach ?>
Logic of the code is written over here: app/code/core/Mage/Checkout/Block
public function getItems()
{
if ($this->getCustomItems()) {
return $this->getCustomItems();
}
return parent::getItems();
}
Another way of displaying the products to the cart is: But I dont want to use this code to display the list in the cart:
$cart = Mage::getModel('checkout/cart')->getQuote();
foreach ($cart->getAllItems() as $item) {
$productName = $item->getProduct()->getName();
$productPrice = $item->getProduct()->getPrice();
echo "Name: ".$productName." Price: ".$productPrice;
}
I have also tried to add attributes in xml file
app/code/core/Mage/Sales/etc/config.xml
<item>
<product_attributes>
<color/>
<size/>
<condition/>
<sku/>
<type_id/>
<name/>
<status/>
<visibility/>
<price/>
<weight/>
<url_path/>
<url_key/>
<thumbnail/>
<small_image/>
<tax_class_id/>
<special_from_date/>
<special_to_date/>
<special_price/>
<cost/>
<is_recurring/>
<recurring_profile/>
<gift_message_available/>
<msrp_enabled/>
<msrp/>
<msrp_display_actual_price_type/>
</product_attributes>
</item>
So I have just copied the block to the onepage checkout page, but I am not able to understand that why the onepage checkout page is not showing product's size and color ("/onepage/") and it is strange that color and sizes can be seen on checkout page ("checkout/cart/")
Thanks in advance
I believe you have not added itemrendere in your layout. Basically cart items are listed using checkout/cart/item/default.phtml.
I will suggest to look over checkout.xml layout file and copy the whole layout <reference name="content"> node into your iwd and then start removing the template includes which you do not require.
Another method is using layout updates which will extend your iwd layout to checkout_cart. Which will do same thing above. You need to use <remove name=""> tag to remove extra cart includes from your iwd handle.
Finally I found the solution with the help of Blastfreak
Include following lines in app/design/frontend/smartwave/porto/layout/iwd_opc.xml
<reference name="content">
<block type="checkout/cart" name="checkout.cart" template="checkout/cart.phtml">
<block type="checkout/cart_coupon" name="checkout.cart.coupon" as="coupon" template="checkout/cart/coupon.phtml"/>
<block type="checkout/cart_totals" name="checkout.cart.totals" as="totals" template="checkout/cart/totals.phtml"/>
<block type="checkout/cart_sidebar" name="checkout.cart.sidebar" as="sidebar" template="checkout/cart/sidebar.phtml"/>
<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>
</reference>
Output page:

where is this method doing in admin controller (Grid Serializer)?

<?php
class Excellence_Manager_Adminhtml_ManagerController extends Mage_Adminhtml_Controller_action
{
public function customerAction(){
$this->loadLayout();
$this->getLayout()->getBlock('customer.grid')
->setCustomers($this->getRequest()->getPost('customers', null));
$this->renderLayout();
}
I am following this grid serializer tute . I just can not understand where is this setCustomers coming from ?
Even in other tutes I saw that setClent(), setDog() etc. But where is get or it's not from the database either. not in the layout either I think. Please help thanks.
<manager_adminhtml_manager_customer>
<block type="core/text_list" name="root" output="toHtml">
<block type="manager/adminhtml_manager_edit_tab_grid" name="customer.grid"/>
<block type="adminhtml/widget_grid_serializer" name="grid_serializer">
<reference name="grid_serializer">
<action method="initSerializerBlock">
<grid_block_name>customer.grid</grid_block_name>
<data_callback>getSelectedCustomers</data_callback>
<hidden_input_name>links[customers]</hidden_input_name>
<reload_param_name>customers</reload_param_name>
</action>
<action method="addColumnInputName">
<input_name>position</input_name>
</action>
</reference>
</block>
</block>
I found it's not from hidden_input_name or reload_param_name.
__set() is run when writing data to inaccessible properties.
__get() is utilized for reading data from inaccessible properties.
Magento implements so called "Magic Methods" to set data in an object.
They implemented __set and __get which are called, when no method for e.g. setCustomers() is found, and use those methods to do setData('customers', "YOUR_VALUE");

Pager toolbar in custom module in magento

I use this tutorial and display products on the basis of multiple categories but now I am getting in issue is that the pager tool bar not working on that.
My block code is :
<reference name="content">
<block name="mymodule" type="mymodule/product_listcategories" template="catalog/product/list.phtml">
<action method="setCategories">
<ids>2,3,4</ids>
</action>
</block>
</reference>
I also add this code with above
<block type="catalog/product_list_toolbar" name="product_list_toolbar" template="catalog/product/list/toolbar.phtml">
<block type="page/html_pager" name="product_list_toolbar_pager"/>
</block>
<action method="setToolbarBlockName"><name>product_list_toolbar</name></action>
</block>
It display toolbar but the toolbar is not working (limit,orderby).
My block code is
class Mymodule_Block_Product_Listcategories extends Mage_Catalog_Block_Product_List
{
protected function _getProductCollection()
{
$this->_productCollection = Mage::getModel('catalog/product')->getCollection();
$this->_productCollection->addAttributeToSelect('*');
if($this->getCategories()!="")
$this->_productCollection->addCategoriesFilter($this->getCategories());
return $this->_productCollection;
}
}
}
Does anyone know where is the problem? I think I'm missing some code for the pager? Thanks in advance
After search so much i got a solution.I didn't know whether it right or wrong way but it solve my problem.On my block i create intance of
$cpBlock = $this->getLayout()->getBlockSingleton('Mage_Catalog_Block_Product_List_Toolbar');
and access the pager core function like $this->_itemPerPage = $cpBlock->getLimit(); .In the above code itemperpage is the total no of items to be displayed on listing page.This code work if you create custom module and extend you block from list block.Thanks

Unable to prevent Magento from Caching a Block

I'm working on a Magento 1.6 site, which has the following xml inside the home page's CMS "Layout Update XML" field:
<reference name="content">
<block type="catalog/navigation" name="catalog.category.home" as="homecategory" template="catalog/category/homecategory.phtml" />
</reference>
As the template shows randomized categories, I would like to disable caching for this block.
To do so, I attempted using getChildHtml('sub-block-template', false) with the following:
(homecategory has $this->getChildHtml('random_categories', false) in its template)
<reference name="content">
<block type="catalog/navigation" name="catalog.category.home" as="homecategory" useCache="false" template="catalog/category/homecategory.phtml">
<block type="catalog/navigation" name="catalog.category.home.randcats" as="random_categories" useCache="false" template="catalog/category/random.phtml" />
</block>
</reference>
So now I'm stuck, wondering why I can't prevent caching of that block, despite using the 'false' argument.
I had this same problem. I beleive it has to do something with the block type of type="catalog/navigation". I've seen this disabling of caching work on other types of blocks. Here is a fix for this block type and this problem:
phtml file change: make sure the second param is false
echo $this->getChildHtml('topCategoriesList',false);
xml file change:
Add these actions to the block
<block type="catalog/navigation" name="topCategoriesList" as="topCategoriesList" template="catalog/navigation/categorylist.phtml">
<action method="unsetData"><key>cache_lifetime</key></action>
<action method="unsetData"><key>cache_tags</key></action>
</block>
Have you tried forcing it by creating a new custom block type and overloading the caching functions? Extend the Mage_Catalog_Block_Product_List_Random class and create an empty pseudo-constructor:
protected function _construct() {}
This will prevent inheriting adding cache tags, lifetime, and other metadata to the block object. Then you can overload the cache key info as well so that it doesn't use any existing (or enabled) cache blocks. For example:
public function getCacheKeyInfo()
{
return array(
'MY_CACHE_TAG',
Mage::app()->getStore()->getId(),
(int)Mage::app()->getStore()->isCurrentlySecure(),
Mage::getDesign()->getPackageName(),
Mage::getDesign()->getTheme('template')
);
}

Resources