opencart make table of categories insted of carousel - php

I am just trying to remake manufacturers carousel so that it will show categories with their images. Part of it I have done. Now i jast want to show them in table 4 in a row. Here is the code that is file catalog/view/module/carousel.tpl/
<div id="carousel<?php echo $module; ?>" class="owl-carousel">
<div class="category">
<table>
<?php if($categories) { ?>
<?php $count = 0;?>
<?php foreach ($categories as $category) { ?>
<?php if($count % 4 == 1){?>
<tr>
<?php}?>
<td>
<a href="<?php echo $category['href']; ?>" <?php echo $category['name']; ?></a>
<img src="<?php echo $category['image'] ?>" alt="<?php echo $category['name']; ?>" title="<?php echo $category['name']; ?>" class="img-thumbnail" />
<p><?php echo $category['name']?>
</td>
<?php if($count % 4 == 0){?>
</tr>
<?php}?>
<?php } ?>
<?php } ?>
</table>
</div>
</div>
<script type="text/javascript"><!--
$('#carousel<?php echo $module; ?>').owlCarousel({
items: 15,
autoPlay: 1000000,
navigation: false,
navigationText: ['<i class="fa fa-chevron-left fa-5x"></i>', '<i class="fa fa-chevron-right fa-5x"></i>'],
pagination: false
});
--></script>
As a result of it I am getting error unexpected end of file. What am I doing wrong? Are their better solutions? I am using opencart version 2.0.1.1

You need to make some minor changes and this will work:
<?php if($count % 4 == 0){?>
</tr>
<?php } ?> //here you need to change
$count++;
Give the space after <?php tag. due to this the error occurs. There is require a space after this tag.
And you are increasing the value of $count.

Related

Show random product on homepage through phtml

I have a block being displayed on my homepage which shows a product, whose id we specify. The code on homepage (static block) looks like this:
{{block type="core/template" product_id="2559" template="catalog/product/one_product.phtml"}}
The one_product.phtml file contains this code:
<?php
$productId = $this->getProduct_id();
$product = Mage::getModel('catalog/product')->load($productId); //load product
?>
<div class="product">
<a href="<?php echo $product->getProductUrl() ?>" >
<img class="product-img" src="<?php echo $this->helper('catalog/image')->init($product, 'image'); ?>"alt="<?php echo $this->htmlEscape($product->getName()) ?>" />
</a>
</div>
<div class="product-detail">
<P><?php // echo $this->htmlEscape($product->getName()) ?>
<?php $prod_name = $this->htmlEscape($product->getName()); ?>
<?php
$count_str = strlen($prod_name);
if ($count_str < 40) {
echo $prod_name;
} else {
$offset = 0;
$length = 41;
$prod_name = html_entity_decode($prod_name);
echo htmlentities(mb_substr($prod_name,0,$length,'utf-8')). "...";;
}
?>
</P>
<!--?php $price = $product->getPrice() ; ?-->
<?php $_product = Mage::getModel('catalog/product')->load($product->getId());
$productBlock = $this->getLayout()->createBlock('catalog/product_price');
?>
<span>
<?php echo $productBlock->getPriceHtml($_product); ?>
<?php $tier_price = end($_product->getTierPrice());
if($tier_price !='0'){ ?>
<span>As Low As:</span>
<?php
echo " ₹ ".number_format( $tier_price['price']);
} ?>
<button type="button" title="<?php echo $this->__('Add to Cart') ?>" class="button btn-cart"
onclick="setLocation('<?php echo Mage::helper('checkout/cart')->getAddUrl($product); ?>')">
</span>
</div>
So basically I want to show random products out of the ones I specify separated by commas. For eg: I specify in the static block product_id="2559,2661,2857,9293" and it should show any one of those 4 products randomly.
What is the way to do that?
Also any way to make it pull products from SKU also? Since we remember all the SKUs but we have to check each product ID everytime we change the product here.
Please excuse me if the question is too basic, I'm not a developer.
You can see it in action at www.giftzila.com
Thanks!
Create a new file called random-product.phtml at app/design/frontend/default/Your_Theme/template/catalog/random-product.phtml then add the following code in that file
<?php
$chosen_category = Mage::getModel('catalog/category')->load($this->getCategoryId());
$_productCollection = $this->getLoadedProductCollection();
$number_of_products = $this->getNumProducts();
if (sizeof($_productCollection) < $number_of_products) {
$number_of_products = sizeof($_productCollection);
}
$displayed_products = array();
foreach ($_productCollection as $_product) {
$displayed_products[] = $_product;
}
$random_products = array();
if (sizeof($_productCollection) > 1) {
$random_products = array_rand($displayed_products, $number_of_products);
} else {
$random_products = array('0');
}
?>
<?php if(!$_productCollection->getSize()):?>
<div class="note-msg">
<?=$this->__('There are no products matching the selection.')?>
</div>
<?php else:?>
<div class="main-binder">
<div class="cms-box">
<div class="category-title">
<h2>Random Products</h2>
</div>
<div class="category-products">
<table id="products-grid-table" class="products-grid">
<?php
$k=0;
for ($i=0; $i < $number_of_products; $i++): ?>
<?php if ($k == 0) { ?>
<tr class="first odd">
<?php } if($k==3) { $k=0; ?>
</tr><tr class="first odd even">
<?php } ?>
<td id="td_<?php echo ($k+1); ?>" <?php if($k==3){ ?>class="last"<? } ?> >
<div class="cms-box">
<div id="cont_<?php echo ($k+1); ?>">
<div class="product-name-block">
<?php
$pname=$this->htmlEscape($displayed_products[$random_products[$i]]->getName());
?>
<h3 class="product-name">
<a href="<?php echo $displayed_products[$random_products[$i]]->getProductUrl()?>" title="<?php echo $pname; ?>">
<?php if(strlen($pname) > 28) {echo substr($pname,0,25)."...";}else {echo $pname;}?>
</a></h3>
</div>
<div class="image-box">
<a class="product-image" href="<?php echo $displayed_products[$random_products[$i]]->getProductUrl()?>"> <img src="<?php echo $this->helper('catalog/image')->init($displayed_products[$random_products[$i]], 'small_image')->resize(140);?>" alt="<?php echo $this->htmlEscape($displayed_products[$random_products[$i]]->getName())?>" title="<?php echo $this->htmlEscape($displayed_products[$random_products[$i]]->getName())?>"/> </a>
</div>
<div class="cms-price-box" style=" text-align:center;">
<span class="regular-price" id="product-price-37">
<span class="price" ><?php echo Mage::app()->getLocale()->currency(Mage::app()->getStore()->
getCurrentCurrencyCode())->getSymbol().$displayed_products[$random_products[$i]]->getPrice(); ?></span>
</span>
</div>
<div class="button-row" style="text-align:center;">
<button onclick="setLocation('<?php echo $displayed_products[$random_products[$i]]->getProductUrl();?>')" class="button" type="button"><span><span><span>Details</span></span></span></button>
<button onclick="setLocation('<?php echo $this->getUrl('')."/checkout/cart/add?product=".$displayed_products[$random_products[$i]]->getId()."&qty=1" ?>')" class="button"><span><?php echo $this->__('Add to Cart') ?></span></button>
</div>
</div>
</div></td>
<?php $k++; ?>
<?php endfor;?>
</tr>
</table>
</div>
</div>
</div>
<?php endif;?>
Now call block in your cms home page by adding following code:-
{{block type="catalog/product_list" category_id="3" num_products="8" template="catalog/random-product.phtml"}}

Page goes blank after adding to PHP foreach

Hello after I add some code to this PHP foreach the page goes blank and all I can see is a round black dot at the top left of screen.
My website is in Joomla3 and I am trying to customize a module. my site is http://get2gethersports.com
I have a recent post module that only shows the articles title.
that code is posted below
<?php if ($items) { ?>
<ul class="rsblog-recent-module unstyled<?php echo $params->get('moduleclass_sfx',''); ?>">
<?php foreach ($items as $item) { ?>
<li>
<a <?php echo $opener; ?> href="<?php echo JRoute::_('index.php?option=com_rsblog&view=post&id='.RSBlogHelper::sef($item->id,$item->alias).$Itemid,false); ?>">
<?php echo $item->title; ?>
</a>
</li>
<?php } ?>
</ul>
<?php } ?>
I would like to add an image abocve like the blog feed on http://vape-co.com
So I navigated to the component and saw the call for the image. which is posted below:
<div class="rsblog-entry-content">
<?php if ($this->item->image) { ?>
<div class="rsblog-entry-image">
<img class="rsblog-entry-thumb img-polaroid" src="<?php echo JURI::root().'components/com_rsblog/assets/images/blog/'.$this->item->image; ?>?nocache=<?php echo uniqid(''); ?>" alt="<?php echo $this->escape($this->item->title); ?>">
</div>
<?php } ?>
but whenever i add it or a snippet of it to the previous code it breaks....
Any ideas why it is breaking the page and how to fix it?
I tried adding in new li tags. Just adding the PHP part above the a link etc...
CODE UPDATE----
<?php if ($items) { ?>
<ul class="rsblog-recent-module unstyled<?php echo $params->get('moduleclass_sfx',''); ?>">
<?php foreach ($items as $item) { var_dump($item);?>
<li>
<div class="rsblog-entry-content">
<?php if ($this->item->image) { ?>
<div class="rsblog-entry-image">
<img class="rsblog-entry-thumb img-polaroid" src="<?php echo JURI::root().'components/com_rsblog/assets/images/blog/'.$this->item->image; ?>?nocache=<?php echo uniqid(''); ?>" alt="<?php echo $this->escape($this->item->title); ?>">
</div>
</div>
<?php } ?>
<a <?php echo $opener; ?> href="<?php echo JRoute::_('index.php?option=com_rsblog&view=post&id='.RSBlogHelper::sef($item->id,$item->alias).$Itemid,false); ?>">
<?php echo $item->title; ?>
</a>
</li>
<?php } ?>
</ul>
<?php } ?>
Try $item->image instead of $this->item->image
Correct code. Looks like it just needed some reduction.
<?php if ($items) { ?>
<ul class="rsblog-recent-module unstyled<?php echo $params -> get('moduleclass_sfx',''); ?>">
<?php foreach ($items as $item) { ?>
<li>
<?php if($item->image != '') ?>
<img src="components/com_rsblog/assets/images/blog/<?php echo $item->image;?>" alt="<?php echo $item->title. "logo";?>" width="100px"/>
<br/>
<a <?php echo $opener; ?> href="<?php echo JRoute::_('index.php?option=com_rsblog&view=post&id='.RSBlogHelper::sef($item->id,$item->alias).$Itemid,false); ?>">
<?php echo $item->title; ?>
</a>
</li>
<?php } ?>
</ul>
<?php } ?>

Foreach in table...limit to 7 then create new row

I have this code printing the output of a list of best selling products. The problem is it spans horizontally without breaking and goes off the side of the screen after 7 products.
What I'd like to do, and am completely stymied as my attempts have been fruitless, is to have the 8th to 14th entries start a new row. How do I need to modify my code in order to accomplish this?
<?php $products = $this->getCollection(); ?>
<?php if ($products && $products->count() > 0) { ?>
<div class="block block-list block-viewed">
<div class="block-title">
<strong><span><?php echo $this->__($this->getHeader()) ?></span></strong>
</div>
<div class="block-content">
<table class="amsorting-table">
<tr>
<?php foreach ($products as $p) { ?>
<td style="padding: 15px 15px 0px 15px;">
<a href="<?php echo $p->getProductUrl() ?>" title="<?php echo $this->htmlEscape($p->getName()) ?>" class="product-image"><img
src="<?php echo $this->helper('catalog/image')->init($p, 'small_image')->resize(125) ?>" width="125" height="125" alt="<?php echo
$this->htmlEscape($p->getName()) ?>" /></a>
<h3 class="product-name"><a href="<?php echo $p->getProductUrl() ?>" title="<?php echo $this->htmlEscape($p->getName())
?>"><?php echo $this->htmlEscape($p->getName()) ?></a></h3>
<?php echo $this->getPriceHtml($p, true) ?>
</td>
<?php } ?>
</tr>
<tr>
<?php foreach ($products as $p) { ?>
<td style="padding: 0px 15px 15px;">
<?php if($p->isSaleable()): ?>
<button type="button" title="<?php echo $this->__('Add to Cart') ?>" class="button btn-cart"
onclick="setLocation('<?php echo $this->getAddToCartUrl($p) ?>')"><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 } ?>
</td>
</tr>
</table>
</div>
</div>
<?php } ?>
Do your <tr> tag conditionally, based on whether the current product index is divisible by 7
<table class="amsorting-table">
<?php
foreach ($products as $i => $p) {
if ($i % 7 == 0) { // start a new row on product 0, 7, 14, etc
if ($i > 0) { // if this is not the 1st product, close the previous row
?></tr><?php
}
?><tr><?php
}
... your code the display the table cell for the current product
}
// after the loop, we need to close the last row
?></tr><?php
...

Navigation menu with product thumbnails Magento

Hi guys I am trying to integrate CSS3 Mashmenu onto my Magento store http://www.mybloggerlab.com/2012/07/mashable-drop-down-navigation-menu-for.html
I would like to setup the menu to show product categories which when hovered over show product thumbnails with short description for each product in that category.
The prodblem I am having is setting this up so it is dynamic as I would prefer to have the menu be controllable via admin.
If someone could tell me where I am going wrong I would be very grateful as the current code isn't working.
<div id="pageContainer">
<div class="mashmenu">
<div class="fnav"><?php echo $this->__('BROWSE PRODUCTS'); ?>+
<div class="allContent">
<?php foreach ($_categories as $_category): ?>
<?php if($_category->getIsActive()): ?>
<div class="snav"><a href="#" class="slink">
<a href="<?php echo $this->getCategoryUrl($_category) ?>"<?php if ($this->isCategoryActive($_category)): ?> class="current"<?php endif; ?>><?php echo $this->htmlEscape($_category->getName()) ?></a> (<?php echo $_category->getProductCount() ?>)</a>
<?php $collection = $_category->getProductCollection()->addAttributeToSort('name', 'asc'); ?>
<?php foreach ($collection as $_product) : ?>
<div class="insideContent">
<a href="<?php echo $this->getProductUrl($_product) ?>" title="<?php echo $this->stripTags($_product->getName(), null, true) ?>" class="product-image">
<img src="<?php echo $this->helper('catalog/image')->init($_product, 'small_image')->resize(75) ?>" width="75" height="75" alt="<?php echo $this->stripTags($_product->getName(), null, true) ?>" />
</a>
<span> <?php $sdesc = $_product->getShortDescription();
$sdesc = trim($sdesc);
$limit = 170;
if (strlen($sdesc) > $limit) {
$sdesc = substr($sdesc, 0, strrpos(substr($sdesc, 0, $limit), ' '));
} ?>
<?php echo $sdesc."..."; ?></span>
</div>
<?php endforeach; ?>
</div>
<?php endif; ?>
<?php endforeach; ?>
<!-- end insideContent -->
</div>
</div>
</div>
</div>
<script>
$j(document).ready(function(){
$j('div.mashmenu img').css({"width":"100px","height":"60px"});
$j('div.mashmenu').find('.allContent').css({"top":"38px"});
$j('div.mashmenu').mouseleave(function(){
$j('div.mashmenu .allContent').show('50');
$j('div.mashmenu .insideContent').fadeOut('50');
});
$j('.flink').mouseenter(function(){
$j('div.mashmenu .allContent').show('50');
$j(this).parent('.fnav').children('.allContent').show(200);
});
$j('.slink').mouseenter(function(){
if($j(this).parent('.snav').children('.insideContent').find('a').size() != 0 )
$j(this).parents('.allContent').css({"width":"640px","height":"500px"});
else $j(this).parents('.allContent').css({"width":"auto","height":"auto"});
$j('div.mashmenu .insideContent').fadeOut('50');
$j(this).parent('.snav').children('.insideContent').fadeIn(200);
});
$j('.snav').mouseleave(function(){
$j(this).parents('.allContent').css({"width":"auto","height":"auto"});
});
$j('.snav').mouseenter(function(){
$j(this).children('.insideContent').css({"display":"block","position":"absolute","width":"auto","height":"450px"});
});
});
</script>
<?php endif; ?>
<div class="insideContent">
<img src="<?php echo $this->helper('catalog/image')->init($_product, 'small_image')->resize(75) ?>" width="75" height="75" alt="<?php echo $this->stripTags($_product->getName(), null, true) ?>" /> <span> <?php $sdesc = $_product->getShortDescription();
$sdesc = trim($sdesc);
$limit = 170;
if (strlen($sdesc) > $limit) {
$sdesc = substr($sdesc, 0, strrpos(substr($sdesc, 0, $limit), ' '));
} ?>
<?php echo $sdesc."..."; ?></span>
</div>
Your $_product is undefined here, moreover you'll have only one product per category so which one to show ?
replace this by something like :
<?php $collection = Mage::getModel('catalog/product')->getCollection()->addCategoryFilter($_category)->setPageSize(4);
<div class="insideContent">
<?php foreach ($collection as $_product) { ?>
//Your product here, copy your code
<?php } /*end foreach*/ ?>
</div>
It should work better like this.

generating links dynamically

so i guess this is pretty easy for most of you, but i can't figure this out.
im trying to make the links dynamic eg: href="linkname(#1 or #2 etc)"
any ideas?
<?php if ($top_fundraisers && is_array($top_fundraisers)): ?>
<?php foreach ($top_fundraisers as $index => $fundraiser): ?>
<a title="" class="fancybox" href="linkname(GENERATE CODE HERE)">
<div class="top-fundraiser">
<div id="newo<?php print htmlentities($index + 1); ?>" class="top-fundraiser-image">
<img src="<?php
if($fundraiser['member_pic_medium']) {
print htmlentities($fundraiser['member_pic_medium']);
} else {
print $template_dir . '/images/portrait_placeholder.png';
}
?>"/>
</div>
</div>
</a>
<?php endforeach;?>
<?php endif; ?>
Suppose below is what you need.
<?php if ($top_fundraisers && is_array($top_fundraisers)): ?>
<?php foreach ($top_fundraisers as $index => $fundraiser): ?>
<a title="" class="fancybox" href="linkname(#<?php echo $index + 1; ?>)">
<div class="top-fundraiser">
<div id="newo<?php print htmlentities($index + 1); ?>" class="top-fundraiser-image">
<img src="<?php
if($fundraiser['member_pic_medium']) {
print htmlentities($fundraiser['member_pic_medium']);
} else {
print $template_dir . '/images/portrait_placeholder.png';
}
?>"/>
</div>
</div>
</a>
<?php endforeach;?>
<?php endif; ?>

Categories