Magento getUrl showing cached url - php

I have added a custom block that shows the sub categories of a parent category. as follows.
<?php
$_helper = $this->helper('catalog/output');
$_category = $this->getCurrentCategory();
echo $_helper->categoryAttribute($_category, $_category->getName(), 'name');
$categoriess = $_category->getCollection()
->addAttributeToSelect(array('name', 'thumbnail'))
->addAttributeToFilter('is_active', 1)
->addIdFilter($_category->getChildren())
?>
<ul class="subcategories">
<?php foreach ($categoriess as $categoryy): ?>
<li>
<a href="<?php echo $categoryy->getUrl() ?>"><img src="<?php echo Mage::getBaseUrl('media') . 'catalog' . DS . 'category' . DS . $categoryy->getThumbnail() ?>" alt="<?php echo $this->htmlEscape($categoryy->getName()) ?>" />
<span><?php echo $categoryy->getName() ?></span></a>
</li>
<?php endforeach; ?>
</ul>
This code correctly shows a list of the sub categories within the parent category. However when I navigate to one of these subcategories something weird happens.
The Problem
Parent category = CAR MAKES
sub categories = AUDI, FORD, VAUXHALL
Using the above code I correctly receive a list of the sub categories above. After flushing the cache, if I were to navigate to AUDI, the url will correctly generate for example:
mysite.com/CAR MAKES/Audi.html
This is fine, however if i then go back and navigate to FORD the url at the top of the page DOES display as
mysite.com/CAR MAKES/Ford.html
BUT
if I echo the url on the genrated page with this code:
<?php $currentUrl = Mage::helper('core/url')->getCurrentUrl(); echo $currentUrl; ?>
it will always display
mysite.com/CAR MAKES/Audi.html
Even if I navigate to FORD, or VAUXHALL the url that is echo'd is /Audi.html, despite the url at the top of the page displaying correctly.
I am completely lost as to why this is happening. I am trying to produce a product list for each car make and model, but once you have navigated to one make and model, it will always display those results when navigating to another make and model. Unless I flush the cache, in which case the first time I navigate to a make and model it shows correctly, and after that the error repeats.

Add this code in your layout update.
<block type="core/template" template="your-template-path.phtml" name="template-name" ><action method="setCacheLifetime"><s>null</s></action></block>

Related

Excluding a Category Name (Not Posts) From A WordPress Template

I'm currently working with a template (Point from MyThemeShop) in WordPress that has been functioning fairly well for me. I've already made some customizations but am having one issue.
The theme pulls the most recent posts and displays them in a "Featured Posts" area at the top of the page. I wanted more control over the posts displayed here so I set up a category (Homepage Featured Posts) to better define which posts should show up there. Works well, however, under this area where the recent post feed is . . . the thumbnail previews pull and display the category name in alphabetical order. So, if I have a featured post categorized under "Review" also, the preview thumbnail currently says "Homepage Featured Post" instead of "Review". I would like to exclude "Homepage Featured Post" from showing up in these thumbnails (and one other category I've also defined, "Trending Articles").
In the Index Template the code that seems to pull the category names is this:
<?php if ( has_post_thumbnail() ) { ?>
<a href="<?php the_permalink() ?>" title="<?php the_title(); ?>" rel="nofollow" id="featured-thumbnail">
<?php echo '<div class="featured-thumbnail">'; the_post_thumbnail('featured',array('title' => '')); echo '</div>'; ?>
<div class="featured-cat"><?php $category = get_the_category(); echo $category[0]->cat_name;?></div>
<?php if (function_exists('wp_review_show_total')) wp_review_show_total(true, 'latestPost-review-wrapper'); ?>
</a>
I've tried an exclusion method that sort of worked . . . but ended up not pulling ANY categories instead, using this method on line 4:
<div class="featured-cat"><?php $category = get_the_category(); echo $category[0]->cat_name !--'Homepage Featured Post';?></div>
Do I need to use specific category ID's? I am unsure how to accomplish this. Any help is much appreciated.
<?php
foreach((get_the_category()) as $cat) {
if ( $cat->cat_name!=='Homepage Featured Post' || $cat->cat_name!=='Trending Articles' ) {
echo $cat->cat_name . ' ';
}
}
?>
Can you use above code.

Can I show a list of contacts within category, on the 'List all contact categories' page in Joomla?

So in a nutshell, you have two options for displaying Contacts in Joomla:
Show all Joomla Contact Categories.
Show all Joomla Contacts in a single Category.
I want to use the first option, but merge a list underneath each Category showing the list of contacts within that category, and a link to their profile.
The simplest way I thought of this was to edit a template override of the file com_contact/categories/default_items.php
I found a point where I want the list to appear, and then copied and pasted the code from the Category view (that generates the list of contacts).
<ul>
<?php // Add list of contacts for each category
foreach ($this->items as $i => $item) : ?>
<li>
<a href="<?php echo JRoute::_(ContactHelperRoute::getContactRoute($item->slug, $item->catid)); ?>">
<?php echo $item->name; ?>
</a>
</li>
<?php endforeach; ?>
</ul>
But I am assuming I can't just copy and paste, as there needs to be an extra node added to $this->items.
At the moment, no list is being generated, just the <ul> outside the foreach loop.. but also interestingly, the <li> and the <a> IS being generated.. but linking to the current page I'm on (Probably because $item->slug is still being seen as the category).
So can anyone point me in the right direction as to how to reference the contacts within a category? All I'm after is the name and the slug/URL.
UPDATE:
I saw this in the same file (default_items.php) and although I realise it's referring to child categories... would this be a place to start for the actual contacts within the categories?
<?php if (count($item->getChildren()) > 0) :?>
<div class="collapse fade" id="category-<?php echo $item->id;?>">
<?php
$this->items[$item->id] = $item->getChildren();
$this->parent = $item;
$this->maxLevelcat--;
echo $this->loadTemplate('items');
$this->parent = $item->getParent();
$this->maxLevelcat++;
?>
</div>
<?php endif; ?>
BUMP - Does anyone have any experience with this? Or being able to call individual contacts when viewing a category? How are they linked?
For Category view in file default_children.php after tag <li... add code:
<?php
// Get Category Model data
$categoryModel = JModelLegacy::getInstance('Category', 'ContactModel', array('ignore_request' => true));
$categoryModel->setState('category.id', $child->id);
$categoryModel->setState('list.ordering', 'a.name');
$categoryModel->setState('list.direction', 'asc');
$categoryModel->setState('filter.published', 1);
$contacts = $categoryModel->getItems();
?>
For Custom Fields add this after previus code:
JLoader::register('FieldsHelper', JPATH_ADMINISTRATOR . '/components/com_fields/helpers/fields.php');
foreach($contacts as $contactItem) {
$currentContFields[] = FieldsHelper::getFields('com_contact.contact', $contactItem, true);
}

Retrieve Url of category on Magento when creating a custom menu

I am trying to create a custom drop-down-menu with only few specific categories and their respective subcategories. So far I managed to retrieve the Names of the subcategories but links won't work.
I also need to make the main category retrieve its own name and URL automaticly in case it is changed on the back-end of Magento. In this case the category id is 265.
The website I am working on is www.personalproducts4u.co.uk
<li class="eight">Hotel Products
<?php $children = Mage::getModel('catalog/category')->getCategories(265); ?>
<ul>
<?php foreach ($children as $category): ?>
<li>
<a href="<?php echo $category->getUrl ?>">
<?php echo $category->getName(); ?>
</a>
</li>
<?php endforeach; ?>
</ul>
</li>
The problem is that the $children collection is of type Varien_Data_Tree_Node_Collection and its elements respectively are of type Varien_Data_Tree_Node. Calling getUrl() on them will return null, they are not Mage_Catalog_Model_Category objects. However, you can retrieve their request path (url) by calling:
$category->getRequestPath();
Alternatively you can load the category object by calling:
$cat = Mage::getModel('catalog/category')->load($category->getEntityId());
And then use the $cat->getUrl() call. This loading will add an extra overhead though.

Magento Product Listing by Category ID

I have a magento (1.5) store and a wordpress (3.2) blog.
The wordpress blogs acts as the main site and the home-page index.
Using Mage-Enabler, I have integrated wordpress and magento together and I am able to pull the checkout block, quick links and all the files.
My question is that I would like to display the top-sellers categories on the home-page. I would usually do this with the XML in the CMS.
e.g.
{{block type="catalog/product_list" category_id="your_category_id" template="catalog/product/list.phtml"}}
But this is not possible in this instance as the store home-page is not seen e.g when a user clicks on the shop-online button on the navigation it takes them onto the category lander page showing all the categories and a search.
My logic has lead me to do this via PHP e.g
<?php
$categoryId = 123; // a category id that you can get from admin
$category = Mage::getModel('catalog/category')->load($category_Id);
?>
... this would be inside a copy of the product > list.phtml page.
Is this possible to pull products in a list via a specfic category via PHP templates rather than the block types XML in the admin?
Thanks
Cameron
This oughtta do what you need:
<?php
$categoryid = 12;
$category = Mage::getModel('catalog/category');
$category->load($categoryid);
$collection = $category->getProductCollection();
$collection->addAttributeToSelect('*');
foreach ($collection as $_product) { ?>
<img src="<?php echo $this->helper('catalog/image')->init($_product, 'small_image')->resize(200); ?>" width="200" height="200" alt="" /> <?php echo $_product->getName(); ?>
<?php } ?>

How to get a category listing from Magento?

I want to create a page in Magento that shows a visual representation of the categories.. example
CATEGORY
product 1
product 2
ANOTHER CATEGORY
product 3
My problem is, their database is organised very differently to what I've seen in the past. They have tables dedicated to data types like varchar, int, etc. I assume this is for performance or similar.
I haven't found a way to use MySQL to query the database and get a list of categories. I'd then like to match these categories to products, to get a listing of products for each category. Unfortunately Magento seems to make this very difficult.
Also I have not found a method that will work from within a page block.. I have created showcase.phtml and put it in the XML layout and it displays and runs its PHP code. I was hoping for something easy like looping through $this->getAllCategories() and then a nested loop inside with something like $category->getChildProducts().
Can anyone help me?
From code found in an SEO related class (Mage_Catalog_Block_Seo_Sitemap_Category)
$helper = Mage::helper('catalog/category');
$collection = $helper->getStoreCategories('name', true, false);
$array = $helper->getStoreCategories('name', false, false);
Try to forget that it's a database that's powering your store, and instead concentrate on using the objects that the Magento system provides.
For example, I had no no idea how to get a list of categories. However, I grepped through the Mage codebase with
grep -i -r -E 'class.+?category'
Which returned a list of around 30 classes. Scrolling through those, it was relatively easy to guess which objects might have methods or need to make method calls that would grab the categories.
Hey something like this might help you, I've adapted it slightly to answer your question more specifically.
$h3h3=Mage::getModel('catalog/category')->load(5); // YOU NEED TO CHANGE 5 TO THE ID OF YOUR CATEGORY
$h3h3=$h3h3->getProductCollection();
foreach($h3h3->getAllIds() as $lol)
{
$_product=Mage::getModel('catalog/product')->load($lol);
print $_product->getName()."<br/>";
}
I adapted this from Paul Whipp's website:
SELECT e.entity_id AS 'entity_id', vn.value AS 'name'
FROM catalog_category_entity e
LEFT JOIN catalog_category_entity_varchar vn
ON e.entity_id = vn.entity_id AND vn.attribute_id = 33
ORDER BY entity_id;
This will provide you with the catalog category IDs.
Here's a quick example
$categories = Mage::getModel('catalog/category')->getCollection()
->addAttributeToSelect('name')
->addAttributeToSelect('url_key')
->addAttributeToSelect('my_attribute')
->setLoadProductCount(true)
->addAttributeToFilter('is_active',array('eq'=>true))
->load();
Thanks a lot. Really helps. To get the game, make a loop and then getName()
foreach ($collection as $cat):
echo $cat->getName();
endforeach;
I used this in /app/design/frontend/default/default/template/catalog/product/feature.xml
<?php
/**
* Home page Featured Product list template
*
* #see Mage_Catalog_Block_Product_List
*/
?>
<?php
if (!is_null($this->_productCollection)) {
$_origCollection = $this->_productCollection;
$this->setCollection(null);
}
$this->setCategoryId(16);
$_productCollection=$this->getLoadedProductCollection() ?>
<?php if($_productCollection->count()): ?>
<div class="featured-products">
<h2><?php echo $this->__('Featured Products') ?></h2>
<?php foreach ($_productCollection as $_product): ?>
<div>
<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(150, 50); ?>" alt="<?php echo $this->htmlEscape($this->getImageLabel($_product, 'small_image')) ?>" />
<h3 class="product-name"><?php echo $this->htmlEscape($_product->getName())?></h3>
<?php echo nl2br($this->htmlEscape($_product->getShortDescription())) ?>
</a>
</div>
<?php endforeach; ?>
</div>
<?php endif; ?>
category Listing block:
<?php
$categories = Mage::getModel('catalog/category')->load(2)->getChildren();
$catIds = explode(',',$cats);
?>
<ul>
<?php foreach($catIds as $catId): ?>
<li>
<?php
$category = Mage::getModel('catalog/category')->load($catId);
echo $category->getName();
$subCats = Mage::getModel('catalog/category')->load($category->getId())->getChildren();
$subCatIds = explode(',',$subCats);
?>
<?php if(count($subCatIds) > 1):?>
<ul>
<?php foreach($subCatIds as $subCat) :?>
<li>
<?php
$subCategory = Mage::getModel('catalog/category')->load($subCat);
echo $subCategory->getName();
?>
</li>
<?php endforeach;?>
</ul>
<?php endif; ?>
</li>
<?php endforeach; ?>
</ul>
I made this little video on how I create custom category listing blocks with Magento.
I am sure there are better ways of achieving this or even something I could have done better, but it's just my method. I only created this it in hopes that it helps explain somethings to some people out there.
Magento Custom Category Listing Tutorial

Categories