Echo short description for related products - php

Is there a way to echo the short description foreach related product?
I've tried to use this code but it doesn't display the description of each product:
<?php echo nl2br($this->getProduct()->getDescription()) ?>
also
<?php echo $_helper->productAttribute($_item, nl2br($_item->getShortDescription()), 'short_description') ?>
Is there a way to do this for related products? If anyone knows please point me in the right direction.
<?php if($this->getItems()->getSize()): ?>
<div class="block block-related">
<div class="block-title">
<strong><span><?php echo $this->__('Related Products') ?></span></strong>
</div>
<div class="block-content">
<p class="block-subtitle"><?php echo $this->__('Check items to add to the cart or add to your wishlist') ?> <br /></p>
<div class="form-horizontal">
<?php foreach($this->getItems() as $_item): ?>
<div class="control-group">
<label for="related-checkbox<?php echo $_item->getId() ?>" class="control-label">
<a class="fancybox static-thumbs pull-left" href="<?php echo $this->helper('catalog/image')->init($_item, 'small_image')->resize(500, 450); ?>" title="<?php echo $this->htmlEscape($_item->getName()) ?>" ><img src="<?php echo $this->helper('catalog/image')->init($_item, 'small_image')->resize(135, 135) ?>" width="135" height="135" alt="<?php echo $this->htmlEscape($_item->getName()) ?>" /></a>
</label>
<div class="controls">
<label class="checkbox">
<?php if(!$_item->isComposite() && $_item->isSaleable()): ?>
<?php if (!$_item->getRequiredOptions()): ?>
<input type="checkbox" class="checkbox related-checkbox" id="related-checkbox<?php echo $_item->getId() ?>" name="related_products[]" value="<?php echo $_item->getId() ?>" />
<?php endif; ?>
<?php endif; ?>
<?php if ($this->helper('wishlist')->isAllow()) : ?>
<span class="icon-check"></span>
<p class="product-name span6">
<?php echo $this->htmlEscape($_item->getName()) ?>
<br />
<?php echo nl2br($this->getProduct($_item)->getDescription()) ?>
</p>
<form action="<?php echo $this->getAddToCartUrl($_item) ?>" method="post">
<fieldset>
<label class="product-name"><?php echo $this->__('Quantity:'); ?></label>
<select name="qty" class="span1">
<?php $i = 1 ?>
<?php do { ?>
<option value="<?php echo $i?>">
<?php echo $i?>
<?php $i++ ?>
</option>
<?php } while ($i <= (int)Mage::getModel('cataloginventory/stock_item')->loadByProduct($_item)->getMaxSaleQty()) ?>
</select>
<div class="clearfix"></div>
<button class="btn btn-danger" data-loading-text="PLease wait..."><span><?php echo $this->__('Add to Cart') ?></span></button>
<span id='ajax_loader' style='display:none'><img src='<?php echo $this->getSkinUrl('images/opc-ajax-loader.gif')?>'/></span>
</fieldset>
</form>
<?php endif; ?>
<?php echo $this->getPriceHtml($_item, true, '-related') ?>
</label>
</div>
</div>
<hr />
<?php endforeach ?>
</div>
</div>

Have you tried nl2br($this->getProduct($_item)->getShortDescription()) ?
It might be that the value for short description isn't loaded for related products by default. You could try to do a $_item->load($_item->getId()) and then use $_item->getShortDescription().

after TRYING $_item->load($_item->getId()) and echoing the shortDescription, you should look for the Collection where the related products are loaded. In this collection the attribute short_description is not loaded with.
If you load every product, you make a lot of queries to the database which will cost a lot of performance. This is not needed. The alternative is:
$relatedProductCollection->addAttributeToSelect('short_description');
The question is to find the $relatedProductCollection.
UPDATE:
I dug into the code (Reference is v. 1.7.0.2):
// app/code/core/Mage/Catalog/Block/Product/List/Related.php:61
$this->_addProductAttributesAndPrices($this->_itemCollection);
// app/code/core/Mage/Catalog/Block/Product/Abstract.php:410
$collection
->addAttributeToSelect(Mage::getSingleton('catalog/config')->getProductAttributes())
// app/code/core/Mage/Catalog/Model/Config.php:260
$this->getAttributesUsedInProductListing()
This should be interpreted as: When the "related products" are loaded, all attributes are loaded, which are used in the product listing as well. The problem might be: short_desciption is loaded by default. So remove the load() call and check, wether the attribute short_description is "Used in Product Listing". If this is set to yes, My interpretation is wrong.
None the less, the solution by loading every product is crap.
EDIT:
I have added the following line to Related.php
$this->_itemCollection = $product->getRelatedProductCollection()
->addAttributeToSelect('required_options')
->addAttributeToSelect('short_description')
->setPositionOrder()
->addStoreFilter()
And using this line does echo a descrtiption foreach product but it is the Same description getProduct()->getDescription()) ?>
You need to <?php echo ($this->getProduct()->getShortDescription()) ?>
But editing the core code is a bad way to change the magento behaviour as well. Here is described how to rewrite a block, but I don't think this is necessary. http://prattski.com/2010/06/24/magento-overriding-core-files-blocks-models-resources-controllers/

Related

Retrieving many images from mysql automatically by an echo and enlarging them on click

I have a PhP shopping cart for autoparts and I need to be able to enlarge the retrieved image upon clicking on it. The problem is I can't manually assign an ID to every picture because I'm echoing them from the database.
I've tried assigning one ID/class to all the pictures that get retrieved and enlarge them on click using the JS function but can't get it working.
$items_array = $db_controller->runQuery("SELECT * FROM mystuff ORDER BY ID ASC");
if (!empty($items_array)) {
foreach($items_array as $key=>$value){
?>
<div class="DisplayCartItems">
<form method="post" action="?action=add&mscode=<?php echo $product_array[$key]["mscode"]; ?>">
<img src="<?php echo $product_array[$key]["msimage"]; ?>" class="imgcartdisplay" align="alignnone"/> /* Right here I need some help */
<div class="lif"><strong>Category:</strong> <?php echo $product_array[$key]["mscategory"]; ?></div>
<div class="lif"><strong>Category №:</strong> <?php echo $product_array[$key]["mscatnum"]; ?></div>
<div class="lif"><strong>Stock №:</strong> <?php echo $product_array[$key]["msnomer"]; ?></div>
<div class="lif"><strong>Mark:</strong> <?php echo $product_array[$key]["msmark"]; ?></div>
<div class="lif"><strong>Model:</strong> <?php echo $product_array[$key]["msmodel"]; ?></div>
<div class="lif"><strong>Year:</strong> <?php echo $product_array[$key]["msyear"]; ?></div>
<div class="lif"><strong>Price:</strong> <?php echo "$".$product_array[$key]["msprice"]; ?></div>
<div style="text-align:center"><input type="number" name="oap" placeholder="Offer a price:"></div>
<br class="">
<div class="cartan">
Quantity: <input type="text" class="product-quantity" name="quantity" value="1" size="2" />
<br class="">
<input type="submit" onclick="AddCartAlert()" value="Add to cart" /></div>
</form>
</div>
<?php
}
}
?>```
I think if I can somehow assign an ID or class name for every picture that get displayed from the MySql and onclick change it's width/height, that'd do the job.
If I have correctly understood:
you should replace this:
<img src="<?php echo $product_array[$key]["msimage"]; ?>" class="imgcartdisplay" align="alignnone"/> /* Right here I need some help */
with:
<a target="_blank" href="<?php echo $product_array[$key]["msimage"]; ?>"><img src="<?php echo $product_array[$key]["msimage"]; ?>" class="imgcartdisplay" align="alignnone"/></a>
for opening the image in another tab.
Or... if you mean something like this https://lokeshdhakar.com/projects/lightbox2/#examples you have to use javascript (for example: Image #1)
In your case something like this: (of course you need to add lightbox script: https://lokeshdhakar.com/projects/lightbox2/#getting-started)
<a data-lightbox="image-1" data-title="My caption" href="<?php echo $product_array[$key]["msimage"]; ?>"><img src="<?php echo $product_array[$key]["msimage"]; ?>" class="imgcartdisplay" align="alignnone"/></a>

Hide add to cart for Guest User in Magento

I got some code while searching for hide add to cart button at category products but i am not able to set this below code to my given List.phtml, Please guide me.
Now we will hide the ‘Add to Cart’ button on category list page. Open /app/design/frontend/default/themeXXX/template/catalog/product/list.phtml file and look for the following code:
<?php
if (!Mage::getSingleton('customer/session')->isLoggedIn()) {
echo '<span class="login_for_details" style="float:left"><strong>Login to Add to Cart</strong></span>';
} else { ?>
My website list.phtml
<?php if ($product->isSaleable()) : ?>
<?php if ( !($product->getTypeInstance(true)->hasOptions($product) || $product->isGrouped()) ) : ?>
<?php if(!Mage::getStoreConfig("ajaxcart/addtocart/enablecategory", $code)):?>
<form id="addtocart_form_<?php echo $_product->getId(); ?>" action="<?php echo $this->getAddToCartUrl($_product) ?>" method="post">
<input type="hidden" name="form_key" value="<?php echo Mage::getSingleton('core/session')->getFormKey(); ?>" />
<?php endif; ?>
<div class="qty-field">
<label for="qty"><?php echo $this->__('Qty:') ?></label>
<div class="qty-holder">
<input type="text" name="qty" id="qty_<?php echo $_product->getId(); ?>" maxlength="12" value="<?php echo $product->getProductDefaultQty() * 1 ?>" title="<?php echo $this->__('Qty') ?>" class="input-text qty" />
<div class="qty-changer">
<i class="icon-up-dir"></i>
<i class="icon-down-dir"></i>
</div>
</div>
</div>
<?php if (!Mage::getStoreConfig("ajaxcart/addtocart/enablecategory", $code)) :?>
</form>
<?php endif; ?>
<?php endif; ?>
<?php endif; ?>
<?php endif; ?>
If your changing in your core code then add the below code in your List.phtml
<?php $session = Mage::getSingleton('customer/session', array('name' => 'frontend')); ?>
<?php if($session->isLoggedIn()) { ?>
//Your Add to Cart Button Html
<?php } else { ?>
// Your Login to Add to Cart Html
<?php } ?>
Put the above code everywhere on your frontend where your products are displaying.

Magento - related product color swatches

I'm working with Magento EE v1.14 and i'm looking for a solution for when a user is viewing a product page to then drop swatches of related product colors if they are out of stock.
Screenshot: Highlighted out of stock related product color
Screenshot of HTML
PHP + HTML code:
<?php
$_base_product = $this->getProduct();
$base_product = Mage::getModel('catalog/product')->load($_base_product->getId());
$base_product_id = $base_product->getId();
$base_name = $base_product->getName();
$base_url = Mage::getBaseUrl();
$product_colors = Mage::getModel('catalog/product')->getCollection();
$product_colors->addAttributeToFilter('status',1); // 1 or 2
$product_colors->addAttributeToFilter('visibility',4); // 1.2.3.4
$product_colors->addAttributeToFilter('name', array('eq' => $base_name));
$product_colors->addAttributeToFilter('sku', array('neq' => $base_product->getSku()));
$product_colors_ids = $product_colors->getAllIds(); // get all products from the category
sort($product_colors_ids);
?>
<?php if(count($product_colors_ids) > 0) : ?>
<div id="product-color-options-wrapper">
<div id="product-color-options-container">
<label><?php echo $this->__('Color') ?> / <span style="font-weight: normal;"><?php echo $base_product->getAttributeText('color'); ?></span></label>
<div id="color-options-wrapper">
<?php $_swatch_img = $base_product->getMediaGalleryImages(false)->getItemByColumnValue('label', 'swatch') ?>
<?php if($_swatch_img) : ?>
<div class="current-product-wash-wrapper wash-wrapper">
<div class="current-product-wash-container wash-container">
<img src="<?php echo $this->helper('catalog/image')->init($base_product, 'small_image', $_swatch_img->getFile())->resize(33,30) ?>" alt="" title="<?php echo $base_product->getAttributeText('color') ?>" />
</div>
</div>
<?php else : ?>
<!-- <span><?php echo $base_product->getColor() ?></span> -->
<?php endif ?>
<?php foreach($product_colors_ids as $prod_id) : ?>
<?php $_sister_product = Mage::getModel('catalog/product')->load($prod_id) ?>
<?php
$_sister_prod_imgs = $_sister_product->getMediaGallery('images');
foreach($_sister_prod_imgs as $_sister_prod_img):
if($_sister_prod_img['label'] == 'swatch'):
$_swatch_img = $_sister_prod_img['file'];
endif;
endforeach;
?>
<?php if($_swatch_img): ?>
<div class="sister-product-wrapper wash-wrapper">
<div class="sister-product-container wash-container">
<a href="<?php echo $base_url ?><?php echo $_sister_product->getUrlKey() ?>">
<img src="<?php echo $this->helper('catalog/image')->init($_sister_product, 'small_image', $_swatch_img)->resize(33,30); ?>" alt="" title="<?php echo $_sister_product->getAttributeText('color') ?>">
</a>
</div>
</div>
<?php endif; ?>
<?php endforeach ?>
<div class="clear"></div>
</div>
</div>
</div>
<?php endif ?>
Any help would be appreciated! :D
**Solution:**Added an if statement to check for stock availability using the isAvailable() function, shown in screenshot.
Link to screenshot: https://gyazo.com/abf07ba0373877836571858ee129cc22

PHP: link a Block

quick question, i want to link the BLOCK1 to another page. Is it possible? do anyone got a solution? i have tried abit and my brain is going mad T_T. there are 5 other blocks that i which to link to other pages aswell.
this is the code below:
<div class="midrow_block axn_block1">
<div class="mid_block_content">
<!--BLOCK1 IMAGE-->
<?php if(!empty($optimizer['block1_image']['url']) && empty($optimizer['block1_img_bg'])){ ?>
<div class="block_img"><img src="<?php echo $optimizer['block1_image']['url']; ?>" width="<?php echo $optimizer['block1_image']['width']; ?>" height="<?php echo $optimizer['block1_image']['height']; ?>" /></div>
<?php } ?>
<div class="block_content">
<h3>
<?php echo do_shortcode( $optimizer['block1_text_id']); ?>
</h3>
<?php echo do_shortcode($optimizer['block1_textarea_id']); ?>
</div>
</div>
</div>
</div>
<?php } ?>
<!--BLOCK1 END-->
Just put a <a>-Tag arount the block you want to link:
<a href="'your link here'"> <!-- start link here -->
<div class="midrow_block axn_block1">
<div class="mid_block_content">
<!--BLOCK1 IMAGE-->
<?php if(!empty($optimizer['block1_image']['url']) && empty($optimizer['block1_img_bg'])){ ?>
<div class="block_img"><img src="<?php echo $optimizer['block1_image']['url']; ?>" width="<?php echo $optimizer['block1_image']['width']; ?>" height="<?php echo $optimizer['block1_image']['height']; ?>" /></div>
<?php } ?>
<div class="block_content">
<h3>
<?php echo do_shortcode( $optimizer['block1_text_id']); ?>
</h3>
<?php echo do_shortcode($optimizer['block1_textarea_id']); ?>
</div>
</div>
</div>
</a> <!-- end link here -->
Or put it around any other part you want to have the link on. It's just basic HTML and has nothing to do with PHP.
here is the fullcode of the block. i missed to copy the top of it:
<?php if ((!empty ($optimizer['block1_text_id'])) || (!empty ($optimizer['block1_textarea_id'])) ) { ?>
<div class="midrow_block axn_block1">
<div class="mid_block_content">
<!--BLOCK1 IMAGE-->
<?php if(!empty($optimizer['block1_image']['url']) && empty($optimizer['block1_img_bg'])){ ?>
<div class="block_img"><img src="<?php echo $optimizer['block1_image']['url']; ?>" width="<?php echo $optimizer['block1_image']['width']; ?>" height="<?php echo $optimizer['block1_image']['height']; ?>" /></div>
<?php } ?>
<div class="block_content">
<h3>
<?php echo do_shortcode( $optimizer['block1_text_id']); ?>
</h3>
<?php echo do_shortcode($optimizer['block1_textarea_id']); ?>
</div>
</div>
</div>
You can use with this JS
<div class="midrow_block axn_block1" onclick="location.href='url'">content</div>
jQuery:
$("div").click(function(){
window.location=$(this).find("a").attr("href"); return false;
});

Magento - Fatal error: Call to a member function getBackend() on a non-object in app/code/core/Mage/Eav/Model/Entity/Abstract.php on line 816

Getting this error when trying to use a custom filter inside a custom theme.
I have set up the new attribute "is_featured" and its in an attribute set. I made a product that assigned it as featured (yes/no choice)
My home page (in the CMS section) is including the following "panel"
<block type="catalog/product" name="catalog.product_featured_list" template="catalog/product/featured_list.phtml" after="-"/>
featured_list.phtml looks like this:
<?php
$storeId = Mage::app()->getStore()->getId();
$_productCollection=Mage::getResourceModel('reports/product_collection')
->addAttributeToSelect(array('name', 'url', 'small_image', 'price', 'short_description'))
->addAttributeToFilter('is_featured', 1)
->addAttributeToFilter('status', 1)
->setPageSize(3)
->setStoreId($storeId)
->addStoreFilter($storeId);
$_helper = $this->helper('catalog/output');
?>
<?php if($_productCollection->count()): ?>
<section class="content-box clearfix">
<header>
<h2>Featured products</h2>
</header>
<ul class="featured-products">
<?php foreach ($_productCollection as $_product): ?>
<?php $_productNameStripped = $this->stripTags($_product->getName(), null, true); ?>
<li>
<h3>
<a href="<?php echo $_product->getProductUrl(); ?>" title="<?php echo $_productNameStripped; ?>">
<?php echo $_helper->productAttribute($_product, $_product->getName() , 'name'); ?>
</a>
</h3>
<a href="<?php echo $_product->getProductUrl(); ?>" title="<?php echo $_productNameStripped; ?>">
<img src="<?php echo $this->helper('catalog/image')->init($_product, 'small_image')->resize(212); ?>" width="200" height="200" alt="<?php echo $this->stripTags($this->getImageLabel($_product, 'small_image'), null, true) ?>" />
</a>
<div>
<ul class="clearfix">
<li>From £<?php echo number_format($_product->price, 2) ?></li>
<li>
<?php
$desct = nl2br($this->htmlEscape($_product->getShortDescription()));
$desct = strip_tags($_product->getShortDescription());
?>
<p>
<?
echo Mage::helper('core/string')->truncate($desct, '100');
?>
<a href="<?php echo $_product->getProductUrl(); ?>" title="<?php echo $_productNameStripped; ?>">
<?php echo $this->__('more details'); ?>
</a>
</p>
</li>
<li>
<form action="<?php echo $this->helper('checkout/cart')->getAddUrl($_product); //echo $this->getAddToCartUrl($_product); ?>" class="product-list-add-to-cart" method="get" id="product_addtocart_form_<?php echo $_product->getId()?>"<?php if($_product->getOptions()): ?> enctype="multipart/form-data"<?php endif; ?>>
<?php if(!$_product->isGrouped()): ?>
<label for="qty"><?php echo $this->__('Qty') ?>:</label>
<input type="text" class="input-text qty" name="qty" id="qty" maxlength="12" value="<?php echo ($this->getMinimalQty($_product)?$this->getMinimalQty($_product):1) ?>" />
<input type="hidden" name="product" value="<?php echo $_product->getId()?>" />
<?php endif; ?>
<button type="button" class="button" onclick="this.form.submit()"><span><span><?php echo $this->__('Add to Cart') ?></span></span></button>
</form>
</li>
</ul>
</div>
</li>
<?php endforeach; ?>
</ul>
</section>
<?php endif; ?>
It seems like the issue is with the collection at the start of the block. (I can remove this panel form the home page, and the site loads fine)
I'm pretty sure I have all the mentioned attributes available (is_featured looks to be the only custom one)
(this theme was inherited, so I'm not 100% versed in how it works! I'm simply copying it across)
I'm currently using 1.7, and whenever I get the "Call to a member function getBackend() on a non-object..." error, it's usually due to calling up the wrong model, or applying a filter to an attribute that doesn't exist in that collection.
After testing your code, it works without issue (mostly...) if I comment out this line:
->addAttributeToFilter('is_featured', 1)
My suggestion is to double-check that the product attribute id exists on your current installation, and it is set to the correct scope (Global / Correct Store?).
If it does exist correctly, another solution is to have the featured products selected manually, you may want to try using:
Mage::getModel('catalog/product')->getCollection()
->addAttributeToSelect(array('name', 'url', 'small_image', 'price', 'short_description'))
->addFieldToFilter('is_featured', 1)
->addFieldToFilter('status', 1)
->addStoreFilter($storeId)
->clear()->setPageSize(3)->load(); //setPageSize = How Many Products To Show
And see if that fixes it.
In my case it helped to out comment this line (Line 765)
$customer->changeResetPasswordLinkCustomerId($newResetPasswordLinkCustomerId);
in
/app/code/core/Mage/Customer/controllers/AccountController.php
I think it could be also important to check weather the update script updated your database correctly.
F.e.
upgrade-1.6.2.0.6-1.6.2.0.7.php
creates a table called rp_customer_id

Categories