How to get checkout/cart_sidebar block in Magento from everywhere - magento

Let me know how can I get "checkout/cart_sidebar" block as HTML into variable ?
I need to get it from my own magento controller
As I see "checkout/cart_sidebar" depends on "Mage_Checkout_Block_Cart_Sidebar" class
so may It is possible to get this template via some Mage:: Static Instance Methods
I tried a few ways I couldnt (
thanks

In your controller you could try something like this:
$block = $this->getLayout()->createBlock('checkout/cart_sidebar');
$block->setTemplate('checkout/cart/sidebar.phtml');
Depending on your configuration (Config -> Checkout -> Shopping Cart Sidebar), you can render the template with
$block->toHtml();
If you use a custom template, you could ignore the config value so it renders anytime.

Actions in layout xml configs are just a block method call.
The two below are equivlent
<block type = "checkout/cart_sidebar"
name = "cart_sidebar"
as = "cartExplorer"
template = "checkout/cart/sidebar.phtml"
before = "-">
<action method="addItemRender">
<type>configurable</type>
<block>checkout/cart_item_renderer_configurable</block>
<template>checkout/cart/sidebar/default.phtml</template>
</action>
<!-- Programatically create the block -->
<?php
$this->getLayout()
->createBlock('checkout/cart_sidebar', 'cart_sidebar')
->setTemplate('checkout/cart/sidebar.phtml');
->addItemRender(
'configurable',
'checkout/cart_item_renderer_configurable',
'checkout/cart/sidebar/default.phtml'
)
?>
<!-- This is if it was already created in a layout.xml file -->
<?php
$this->getLayout()
->getBlock('cart_sidebar')
->addItemRender(
'configurable',
'checkout/cart_item_renderer_configurable',
'checkout/cart/sidebar/default.phtml'
)
?>
Hope this helps!

Related

Magento - Dynamic Product Attribute Only Pulling Data from Single CMS Block

I have set up 2 extra tabs on my individual product pages which should be pulling information from a different CMS Block for each product. However, when I view the product pages they are all pulling the exact same information from 1 of the CMS Blocks I've created and not from the CMS Block for that particular product.
Here's a few more things I noticed while trying to troubleshoot:
1) If I refresh the Magento cache and then go to a product page the correct information is displayed for that page. Then if I navigate to any other product page it always shows the information from the first product I viewed after I refreshed the Magento cache.
2) This seems to be related to product categories as well. I have 2 product categories. Every product in Category 1 will show the same CMS Block's info. Every product in Category 2 will show the same CMS Block's info, but the CMS Block's info for these products are actually different from the products in Category 1, but all the same.
Here's how I've set everything up:
1) Created separate CMS Blocks for each product each with different information.
2) Created a textarea attribute and added it to the correct attribute set. For each product I have entered the ID to the CMS Block for that product. I've double checked and every product has a different ID entered.
3) In app/design/frontend/rwd/default/layout/catalog.xml I have added the following to display the 2 new tabs:
<!-- Features -->
<block type="catalog/product_view_attributes" name="product.features" as="features" template="catalog/product/view/features.phtml">
<action method="addToParentGroup"><group>detailed_info</group></action>
<action method="setTitle" translate="value"><value>Features</value></action>
</block>
<!-- END Features -->
<!-- TECH SPECS -->
<block type="catalog/product_view_attributes" name="product.tech_specs" as="techspecs" template="catalog/product/view/tech_specs.phtml">
<action method="addToParentGroup"><group>detailed_info</group></action>
<action method="setTitle" translate="value"><value>Tech Specs</value></action>
</block>
<!-- END TECH SPECS -->
4) Lastly, I've created the 2 files
app/design/frontend/rwd/default/template/catalog/product/view/features.phtml
app/design/frontend/rwd/default/template/catalog/product/view/tech_specs.phtml
Here's the code:
features.phtml
<?php
$_product = $this->getProduct();
$attribute = $_product->getResource()->getAttribute('features');
if ( is_object($attribute) ) {
$identifier = $_product->getData("features");
}
?>
<?php if ($_sizeBlock = Mage::app()->getLayout()->createBlock('cms/block')->setBlockId($identifier)): ?>
<div class="std">
<?php echo $_sizeBlock->toHtml() ?>
</div>
<?php endif; ?>
tech_specs.phtml
<?php
$_product = $this->getProduct();
$attribute = $_product->getResource()->getAttribute('tech_specs');
if ( is_object($attribute) ) {
$identifier = $_product->getData("tech_specs");
}
?>
<?php if ($_sizeBlock = Mage::app()->getLayout()->createBlock('cms/block')->setBlockId($identifier)): ?>
<div class="std">
<?php echo $_sizeBlock->toHtml() ?>
</div>
<?php endif; ?>
Anyone have a clue what's going on here?
Figured it out. It's because Magento is caching the CMS Blocks.
Solution:
Copy
app/code/core/Mage/Cms/Block/Block.php
To
app/code/local/Mage/Cms/Block/ (I had to add the missing folder hierarchy)
Then edit Block.php
In protected function _construct()
Change
$this->setCacheLifetime(false);
To
$this->setCacheLifetime(null);
No more CMS Block caching and the dynamic content is displayed as expected!
If the attribute is dynamic, you can due a a hash with it's content and add it to the cache tag, see the example to a attribute that is a "yes" or "no" but if it changes the caches reloads ;)
After rewrites the bock from Magento 2.1 vendor in di.xml, do this:
namespace Dpl\Xxx\Block\Catalog\Product\View;
use Magento\Framework\Pricing\PriceCurrencyInterface;
class Attributes extends \Magento\Catalog\Block\Product\View\Attributes
{
/**
* Retrieve current product model
*
* #return \Magento\Catalog\Model\Product
*/
public function __construct(
\Magento\Framework\View\Element\Template\Context $context,
\Magento\Framework\Registry $registry,
PriceCurrencyInterface $priceCurrency,
array $data = []
) {
parent::__construct($context, $registry, $priceCurrency, $data);
$this->setCacheTag($this->getProduct()->getId().'-'.$this->getProduct()->getXxxopen();
}
}
?>
Regards ;)

Magento - Override adminhtml template file

I have read several posts on stack overflow
Overriding a Magento Adminhtml template file
Magento - overriding Adminhtml block
and a couple threads on the magento forum
http://www.magentocommerce.com/boards/viewthread/21978/
However, None of these posts attempt to do what I am trying to do
I would like to override the
app/design/adminhtml/default/default/template/widget/grid.phtml
file, as this file contains a portion of html that allows anyone to export from the sales->order view.
Note: We have disabled all of the export options for this user role in the permissions->role view
The code that displays the "Export to: " -> "CSV/Excel XML" feature is included in the path I have listed above. I would like to remove that chunk of html and override the file included with Magento.
Adminhtml uses the same theming fallback as the frontend, therefore you need only declare a custom template theme for your installation in module config XML:
<stores>
<admin>
<design>
<theme>
<template>custom</template>
</theme>
</design>
</admin>
</stores>
Then you can create app/design/adminhtml/default/custom/template/widget/grid.phtml with any customizations you like, and this file will be used in preference to the one from the default/default adminhtml theme. Your solution then would be to add an ACL check in the logic which renders the export control:
<?php if($this->getExportTypes() && {ACL LOGIC}}): ?>
<td class="export a-right">
<img src="<?php echo $this->getSkinUrl('images/icon_export.gif') ?>" alt="" class="v-middle"/> <?php echo $this->__('Export to:') ?>
<select name="<?php echo $this->getId() ?>_export" id="<?php echo $this->getId() ?>_export" style="width:8em;">
<?php foreach ($this->getExportTypes() as $_type): ?>
<option value="<?php echo $_type->getUrl() ?>"><?php echo $_type->getLabel() ?></option>
<?php endforeach; ?>
</select>
<?php echo $this->getExportButtonHtml() ?>
</td>
<?php endif; ?>
While this logic might be more appropriately implemented in the block class, the class rewrite system does not accommodate rewriting of parent classes, leaving you to rewrite every subclass. In this instance, obeying DRY outweighs embedding too much logic in templates. Moreover, the change is obvious and easily maintained.
Ideally the core team would have implemented this check in the Mage_Adminhtml_Block_Widget_Grid class or at least provided a public setter for the _exportTypes property, which would have made this logic a bit cleaner to implement.
It might seem the simplest solution to rewrite the block but that's more of a dirty hack than a clean solution. Class rewrites should be used very carefully and always avoided if possible. Otherwise you will quickly run into conflicts and also updating Magento gets a hell.
Usually you can change templates by a custom layout update (i.e. in your local.xml), but in this case it is a widget, which are not configured via layout XML.
So, enter observers: create a module that contains the following in its config.xml
<adminhtml>
<events>
<adminhtml_block_html_before>
<observers>
<yourmodulename_observer>
<class>yourmodulename/observer</class>
<method>changeWidgetTemplate</method>
</yourmodulename_observer>
</observers>
</adminhtml_block_html_before>
</events>
</adminhtml>
If you don't understand any of the above, read about Magento Events and Observers.
Now you will need the observer itself to actually change the template, but only for this block type:
class Your_Modulename_Observer
{
public function changeWidgetTemplate(Varien_Event_Observer $observer)
{
$block = $observer->getEvent()->getBlock();
if ($block instanceof Mage_Adminhtml_Block_Widget_Grid) {
// consider getting the template name from configuration
$template = '...';
$block->setTemplate($template);
}
}
}
Magento - Override adminhtml template file
add below code to config.xml file of extension (you created)
<stores>
<admin>
<design>
<theme>
<default>default</default>
<template>rwd</template>
</theme>
</design>
</admin>
</stores>
Now create rwd folder under adminhtml/default/rwd package.
and create template and layout file as you want to override.
like we want to override order comment history.phtml file.
<root>\app\design\adminhtml\default\default\template\sales\order\view\history.phtml
<root>\app\design\adminhtml\default\rwd\template\sales\order\view\history.phtml
Template definition can be found here
class Mage_Adminhtml_Block_Widget_Grid extends Mage_Adminhtml_Block_Widget
in
public function __construct($attributes=array())
So you need to rewrite sales grid block if you want to remove export csv from Sales Order Grid (use this guide if you don't know how http://www.magentocommerce.com/wiki/groups/174/changing_and_customizing_magento_code) and to change __construct to be like
public function __construct($attributes=array())
{
parent::__construct($attributes);
$this->setTemplate('...'); //here is your template
}

How can I have the cart sidebar only show up when it's in checkout?

I have modified the layout for checkout so the one-page checkout has a right-hand column and has the sidebar cart in it:
<checkout_onepage_index translate="label">
<label>One Page Checkout</label>
<!-- Mage_Checkout -->
<remove name="left"/>
<update handle="page_two_columns_right" />
<reference name="right">
<block type="checkout/cart_sidebar" name="checkout_cart_sidebar" template="checkout/cart/sidebar.phtml"/>
</reference>
<reference name="root">…snip
I would like the sidebar to appear in the checkout even if it's disabled in other pages via the admin. Basically I just need a boolean value to insert in my overridden sidebar.phtml:
<?php if ($_someBooleanValue || $this->getIsNeedToDisplaySideBar()):?>
What's the best way to set the value of $_someBooleanValue to true when the block is within the checkout process, and false otherwise?
I was able to solve this. What I really needed was getNameInLayout() from Mage_Core_Block_Abstract. Since I control the layout xml I'm dealing with, I know what the name will be (*checkout_cart_sidebar* in this case), so I just needed to check if that was the name of the current block.
<?php if ($this->getNameInLayout() === 'checkout_cart_sidebar'
|| $this->getIsNeedToDisplaySideBar()):?>…snip
The best way is to use the local.xml file in the /app/design/frontend/yourtheme/yourskin/layout folder. You can target specific pages to add/remove blocks from to override the base definitions.
Here's a good primer on the power of the local.xml file:
http://magebase.com/magento-tutorials/5-useful-tricks-for-your-magento-local-xml/
You could get the page name and if the page name equals the page you want it to show up on then show the sidebar.
<?php
$currentFile = $_SERVER["PHP_SELF"];
$parts = Explode('/', $currentFile);
$page = $parts[count($parts) - 1];
if($page == "checkoutpage.php")
{
$_someBooleanValue = true;
}
?>
You could check if "checkout" is in the page name also.
<?php
$position = strpos($page, "checkout");
if($position == true){$_someBooleanValue = true;};
?>
However this is just a suggestion, but it may not be the best solution.

Display Tiered Pricing on the Cart page

If Im on this page:
http:///checkout/cart/
With products in my cart I would like to display the tiered pricing, the same that is shown on the item page, if available.
My attempt was add
<checkout_cart_index>
<block type="catalog/product_view" name="product.tierprices" as="tierprices" template="catalog/product/view/tierprices.phtml"/>
</checkout_cart_index>
to my xml file and add
<?php echo $this->getChildHtml('tierprices') ?>
to
\app\design\frontend\enterprise\<mytemplate>\template\checkout\cart\item\default.phtml
Doesn’t do anything - any further suggestions?
It seems impossible to easily change layout. You need to modify item renderer and add tier price displaying manually. To fetch list of available tier prices you need to get price model. You can get it from product model
$product->getPriceModel()
or if don't have product model try the following code
Mage::getSingleton('catalog/product_type')->priceFactory($productTypeId)
Quote item contains product type information.
When you have price model just call method getTierPrice() to get all tier prices as array.
$priceModel->getTierPrice()
You could edit the .phtml file and adding the $this->getTierPrices($_product);//or$this->getTierPrices($_item); if you simply want to display the tier prices of products.
Do note that the getTierPrices() only works when being on the product list or product view page, so you would need to copy the getTierPrices() method that can be found inside the List.php to your custom module.
This should give you an idea what needs to be done.
layout file
<?xml version="1.0"?>
<layout version="0.1.0">
<checkout_cart_index>
<reference name="additional.product.info">
<block type="LokeyCoding_Cart/TierPrice" name="additional.product.info.tierprice" />
</reference>
</checkout_cart_index>
</version>
block file
<?php
class LokeyCoding_Cart_Block_TierPrice extends Mage_Core_Block_Abstract
{
protected function _toHtml()
{
$parent = $this->getParentBlock();
if ($parent) {
$item = $parent->getItem();
if ($item instanceof Mage_Sales_Model_Quote_Item) {
return $item->getProduct()->getTierPriceHtml();
}
}
return '';
}
}

Add css from a block

I am building a custom magento module and i try to add a custom css file to my block. I wrote :
<?php
class Wise_InteractiveSlider_Block_Slider extends Mage_Core_Block_Template
{
protected function _prepareLayout()
{
$this->getLayout()->getBlock('head')->addCss('css/mycompany/mymodule/stylesheet.css');
return parent::_prepareLayout();
}
}
but it doesn't work, my css file is not loaded, any idea?
Thank you.
My alternative solution was to add this in my xml layout :
<default>
<reference name="head">
<action method="addCss"><stylesheet>css/interactiveslider.css</stylesheet></action>
</reference>
</default>
Thank you for your help
All the CSS & Images are normally available in the "skin" folder. It should be:-
"skin" folder
-> Package Name (like "base" or "default")
-> Theme Name (like "modern" or "mycompany")
-> "css" folder
-> "mymodule" folder
-> "stylesheet.css" file
So I suppose that you have been following this above-mentioned basic structure, which is considered as one of the best practices.
Coming back to your question, I suppose that you have mentioned the correct block class in your module's layout file "layout.xml". So the above code should be, according to the above folder structure:-
<?php
class Wise_InteractiveSlider_Block_Slider extends Mage_Core_Block_Template
{
protected function _prepareLayout()
{
$this->getLayout()->getBlock('head')->addCss('css/mymodule/stylesheet.css');
return parent::_prepareLayout();
}
}
Lastly, please make sure that you have uploaded your CSS file "stylesheet.css" in the correct folder.
Hope it helps.
You can only use the _prepareLayout() method if the block is defined in the layout XML. If you 'inline' the block inside a CMS page via the {{block type... method, the layout is already prepared by the time the block is rendered

Resources