Posting in the Magento forums has been disabled pending the implementation of a new and improved forum solution which should better serve the community.

For new questions please post at magento.stackexchange.com, the community-run support site for the Magento community. We will be providing updates on the new forum solution soon. For questions or concerns please email community@magento.com.

Magento Forum

Error on getProductCollection
 
blockhead
Jr. Member
 
Total Posts:  20
Joined:  2008-05-12
 

I added this code on product view.phtml but i have been having error problems. i just wana get a list of products in a category. basicaly the error starts at “foreach ($_main_category_sub->getProductCollection() as $_main_category_sub_products):” Hope somebody can help me.

<div id="messages_product_view"><?php echo $this->getMessagesBlock()->getGroupedHtml() ?></div>
<?php 
/* this is the code that i added [start] */
$_product=$this->getProduct();


$_main_categories=$this->helper('catalog/category')->getStoreCategories();
if(
$_main_categories->count()):
    foreach (
$_main_categories as $_main_category):
        if(
$_main_category->getIsActive()):
            if(
$_main_category->getId() == 18){
                   
foreach ($_main_category->getChildren() as $_main_category_sub):
                   
                       foreach (
$_main_category_sub->getProductCollection() as $_main_category_sub_products):
                           echo 
$_main_category_sub_products->getName "<br />";
                    endforeach;
                endforeach;
            
}
        
endif;
    endforeach;
endif; 

/* this is the code that i added [end] */

/*
$_category = $this->helper('catalog/data')->getCategory();

$_category_url = $this->helper('catalog/category')->getCategoryUrl($_category);

print_r($_category_url);*/

?>
<div class="product-info-box">
    <
div class="product-essential">

        <
div class="product-img-box">
            
<?php echo $this->getChildHtml('media'?>
        
</div>

        <
div class="product-shop">
            
<?php 
            
            
            
            ?>
            
<h3 class="product-name"><?php echo $this->htmlEscape($_product->getName()) ?></h3>
    
            
<?php if ($this->canEmailToFriend()): ?>
                
<a href="<?php echo $this->helper('catalog/product')->getEmailToFriendUrl($_product) ?>"><?php echo $this->__('Email to a Friend'?></a><br />
            
<?php endif; ?>

            <?php 
echo $this->helper('review/product')->getSummaryHtml($_productnull1?>
            

            
<form action="<?php echo $this->getAddToCartUrl($_product) ?>" method="post" id="product_addtocart_form">
            <
fieldset>
            <
input type="hidden" name="product" value="<?php echo $_product->getId() ?>" />
            <
input type="hidden" name="related_product" id="related-products-field" value="" />
            </
fieldset>

                
<?php echo $this->getChildHtml('alert_urls'?>

                <?php 
echo $this->getChildHtml('product_type_data'?>

            
</form>

            <
script type="text/javascript">
                var 
productAddToCartForm = new VarienForm('product_addtocart_form');
                
productAddToCartForm.submit = function() {
                    
if(this.validator.validate()) {
                        this
.form.submit();
                    
}
                }
.bind(productAddToCartForm);
            
</script>

            
<div class="clear"></div>
            <
div class="divider"></div>

            <
h4>Product Description</h4>
            <
div class="short-description"><?php echo nl2br($_product->getShortDescription()) ?></div>

        </
div>
    </
div>

    <
div class="product-collateral">

        <
div class="collateral-box">
            <
div class="head">
                <
h4>Suggested Use</h4>
            </
div>
            <
div class="product-specs"><?php echo nl2br($_product->getDescription()) ?></div>
        </
div>

        
<?php if($_additional $this->getAdditionalData()): ?>
        
<div class="collateral-box attribute-specs">
            <
div class="head">
                <
h4><?php echo $this->__('Additional Information'?></h4>
            </
div>
            <
table cellspacing="0" class="data-table" id="product-attribute-specs-table">
            
<?php foreach ($_additional as $_data): ?>
            
<tr>
                <
td class="label"><?php echo $_data['label'] ?></td>
                <
td class="data"><?php echo $_data['value'] ?></td>
            </
tr>
            
<?php endforeach; ?>
            
</table>
            <
script type="text/javascript">decorateTable('product-attribute-specs-table')</script>
        
<[code]/div>
        
<?php endif; ?>

        <?php 
echo $this->getChildHtml('upsell_products'?>
        <?php 
echo $this->getChildHtml('product_additional_data'?>
    
</div>
</
div>

This is the error

Fatal errorCall to a member function setCurrentCategory() on a non-object in /home/lavender/public_html/app/design/frontend/default/default/template/catalog/product/view.phtml on line 143
 
Magento Community Magento Community
Magento Community
Magento Community
 
Vofka
Jr. Member
 
Avatar
Total Posts:  30
Joined:  2008-06-19
 

Protect your $_main_category_sub with ‘if’ statement

if ($_main_category_sub->getProductCollection())
foreach(
$_main_category_sub->getProductCollection() as $something)
{
//my code
}
 
Magento Community Magento Community
Magento Community
Magento Community
 
blockhead
Jr. Member
 
Total Posts:  20
Joined:  2008-05-12
 

Yes it works but i still have problems with getProductCollection. it seems that it doesnt give me the product list for that specific category.

This is the script with the revision that you make. thanks in advance.

$_main_categories=$this->helper('catalog/category')->getStoreCategories();
if(
$_main_categories->count()):
    foreach (
$_main_categories as $_main_category):
        if(
$_main_category->getIsActive()):
            if(
$_main_category->getId() == 18){
                   
foreach ($_main_category->getChildren() as $_main_category_sub):
                           echo 
"Category:" $_main_category_sub->getName() . "<br />";
                           
print_r($_main_category_sub);
                           if(
$_main_category_sub->getProductCollection()){
                           
foreach ($_main_category_sub->getProductCollection() as $_main_category_sub_products):
                               echo 
"Product:" $_main_category_sub_products->getName() . "<br />";
                            endforeach;
                           
}
                
endforeach;
            
}
        
endif;
    endforeach;
endif; 
/*
 
Magento Community Magento Community
Magento Community
Magento Community
 
brendanb
Mentor
 
Total Posts:  1093
Joined:  2008-07-16
London, United Kingdom
 

Hey there,

did you get this to work?

cheers

 
Magento Community Magento Community
Magento Community
Magento Community
 
blockhead
Jr. Member
 
Total Posts:  20
Joined:  2008-05-12
 

nope not at all :(

 
Magento Community Magento Community
Magento Community
Magento Community
 
marcM
Jr. Member
 
Total Posts:  7
Joined:  2008-04-11
 

sounds like issue similar to this post
http://www.magentocommerce.com/boards/viewthread/13780
where must call load on collection to fill it. 

Hope this helps

 
Magento Community Magento Community
Magento Community
Magento Community
Magento Community
Magento Community
Back to top