1

If I use the navigation menu to arrive at a product "detail" page then it all shows up nicely. But if I use the search to arrive at a product "detail" page then the picture and title show up but no pricing or description show up. This, I now know, has to do with the search view not providing a category ID.

Here it is when you use the search feature to arrive at the page. Testing this feature Here it is when you use the regular navigation to arrive at the page. enter image description here Here is the error message that I get on that page when I turn on error reporting Fatal error: Call to a member function getId() on a non-object in /../../public_html/store/app/design/frontend/rwd/default/template/catalog/product/view/type/availability/default.phtml on line 38

The code throwing the error is

$currentCategory = Mage::registry('current_category');

on line 38 but I don't understand how to fix it so the code responds on search and product pages.

<!-- Stock Status -->
<div class="stock status">
<?php 
    $manageStock = $_product->getStockItem()->getManageStock();

    $inventoryQuantity = (int)Mage::getModel('cataloginventory/stock_item')->loadByProduct($_product)->getQty();

    $currentCategory = Mage::registry('current_category');
    $curID = $currentCategory->getId();

    $one = 71;
    $two = 8;
    $three = 4;
    if ($manageStock == 1) // If 'Manage Stock' is set to YES
        {
        // If 'Qty' is less than or equal to zero
        /*if(($inventoryQuantity <= 0) && ($curID == $one || $curID == $two || $curID == $three)) { ?> */
        if(($inventoryQuantity <= 0) && (in_array($curID, array($one, $two, $three)))) { ?> 
            <div class="availability in-stock">
                <span>
                <?php echo $this->__('Availability dependent on shipping method.'); ?>
                </span> 
            </div>
        <?php } 
        // If 'Qty' is less than or equal to zero
        if(($inventoryQuantity <= 0) && ($curID != $one && $curID != $two && $curID != $three)) { ?> 
            <div class="availability out-of-stock">
                <span>
                    <?php echo $this->__('Out of Stock'); ?>
                    <?php echo $curID; ?>
                </span> 
            </div>
        <?php }
        else if($inventoryQuantity > 0) { ?> 
            <div class="availability in-stock">
                <span>
                    <?= (int) Mage::getModel('cataloginventory/stock_item')->loadByProduct($_product)->getQty()?><?php echo $this->__(' In Stock'); ?>
                </span>
            </div> 
        <?php } 
        }
    ?>
    </div>
<!-- // Stock Status -->

Any thoughts? It's been driving me crazy!

Thanks in advance!

1

When you are in a search result context there is no category.
So this code $currentCategory = Mage::registry('current_category'); returns null and later you try to call a method on a null variable $curID = $currentCategory->getId();.
PHP does not agree with this.

On a separate topic this is wrong for so many reasons:

if(($inventoryQuantity <= 0) && ($curID == $one || $curID == $two || $curID == $three)) { ?> 
   <div class="availability in-stock">

First if all you are hard-coding some category ids.
Second, you have a long condition that looks ugly.

($curID == $one || $curID == $two || $curID == $three)

can simply be written as

(in_array($curID, array($one, $two, $three)))

Third...I can't seam to understand the logic of the code.. So you are displaying the availability only if the inventory qty is 0 or below and the current category is one of the 3 listed above?

| improve this answer | |
  • Yeah it's rough, I agree. The logic is that I'm displaying a custom availability message for products that are on back order (not really actually back ordered because it's a drop ship service) and only in a certain couple categories. – new2programming Mar 12 '15 at 13:27
  • Any thoughts then on how to make this whole thing work properly? – new2programming Mar 12 '15 at 13:44
  • Instead of checking the current category id, get the category ids from the product with $_product->getCategoryIds(). Then see if one of the ids matches your ids. – Marius Mar 12 '15 at 13:48
  • ok cool, I can see how that would help. This leaves me trying to figure out how to grab the "last" categoryID though because I'm only referring to the lowest category level and $curID = $_product->getCategoryIds(); doesn't return an int. Could you help me format that statement properly? $_product->getCategoryIds(); $curID = "last category ID code" – new2programming Mar 12 '15 at 13:54
1

I was just writing my answer but Marius got there first.

But, I'll just piggyback on his answer:

Search for: Mage::register('current_category' to see where the code in your base tries to set current_category. Ideally, templates on product page shouldn't expect current_category to exist in the registry but I guess you have some customization to your code. That search I mentioned would help.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.