what is the difference between the functions getAddUrl and getAddToCartUrl? - magento

I have found two function returning the exactly same value.
getAddUrl in \app\code\local\Mage\Checkout\Helper\Cart.php
getAddToCartUrl in \app\code\local\Mage\Catalog\Block\Product\Abstract.php
what is really the difference between these two?as I am adding an add to cart button in my block.
Which function should I use?

Consider the example below
<?php if ($this->helper('wishlist')->isAllow()) : ?>
<li><?php echo $this->__('Add to Wishlist') ?></li>
<?php endif; ?>
<?php if($_cartUrl=$this->getAddToCartUrl($_product)): ?>
<li><span class="separator">|</span> <?php echo $this->__('Add to Cart') ?></li>
<?php endif; ?>
getAddUrl can fetch you URL of the helper you associate it with, eg: wishlist, compare, etc.
but getAddToCartUrl will only get you the add to cart URL for the product passed to it.
I hope it clarifies things for you

Does your block inherit the Mage_Catalog_Block_Product_Abstract class? If so you should use the getAddToCartUrl method of the block.
Otherwise you should use the helper, there is no neat way of calling a block method without creating the block in this case a product block.

The difference between these two functions is that the getAddToCartUrl (Mage_Catalog_Block_Product_Abstract) will return product view page URL if product has required options.
And getAddUrl (Mage_Checkout_Helper_Cart) will always return url for add product to cart.

Related

Magento - Unable to display custom options

I'm using a module for displaying grouped configurable products and all options are showing except custom options. They are displaying on the configurable product page but that's it. I'm trying to use the code in app\design\frontend\blank\blank\template\catalog\product\view\options.phtml in my custom configurable.phtml but $_options is showing up null. Here is the code used to retrieve $_options
<?php $_options = Mage::helper('core')->decorateArray($this->getOptions()) ?>
<?php if (count($_options)):?>
and after the javascript
<?php foreach($_options as $_option): ?>
<?php echo $this->getOptionHtml($_option) ?>
<?php endforeach; ?>
</dl>
<?php else: echo dlkghflghf;?>
<?php endif; ?>
The dlkghflghf is displaying so i know $_options is not showing up. Any suggestions?
Your custom configurable.phtml is a template of what block? You can use a block that extends Mage_Catalog_Block_Product_View_Options or you can set your own method for options like the method from Mage_Catalog_Block_Product_View_Options, you also need to have a method getProduct():
public function getOptions()
{
return $this->getProduct()->getOptions();
}

How to show the original price in the shopping cart in Magento

I'm having problems trying to show the original unit price in the shopping cart in Magento. By default Magento only shows the special price but I would like to also show the original price to highlight the savings.
I know which template needs changing which is /template/checkout/cart/item/default.phtml.
On line 145 it shows
<?php
echo $this->helper('checkout')->
formatPrice($_incl-$_item->getWeeeTaxDisposition())
?>
which displays the special price. I just don't know the syntax to display the original price.
see below for an alternative method,
i'm not sure if $_item->getPrice has access to the non special price, so jumped it over to product
<?php echo $_item->getProduct()->getPrice(); ?>
within
<?php foreach($this->getItems() as $_item): ?>
<?php echo $this->getItemHtml($_item) ?>
// insert it here
<?php endforeach ?>

Setting model as template data (attribute) for child block

In my product view template i'm loading child template, and transfering product instance to be available in this child template:
<?php
echo $this->getLayout()
->createBlock('core/template')
->setTemplate('catalog/product/view/addedToCartDialog.phtml')
->setAttribute('product', $_product)
->toHtml();
?>
Then in my catalog/product/view/addedToCartDialog.phtml i'm trying to use this product instance:
<?php $product = $this->getData('product'); ?>
<?php echo"<pre>";print_r($product->getId());echo"</pre>"; ?>
However it seems to be not loaded: Fatal error: Call to a member function getId() on a non-object in /home/ryba/workspace/polcode/Greenlights/app/design/frontend/default/greenlights/template/catalog/product/view/addedToCartDialog.phtml on line 2
But when i check variable $product with print_r:
<?php echo"<pre>";print_r($product);echo"</pre>"; ?>
It is displayed that this variable is correct Mage_Catalog_Model_Product Object, also checked if attributes is correct (like sku, name etc) - everything is correct.
What is wrong with this ?
I'm going to give you several answers. The first is the direct answer to your question. The rest are alternatives, but better ways to do what you're trying. The last answer is, in my opinion, the best.
Direct Answer:
Instead of using setAttribute, just use the magic setter/getter methods:
<?php
// In catalog/product/view.phtml
echo $this->getLayout()
->createBlock('core/template')
->setTemplate('catalog/product/view/addedToCartDialog.phtml')
->setProduct($_product)
->toHtml();
?>
<?php
// In addedToCartDialog.phtml
$_product = $this->getProduct();
echo $_product->getId();
?>
Better:
And, if you know you are in a template loaded by the catalog/product controller, you can get the product this way.
<?php
// In catalog/product/view.phtml
echo $this->getLayout()
->createBlock('core/template')
->setTemplate('catalog/product/view/addedToCartDialog.phtml')
->toHtml();
?>
<?php
// In addedToCartDialog.phtml
$_product = Mage::registry('product');
echo $_product->getId();
?>
Even Better
The best way would be to use a different block type which has the methods already loaded (again, if you know you are in a template loaded by the catalog/product controller)
<?php
// In catalog/product/view.phtml
echo $this->getLayout()
->createBlock('catalog/product_view')
->setTemplate('catalog/product/view/addedToCartDialog.phtml')
->toHtml();
?>
<?php
// In addedToCartDialog.phtml
$_product = $this->getProduct();
echo $_product->getId();
?>
And Finally, the Best
One last item of business. The better way to add extra blocks to your templates is to add the block in your local.xml file.
<!-- Local.xml -->
<catalog_product_view translate="label">
<reference name="content">
<block type="catalog/product_view" name="addedToCartDialog" as="addedToCartDialog" template="catalog/product/view/addedToCartDialog.phtml" />
</reference>
</catalog_product_view>
Now, set up your phtml file
<?php
// In addedToCartDialog.phtml
$_product = $this->getProduct();
echo $_product->getId();
?>
Then call the block from your phtml file
// In catalog/product/view.phtml
<?php echo $this->getChildHtml('addedToCartDialog'); ?>

Best way for creating dynamic sidebar section in Symfony 1.4

I have few frontend modules which has own sidebar menu link. I want to create those links in actions class of module:
public function preExecute()
{
$items['plan/new'] = 'Create Plan';
$items['plan/index'] = 'Plans Listing';
$this->getResponse()->setSlot('sidebar', $items);
}
Slot file sidebar.php
#apps/frontend/templates/sidebar.php
<?php slot('sidebar') ?>
<ul>
<?php foreach($items as $url => $title) : ?>
<li><?php echo link_to($url, $title) ?></li>
<?php endforeach ?>
</ul>
<?php end_slot() ?>
layout.php:
<?php if (has_slot('sidebar')): ?>
<div id="sidebar"><?php include_slot('sidebar') ?></div>
<?php endif ?>
but my output is Array, how can I render my slot?
You seem to be mixing slots and partials. In your action, you set your slot to an array, later you call include_slot, and the string representation is Array, that is correct.
You should pass items via $this->items = $items, then in your action see if isset($items) is true, and call include_partial("sidebar", array("items" => $items)) if neccesary. This will look for a file called _sidebar.php.
For more detailed information of how this stuff works, read the Inside the View Layer: Code fragments part of the sf1.4 book.

Magento: Catalog Page 1 different from other pages

I want to setup the category catalog pages in Magento such that the first page contains the category image and the first three products in that category. Then the following pages contain six products per page without the category image.
I can't figure out how this can be done.
Unfortunately I don't think you can do this without a lot of work. You'll need to rewrite the logic of the pagination, change the page size depending on which page it is, & offset the returned collection.
However you can easily only have the category image displayed on the first page.
This line returns the current page number:
Mage::getBlockSingleton('page/html_pager')->getCurrentPage();
So in template/catalog/category/view.phtml you can just do a conditional around the category image display, find the section:
<?php if($_imgUrl): ?>
<?php echo $_imgHtml ?>
<?php endif; ?>
and replace it with:
<?php if($_imgUrl): ?>
<?php if(Mage::getBlockSingleton('page/html_pager')->getCurrentPage()==1):?>
<?php echo $_imgHtml ?>
<?php endif; ?>
<?php endif; ?>

Resources