How to get bigger image in magento? - php

I am using magento rwd theme with a extension - for selected product widget.
The problem I am not able to increase the size of the image.
the relevant code is like this:
<?php
$products = $this->getProductCollection();?>
<?php if ($products && $products->getSize()): ?>
<div class="block block-products selected-products">
<div class="block-title">
<strong><span><?php echo $this->getBlockName() ?></span></strong>
</div>
<div class="block-content">
<div class="products-block">
<?php foreach ($products->getItems() as $product): ?>
<?php /** #var $product Mage_Catalog_Model_Product */ ?>
<div class="item">
<a class="product-image"
href="<?php echo $product->getProductUrl() ?>"
title="<?php echo $this->stripTags($product->getName(), null, true) ?>">
<img
src="<?php echo $this->helper('catalog/image')->init($product, 'image')->resize(300,400) ?>"
alt="<?php echo $this->stripTags($product->getName(), null, true) ?>" />
</a>
<div class="product-details">
<p class="product-name">
<a href="<?php echo $product->getProductUrl() ?>"
title="<?php echo $this->stripTags($product->getName(), null, true) ?>)">
<?php echo $this->helper('catalog/output')->productAttribute($product, $product->getName(), 'name') ?>
</a>
</p>
<?php if ($product->getIsSalable()): ?>
<div class="product-price">
<?php echo $this->getPriceHtml($product, true) ?>
</div>
<button type="button"
title="<?php echo $this->__('Add to Cart') ?>"
class="button btn-cart"
onclick="setLocation('<?php echo $this->getAddToCartUrl($product) ?>')">
<span><span><?php echo $this->__('Add to Cart') ?></span></span>
</button>
<?php endif; ?>
</div>
</div>
<?php endforeach; ?>
</div>
</div>
</div>
I have set the resize to 300x400, but I am getting it - 118x157
The website link for reference -
Change your Belief for Success

You have to set constrainOnly to true on the image helper before resizing:
$this->helper('catalog/image')
->init($product, 'image')
->constrainOnly(true)
->resize(300,400);
From the PHPDoc of the method:
Guarantee, that image picture will not be bigger, than it was.
Applicable before calling resize()
It is false by default

Related

Display random products on product page of magento every time i refresh the page

I want to Display random products on product page of magento every time i refresh the page.
I have tried finding the answer on google and some of the solutions were to change my list.phtml code.
i have tried replacing
$_productCollection=$this->getLoadedProductCollection();
with
$_productCollection=shuffle($this->getLoadedProductCollection());
or adding
$items = $_productCollection->getitems;
$item = shuffle($items);
but its not working, Even if shuffle() would work on collections (it doesn't), this would only show the same products in a different order. i want my products which are on the later page to come forward.
Here is my list.phtml code:
<?php
$_productCollection=$this->getLoadedProductCollection();
$_helper = $this->helper('catalog/output');
$gallery_helper = $this->helper('productlistgallery/data');
// Chech if Tm_SpecialPriceCountdown module is installed
if (Mage::helper('core')->isModuleEnabled('Tm_SpecialPriceCountdown')) {
$spcd_helper = $this->helper('specialpricecountdown/data');
$currDate = $spcd_helper->currDate();
}
?>
<?php if(!$_productCollection->count()): ?>
<p class="note-msg"><?php echo $this->__('There are no products matching the selection.') ?></p>
<?php else: ?>
<div class="category-products">
<?php echo $this->getToolbarHtml() ?>
<?php // List mode ?>
<?php if($this->getMode()!='grid'): ?>
<?php $_iterator = 0; ?>
<ul class="products-list" id="products-list">
<?php foreach ($_productCollection as $_product): ?>
<li class="item<?php if( ++$_iterator == sizeof($_productCollection) ): ?> last<?php endif; ?>" itemscope itemtype="http://schema.org/product">
<?php // Product Image ?>
<div class="product-image-container">
<a href="<?php echo $_product->getProductUrl() ?>" title="<?php echo $this->stripTags($this->getImageLabel($_product, 'small_image'), null, true) ?>" class="product-image" itemprop="url" style="width:<?php echo $gallery_helper->getListData('image_width') + 20; ?>px;">
<img id="product-collection-image-<?php echo $_product->getId(); ?>" src="<?php echo $this->helper('catalog/image')->init($_product, 'small_image')->resize($gallery_helper->getListData('image_width'), $gallery_helper->getListData('image_height')); ?>" width="<?php echo $gallery_helper->getListData('image_width'); ?>" height="<?php echo $gallery_helper->getListData('image_height'); ?>" alt="<?php echo $this->stripTags($this->getImageLabel($_product, 'small_image'), null, true) ?>" itemprop="image" />
</a>
<?php
if($gallery_helper->getListData('active')):
?>
<ul class="product-thumbs">
<?php
$count_thumbs = 1;
foreach ($_product->getMediaGalleryImages() as $image):
if($count_thumbs <= 3):
?>
<li class="product-thumb">
<a href="<?php echo $this->helper('catalog/image')->init($_product, 'small_image', $image->getFile())->resize($gallery_helper->getListData('image_width')); ?>">
<img src="<?php echo $this->helper('catalog/image')->init($_product, 'thumbnail', $image->getFile())->resize($gallery_helper->getListData('thumb_size_w'), $gallery_helper->getListData('thumb_size_h')); ?>" width="<?php echo $gallery_helper->getListData('thumb_size_w'); ?>" height="<?php echo $gallery_helper->getListData('thumb_size_h'); ?>" alt="" />
</a>
</li>
<?php endif ?>
<?php $count_thumbs++; ?>
<?php endforeach ?>
</ul>
<?php endif ?>
</div>
<?php // Product description ?>
<div class="product-shop">
<div class="f-fix">
<div class="product-primary">
<?php $_productNameStripped = $this->stripTags($_product->getName(), null, true); ?>
<h2 class="product-name" itemprop="name"><?php echo $_helper->productAttribute($_product, $_product->getName() , 'name'); ?></h2>
<?php if($_product->getRatingSummary()): ?>
<?php echo $this->getReviewsSummaryHtml($_product) ?>
<?php endif; ?>
<?php
// Provides extra blocks on which to hang some features for products in the list
// Features providing UI elements targeting this block will display directly below the product name
if ($this->getChild('name.after')) {
$_nameAfterChildren = $this->getChild('name.after')->getSortedChildren();
foreach ($_nameAfterChildren as $_nameAfterChildName) {
$_nameAfterChild = $this->getChild('name.after')->getChild($_nameAfterChildName);
$_nameAfterChild->setProduct($_product);
echo $_nameAfterChild->toHtml();
}
}
?>
<div class="desc std">
<div itemprop="description"><?php echo $_helper->productAttribute($_product, $_product->getShortDescription(), 'short_description') ?></div>
<?php echo $this->__('Learn More') ?>
</div>
</div>
<div class="product-secondary">
<?php echo $this->getPriceHtml($_product, true) ?>
<?php if(!$_product->canConfigure() && $_product->isSaleable()): ?>
<p class="actions">
<button type="button" title="<?php echo $this->__('Add to Cart') ?>" class="button btn-cart" onclick="setLocation('<?php echo $this->getAddToCartUrl($_product) ?>')"><span><span><?php echo $this->__('Add to Cart') ?></span></span></button>
</p>
<?php elseif($_product->getStockItem() && $_product->getStockItem()->getIsInStock()): ?>
<p class="actions"><a title="<?php echo $this->__('View Details') ?>" class="button btn-details" href="<?php echo $_product->getProductUrl() ?>"><span><span><?php echo $this->__('View Details') ?></span></span></a></p>
<?php else: ?>
<p class="actions availability out-of-stock"><span><?php echo $this->__('Out of stock') ?></span></p>
<?php endif; ?>
<ul class="add-to-links">
<?php if ($this->helper('wishlist')->isAllow()) : ?>
<li><a title="<?php echo $this->__('Add to Wishlist') ?>" href="<?php echo $this->helper('wishlist')->getAddUrl($_product) ?>" class="link-wishlist tooltips"><?php echo $this->__('Add to Wishlist') ?></a></li>
<?php endif; ?>
<?php if($_compareUrl=$this->getAddToCompareUrl($_product)): ?>
<li><span class="separator">|</span> <a title="<?php echo $this->__('Add to Compare') ?>" href="<?php echo $_compareUrl ?>" class="link-compare tooltips"><?php echo $this->__('Add to Compare') ?></a></li>
<?php endif; ?>
</ul>
</div>
<div class="clear"></div>
</div>
</div>
<div class="label-product">
<?php if($_product->getNew()){echo '<span class="new">'.$this->__('New').'</span>'; }?>
<?php if($_product->getSale()){echo '<span class="sale">'.$this->__('Sale').'</span>'; }?>
</div>
</li>
<?php endforeach; ?>
</ul>
<script type="text/javascript">decorateList('products-list', 'none-recursive')</script>
<?php else: ?>
<?php // Grid Mode ?>
<?php $_collectionSize = $_productCollection->count() ?>
<?php //$_columnCount = $this->getColumnCount(); ?>
<?php $_columnCount = 3; ?>
<?php $i=0; foreach ($_productCollection as $_product): ?>
<?php
$_productId = $_product->getId();
if ($i++%$_columnCount==0): ?>
<ul class="products-grid row">
<?php endif ?>
<li class="item<?php if(($i-1)%$_columnCount==0): ?> first<?php elseif($i%$_columnCount==0): ?> last<?php endif; ?> col-xs-12 col-sm-4" itemscope itemtype="http://schema.org/product">
<div class="wrapper-hover">
<div class="product-image-container">
<?php if ($gallery_helper->getGridData('type_hover') == 'carusel_images'): ?>
<?php include("include_gallery.phtml"); ?>
<?php else: ?>
<a href="<?php echo $_product->getProductUrl() ?>" title="<?php echo $this->stripTags($this->getImageLabel($_product, 'small_image'), null, true) ?>" class="product-image" itemprop="url">
<img id="product-collection-image-<?php echo $_productId; ?>" src="<?php echo $this->helper('catalog/image')->init($_product, 'small_image')->resize($gallery_helper->getGridData('image_width'), $gallery_helper->getGridData('image_height')); ?>" alt="<?php echo $this->stripTags($this->getImageLabel($_product, 'small_image'), null, true) ?>" width="<?php echo $gallery_helper->getGridData('image_width'); ?>" height="<?php echo $gallery_helper->getGridData('image_height'); ?>" itemprop="image" />
</a>
<?php endif ?>
<?php if($gallery_helper->getGridData('type_hover') == 'thumbnails'): ?>
<ul class="product-thumbs">
<?php $count_thumbs = 1; foreach ($_product->getMediaGalleryImages() as $image): ?>
<?php if($count_thumbs <= 3): ?>
<li class="product-thumb">
<a href="<?php echo $this->helper('catalog/image')->init($_product, 'small_image', $image->getFile())->resize($gallery_helper->getGridData('image_width')); ?>">
<img src="<?php echo $this->helper('catalog/image')->init($_product, 'thumbnail', $image->getFile())->resize($gallery_helper->getGridData('thumb_size_w'), $gallery_helper->getGridData('thumb_size_h')); ?>" width="<?php echo $gallery_helper->getGridData('thumb_size_w'); ?>" height="<?php echo $gallery_helper->getGridData('thumb_size_h'); ?>" alt="" />
</a>
</li>
<?php endif; ?>
<?php $count_thumbs++; ?>
<?php endforeach ?>
</ul>
<?php endif ?>
</div>
<div class="product-details">
<h2 class="product-name" itemprop="name"><?php echo $_helper->productAttribute($_product, $_product->getName(), 'name') ?></h2>
<h3 style="font-size:14px;text-transform:capitalize;"><?php echo $this->htmlEscape($_product->getData('artistname')); ?></h3>
<?php echo $this->getPriceHtml($_product, true) ?>
<?php if(isset($spcd_helper) && $spcd_helper->isEnabledGridList()): ?>
<?php
$setDate = $spcd_helper->countdownTime($_product);
if ($setDate > 0):
if ($currDate < $setDate): ?>
<div id="special_countdown_<?php echo $_product->getId(); ?>" class="special_countdown"></div>
<script type="text/javascript">
setCountdown("<?php echo $setDate; ?>", "<?php echo $_product->getId(); ?>");
</script>
<?php endif; ?>
<?php endif; ?>
<?php endif; ?>
<?php
// Provides extra bloc->keepFrame(false)s on which to hang some features for products in the list
// Features providing UI elements targeting this block will display directly below the product name
if ($this->getChild('name.after')) {
$_nameAfterChildren = $this->getChild('name.after')->getSortedChildren();
foreach ($_nameAfterChildren as $_nameAfterChildName) {
$_nameAfterChild = $this->getChild('name.after')->getChild($_nameAfterChildName);
$_nameAfterChild->setProduct($_product);
echo $_nameAfterChild->toHtml();
}
}
?>
<div class="wrapper-hover-hiden">
<?php if($_product->getRatingSummary()): ?>
<?php echo $this->getReviewsSummaryHtml($_product, 'short') ?>
<?php endif; ?>
<div class="actions">
<?php if(!$_product->canConfigure() && $_product->isSaleable()): ?>
<button type="button" title="<?php echo $this->__('Add to Cart') ?>" class="button btn-cart" onclick="setLocation('<?php echo $this->getAddToCartUrl($_product) ?>')"><span><span><?php echo $this->__('Add to Cart') ?></span></span></button>
<?php elseif($_product->getStockItem() && $_product->getStockItem()->getIsInStock()): ?>
<a title="<?php echo $this->__('View Details') ?>" class="button btn-details" href="<?php echo $_product->getProductUrl() ?>"><span><span><?php echo $this->__('View Details') ?></span></span></a>
<?php else: ?>
<p class="availability out-of-stock"><span><?php echo $this->__('Out of stock') ?></span></p>
<?php endif; ?>
<ul class="add-to-links">
<?php if ($this->helper('wishlist')->isAllow()) : ?>
<li><?php echo $this->__('Add to Wishlist') ?></li>
<?php endif; ?>
<?php if($_compareUrl=$this->getAddToCompareUrl($_product)): ?>
<?php endif; ?>
</ul>
</div>
</div>
</div>
<div class="label-product">
<?php if($_product->getNew()){echo '<span class="new">'.$this->__('New').'</span>'; }?>
<?php if($_product->getSale()){echo '<span class="sale">'.$this->__('Sale').'</span>'; }?>
</div>
</div>
</li>
<?php if ($i%$_columnCount==0 || $i==$_collectionSize): ?>
</ul>
<?php endif ?>
<?php endforeach ?>
<script type="text/javascript">decorateGeneric($$('ul.products-grid'), ['odd','even','first','last'])</script>
<?php endif; ?>
<div class="toolbar-bottom">
<?php echo $this->getToolbarHtml() ?>
</div>
</div>
<?php endif; ?>
<?php
// Provides a block where additional page components may be attached, primarily good for in-page JavaScript
if ($this->getChild('after')) {
$_afterChildren = $this->getChild('after')->getSortedChildren();
foreach ($_afterChildren as $_afterChildName) {
$_afterChild = $this->getChild('after')->getChild($_afterChildName);
//set product collection on after blocks
$_afterChild->setProductCollection($_productCollection);
echo $_afterChild->toHtml();
}
}
?>
First Override your list.phtml file in your custom theme.
Add this code in your list.phtml file.
$productArray = iterator_to_array($_productCollection);
shuffle($productArray);
and add this $productArray in foreach loop.
<?php foreach ($productArray as $_product): ?>
cacheable="false" in your catalog_category_view.xml file.
<block class="Magento\Catalog\Block\Product\ListProduct"
name="category.products.list"
as="product_list"
template="Magento_Catalog::product/list.phtml"
cacheable="false"
>
The collection is an iterator and not an array, array functions do not work with iterators. but you can convert it.
Try this:-
$productCollection=$this->getLoadedProductCollection();
$productArray = iterator_to_array($productCollection);
$items = shuffle($productArray);

Shortcode is moving the_content outside of the article container in WordPress

I've made a shortcode function that returns some Advanced Custom Fields depending on which repeater field is chosen. It displays correctly and in the right order however any content typed underneath the shortcode is moved outside of its <article> container element.
Here is the code:
function boxes_shortcode($atts, $content = null) {
ob_start(); ?>
<div id="boxes">
<div class="box-gutter"></div>
<div class="box-sizer"></div>
<?php while( have_rows('box_repeater') ): the_row();
$image_id = get_sub_field('link_image');
$image_size = 'box-thumbnail';
$image_array = wp_get_attachment_image_src($image_id, $image_size);
$linkImage = $image_array[0]; ?>
<?php if(get_sub_field('box_type') == "box-link"): ?>
<div class="box">
<div class="link-box">
<img src="<?php echo $linkImage; ?>" alt="<?php echo $linkImage['alt']; ?>" />
<a class="caption-wrapper" href="http://www.google.com">
<span id="link-box-content" style="background:<?php the_sub_field('link_background_colour'); ?>">
<h6 style="color:<?php the_sub_field('link_title_colour'); ?>"><?php the_sub_field('link_title'); ?></h6>
<h4 style="color:<?php the_sub_field('link_text_colour'); ?>"><?php the_sub_field('link_text'); ?></h4>
<p style="color:<?php the_sub_field('link_text_colour'); ?>" href="<?php the_sub_field('link_url'); ?>"><?php the_sub_field('link_url_text'); ?> ></p>
</span>
</a>
</div>
</div>
<?php endif;
if(get_sub_field('box_type') == "box-quote"): ?>
<div class="box">
<div class="quote-box">
<div class="quotation-mark"></div>
<h4><?php the_sub_field('quote'); ?></h2>
<p><?php the_sub_field('quote_source'); ?></p>
<?php the_sub_field('quote_link_text'); ?> >
</div>
</div>
<?php endif;
if(get_sub_field('box_type') == "box-twitter"): ?>
<div class="box">
<div class="twitter">
<a class="twitter-timeline" href="<?php the_sub_field('twitter_url'); ?>" data-widget-id="<?php the_sub_field('twitter_widget_id'); ?>" data-chrome="noheader noscrollbar nofooter noborders transparent" data-tweet-limit="<?php the_sub_field('number_of_tweets'); ?>"></a>
<script>!function(d,s,id){var js,fjs=d.getElementsByTagName(s)[0],p=/^http:/.test(d.location)?'http':'https';if(!d.getElementById(id)){js=d.createElement(s);js.id=id;js.src=p+"://platform.twitter.com/widgets.js";fjs.parentNode.insertBefore(js,fjs);}}(document,"script","twitter-wjs");</script>
<a class="twitter-badge" href="<?php the_sub_field('twitter_url'); ?>"></a>
<div class="twitter-bottom">See more ></div>
</div>
</div>
<?php endif;
endwhile;?>
</div>
</div>
</div>
<?php
/* Get the buffered content into a var */
$sc = ob_get_contents();
ob_end_clean();
return $sc;
}
add_shortcode('boxes', 'boxes_shortcode');
Can anyone help solve this mystery? Many thanks.
You have two extra closing <div>'s in your code.
Try this:
function boxes_shortcode($atts, $content = null) {
ob_start(); ?>
<div id="boxes">
<div class="box-gutter"></div>
<div class="box-sizer"></div>
<?php while( have_rows('box_repeater') ): the_row();
$image_id = get_sub_field('link_image');
$image_size = 'box-thumbnail';
$image_array = wp_get_attachment_image_src($image_id, $image_size);
$linkImage = $image_array[0]; ?>
<?php if(get_sub_field('box_type') == "box-link"): ?>
<div class="box">
<div class="link-box">
<img src="<?php echo $linkImage; ?>" alt="<?php echo $linkImage['alt']; ?>" />
<a class="caption-wrapper" href="http://www.google.com">
<span id="link-box-content" style="background:<?php the_sub_field('link_background_colour'); ?>">
<h6 style="color:<?php the_sub_field('link_title_colour'); ?>"><?php the_sub_field('link_title'); ?></h6>
<h4 style="color:<?php the_sub_field('link_text_colour'); ?>"><?php the_sub_field('link_text'); ?></h4>
<p style="color:<?php the_sub_field('link_text_colour'); ?>" href="<?php the_sub_field('link_url'); ?>"><?php the_sub_field('link_url_text'); ?> ></p>
</span>
</a>
</div>
</div>
<?php endif;
if(get_sub_field('box_type') == "box-quote"): ?>
<div class="box">
<div class="quote-box">
<div class="quotation-mark"></div>
<h4><?php the_sub_field('quote'); ?></h2>
<p><?php the_sub_field('quote_source'); ?></p>
<?php the_sub_field('quote_link_text'); ?> >
</div>
</div>
<?php endif;
if(get_sub_field('box_type') == "box-twitter"): ?>
<div class="box">
<div class="twitter">
<a class="twitter-timeline" href="<?php the_sub_field('twitter_url'); ?>" data-widget-id="<?php the_sub_field('twitter_widget_id'); ?>" data-chrome="noheader noscrollbar nofooter noborders transparent" data-tweet-limit="<?php the_sub_field('number_of_tweets'); ?>"></a>
<script>!function(d,s,id){var js,fjs=d.getElementsByTagName(s)[0],p=/^http:/.test(d.location)?'http':'https';if(!d.getElementById(id)){js=d.createElement(s);js.id=id;js.src=p+"://platform.twitter.com/widgets.js";fjs.parentNode.insertBefore(js,fjs);}}(document,"script","twitter-wjs");</script>
<a class="twitter-badge" href="<?php the_sub_field('twitter_url'); ?>"></a>
<div class="twitter-bottom">See more ></div>
</div>
</div>
<?php endif;
endwhile;?>
</div>
<!--/div>
</div-->
<?php
/* Get the buffered content into a var */
$sc = ob_get_contents();
ob_end_clean();
return $sc;
}
add_shortcode('boxes', 'boxes_shortcode');

Link to a CMS product page in Magento

I am looking for some advice on how best to link to a CMS product page from a template phtml file. I need to generate a link using 'foreach' as each product will have its own CMS product page. For example I have six products currently being displayed on the homepage I would like to have an 'order now' button that will take the user to the 'add to cart' page (view.phtml) and a 'more info?' button that will take the user to the CMS page for that product... is there a way to achieve this?
Here's the code from new.phtml
<div class="new-product-content">
<?php if (($_products = $this->getProductCollection()) && $_products->getSize()): ?>
<h3 class="subtitle new-products"><?php echo $this->__('HOT PRODUCTS') ?></h3>
<?php $_columnCount = $this->getColumnCount(); ?>
<?php $i=0; foreach ($_products->getItems() as $_product): ?>
<?php if ($i++%$_columnCount==0): ?>
<ul>
<?php endif ?>
<li class="thumb<?php if(($i-1)%$_columnCount==0): ?> first<?php elseif($i%$_columnCount==0): ?> last<?php endif; ?>">
<a href="<?php echo $_product->getProductUrl() ?>" title="<?php echo $this->htmlEscape($_product->getName()) ?>" ><img src="<?php echo $this->helper('catalog/image')->init($_product, 'small_image')->resize(175) ?>" width="175" height="175" alt="<?php echo $this->htmlEscape($_product->getName()) ?>" />
</a>
<div class="caption">
<h4 class="product-name"> <?php $_productName = $this->helper('core/string')->truncate($this->htmlEscape($_product->getName()),20,'...', $_remainder, true); ?>
<a href="<?php echo $_product->getProductUrl() ?>" title="<?php echo $_productName ?>">
<?php echo $_productName ?><?php echo $this->__('™'); ?></a></h4>
<?php echo $this->getPriceHtml($_product, true, '-new') ?>
<div class="clearfix"></div>
<div class="desc">
<?php $sdesc = $_product->getShortDescription();
$sdesc = trim($sdesc);
$limit = 180;
if (strlen($sdesc) > $limit) {
$sdesc = substr($sdesc, 0, strrpos(substr($sdesc, 0, $limit), ' '));
} ?>
<?php echo $sdesc."..."; ?>
</div>
<div class="clearfix"></div>
<div class="btn-group">
<button class="btn" type="button" title="<?php echo $this->__('Add to Cart') ?>" onclick="setLocation('<?php echo $this->getProductUrl($_product) ?>')"><?php echo $this->__('Order now...') ?></button>
<!--I NEED TO CHANGE THE DESTINATION OF THIS LINK <button class="btn" type="button" title="<?php echo $this->__('More info') ?>" onclick="setLocation('<?php echo $this->getProductUrl($_product) ?>')"><?php echo $this->__('More info?') ?></button> --->
</div>
</li>
<?php if ($i%$_columnCount==0 || $i==count($_products)): ?>
</ul>
<div class="clearfix"></div>
<?php endif ?>
<script type="text/javascript">
$j('.thumb li').last().css('border-right', 'none');
</script>
<script type="text/javascript">decorateGeneric($$('ul.products-grid'), ['odd','even','first','last'])</script>
<?php endforeach; ?>
<?php endif; ?>
<div class="clearfix"></div>
</div>
Try something like, you need to get the 'url key' for the cms pages
<button class="btn" type="button" title="<?php echo $this->__('More info') ?>" onclick="setLocation('<?php echo $this->getUrl('url key'); ?>')"><?php echo $this->__('More info?') ?></button>

add products to cart from cms page magento

I have a relatively small problem but I cannot find the solution.
I have a static CMS page which shows a list of products from a category in my Magento store.
I would like to include an add to cart button and a quantity box as well so users can add each product to their shopping cart.
The problem is I have included the add to cart button but when clicked it doesn't add a product to cart. I've read that I need to include the product id into the string but I am displaying more than one product from a category dynamically using a foreach loop....
Is there a workaround or solution to my problem? Any ideas will be welcomed.
Here's the code:
<?php
$category_id = "49"; // category_id for "Accessories"
$_productCollection = Mage::getResourceModel('catalog/product_collection')
->addAttributeToSelect(array('name', 'price', 'small_image', 'short_description'), 'inner')
->addCategoryFilter(Mage::getModel('catalog/category')->load($category_id));
?>
<?php if($_productCollection->count()): ?>
<ul class="media-list pull-left">
<?php
$products = array();
foreach ($_productCollection as $_product) {
?>
<li class="media">
<a class="fancybox pull-left" href="<?php echo $this->helper('catalog/image')->init($_product, 'small_image')->resize(220, 200); ?>" title="<?php echo $this->htmlEscape($this->getImageLabel($_product, 'small_image')) ?>">
<img class="media-object" src="<?php echo $this->helper('catalog/image')->init($_product, 'small_image')->resize(100, 100); ?>" alt="<?php echo $this->htmlEscape($this->getImageLabel($_product, 'small_image')) ?>" title="<?php echo $this->htmlEscape($this->getImageLabel($_product, 'small_image')) ?>" />
</a>
</li>
<div class="media-body">
<h4 class="media-heading"><a class="view-item-button" href="<?php echo $_product->getProductUrl() ?>" title="<?php echo $this->htmlEscape($_product->getName()) ?>"><?php echo $this->__('Order'); ?> <?php echo $this->htmlEscape($_product->getName())?><?php echo $this->__('™'); ?></a></h4>
<div class="media">
<?php echo $_product->_data['short_description']; ?>
<div class="clearfix"></div>
<h6>Price <?php echo Mage::helper('core')->currency($_product->getPrice());; ?></h6>
<?php if($_product->isSaleable()): ?>
<button type="button" title="<?php echo $this->__('Add to Cart') ?>" class="btn btn-danger btn-mini" onclick="setLocation('<?php echo $this->getAddToCartUrl($_product) ?>')"><span><span><?php echo $this->__('Add to Cart') ?></span></span></button>
<?php else: ?>
<p class="availability out-of-stock"><span><?php echo $this->__('Out of stock') ?></span></p>
<?php endif; ?>
</div>
</div>
<?php } ?>
</ul>
<script type="text/javascript">decorateGeneric($$('ul.products-grid'), ['odd','even','first','last'])</script><?php endif; ?>
<script type="text/javascript">
$j(document).ready(function() {
$j(".fancybox").fancybox();
});
</script>
You could use querystrings to achieve this, as explained here
I am able to add a product to cart by using the following block code:
 
{{block type="catalog/product_list_related" category_id="49" template="PATH_TO_TEMPLATE_FILE"}}
Then add a a checkout button to the file and it works!
`<?php if($_product->isSaleable()): ?>
<button type="button" title="<?php echo $this->__('Add to Cart') ?>" class="btn btn-danger" onclick="setLocation('<?php echo $this->getAddToCartUrl($_product) ?>')"><span><span><?php echo $this->__('Add to Cart') ?></span></span></button>
<?php else: ?>
<p class="availability out-of-stock"><span><?php echo $this->__('Out of stock') ?></span></p>
<?php endif; ?> `
<?php
$category_id = 49; // category_id for "Accessories"
$category = new Mage_Catalog_Model_Category();
$category->load($categoryid);
$_productCollection = $category->getProductCollection();
$_productCollection->addAttributeToSelect('*');
?>
<?php if($_productCollection->count()): ?>
<ul class="media-list pull-left">
<?php
$products = array();
foreach ($_productCollection as $_product) {
?>
<li class="media">
<a class="fancybox pull-left" href="<?php echo $this->helper('catalog/image')->init($_product, 'small_image')->resize(220, 200); ?>" title="<?php echo $this->htmlEscape($this->getImageLabel($_product, 'small_image')) ?>">
<img class="media-object" src="<?php echo $this->helper('catalog/image')->init($_product, 'small_image')->resize(100, 100); ?>" alt="<?php echo $this->htmlEscape($this->getImageLabel($_product, 'small_image')) ?>" title="<?php echo $this->htmlEscape($this->getImageLabel($_product, 'small_image')) ?>" />
</a>
</li>
<div class="media-body">
<h4 class="media-heading"><a class="view-item-button" href="<?php echo $_product->getProductUrl() ?>" title="<?php echo $this->htmlEscape($_product->getName()) ?>"><?php echo $this->__('Order'); ?> <?php echo $this->htmlEscape($_product->getName())?><?php echo $this->__('™'); ?></a></h4>
<div class="media">
<?php echo $_product->_data['short_description']; ?>
<div class="clearfix"></div>
<h6>Price <?php echo Mage::helper('core')->currency($_product->getPrice());; ?></h6>
<?php if($_product->isSaleable()): ?>
<form id="addtocartform_<?=$_product->getId()?>" action="<?=Mage::helper('checkout/cart')->getAddUrl($_product)?>">
<input type="hidden" value="<?=$_product->getId()?>" name="product" id="product"/>
<?php if(!$_product->isGrouped()): ?>
<input type="text" name="qty" id="qty" maxlength="12" value="1" />
<?php endif; ?>
<button type="button" class="button btn-cart" onclick="this.form.submit()"><span><span>Add To Cart</span></span></button>
</form>
<?php else: ?>
<p class="availability out-of-stock"><span><?php echo $this->__('Out of stock') ?></span></p>
<?php endif; ?>
</div>
</div>
<?php } ?>
</ul>
<?php endif; ?>

How to echo custom text field attribute instead of product url

I'm trying to echo a custom text field attribute into a url link.
I have a CMS page with URL key "my-page" so I have created a text field called "cmspageid" for a product and added the URL key "my-page" into the text filed.
Here's the whole script:
<div class="new-product-content">
<?php if (($_products = $this->getProductCollection()) && $_products->getSize()): ?>
<h3 class="subtitle new-products"><?php echo $this->__('HOT PRODUCTS') ?></h3>
<?php $_columnCount = $this->getColumnCount(); ?>
<?php $i=0; foreach ($_products->getItems() as $_product): ?>
<?php if ($i++%$_columnCount==0): ?>
<ul>
<?php endif ?>
<li class="thumb<?php if(($i-1)%$_columnCount==0): ?> first<?php elseif($i%$_columnCount==0): ?> last<?php endif; ?>">
<a href="<?php echo $_product->getProductUrl() ?>" title="<?php echo $this->htmlEscape($_product->getName()) ?>" ><img src="<?php echo $this->helper('catalog/image')->init($_product, 'small_image')->resize(175) ?>" width="175" height="175" alt="<?php echo $this->htmlEscape($_product->getName()) ?>" />
</a>
<div class="caption">
<h4 class="product-name"> <?php $_productName = $this->helper('core/string')->truncate($this->htmlEscape($_product->getName()),20,'...', $_remainder, true); ?>
<a href="<?php echo $_product->getProductUrl() ?>" title="<?php echo $_productName ?>">
<?php echo $_productName ?><?php echo $this->__('™'); ?></a></h4>
<?php echo $this->getPriceHtml($_product, true, '-new') ?>
<div class="clearfix"></div>
<div class="desc">
<?php $sdesc = $_product->getShortDescription();
$sdesc = trim($sdesc);
$limit = 180;
if (strlen($sdesc) > $limit) {
$sdesc = substr($sdesc, 0, strrpos(substr($sdesc, 0, $limit), ' '));
} ?>
<?php echo $sdesc."..."; ?>
</div>
<div class="clearfix"></div>
<div class="btn-group">
<button class="btn" type="button" title="<?php echo $this->__('Add to Cart') ?>" onclick="setLocation('<?php echo $this->getProductUrl($_product) ?>')"><?php echo $this->__('Order now...') ?></button>
<button class="btn-link" type="button" title="<?php echo $this->__('More info') ?>" onclick="setLocation('<?php echo $this->getProductUrl($_product) ?>')"><?php echo $this->__('More info?') ?></button>
</li>
<?php if ($i%$_columnCount==0 || $i==count($_products)): ?>
</ul>
<div class="clearfix"></div>
<?php endif ?>
<script type="text/javascript">
$j('.thumb li').last().css('border-right', 'none');
</script>
<script type="text/javascript">decorateGeneric($$('ul.products-grid'), ['odd','even','first','last'])</script>
<?php endforeach; ?>
<?php endif; ?>
<div class="clearfix"></div>
</div>
Now I need include cmspageid instead of getProductUrl:
<button class="btn-link" type="button" title="<?php echo $this->__('More info') ?>" onclick="setLocation('<?php echo $this->getUrl('')?><?php echo $_product->getCmspageid() ?>')"><?php echo $this->__('More info?') ?></button>
So when the link is clicked the correct CMS page loads.
If anyone can give us a hand I'd be very grateful.
Many thanks
You should try something like that:
Link Here
My problem was somehow getting the attribute text as part of the URL foreach product. Since I was using the catalog/product_new I couldn't echo the attribute text as Magento only allows attribute to be displayed on catalog/product_view pages!
I had to modify the new.php file at line 114 adding this line:
->addAttributeToSelect('cmspageid')
Then in the new.phtml file where I have the "More info" button I echoed the value from 'cmspageid'
__('More info') ?>" onclick="setLocation('getCmspageid(); ?>')">__('More info?')
?>
Now I have an "Order now" button and a "More info" button for each product. When "More info" is clicked the user will go to a CMS Page showing full details for that product...
Phew!

Categories