show subcategories of parent category when no subcategories in category page - magento-1.9

In magento when we click on a category it takes us to category page and shows
(if exists) subcategories of the clicked category in layered navigation on left.But when we click on a category that has no subcategories it show empty on that category page in layered navigation on left. What I want is to show subcategories of the parent if the current category has no subcategories.For this I have done the following but did not work for me.
I have tried to add the following to to
app/code/core/Mage/Catalog/Model/Layer/Filter/Category.php
if(count($categoty->getChildrenCategories())){
$categories = $categoty->getChildrenCategories();
}else{
$categories = $categoty->getParentCategory()->getChildrenCategories();
}
and removed line #163
$categories = $categoty->getChildrenCategories();
Please suggest me a solution. Any help will be greatly appreciated.

I have found the solution. I have to put the above login in left.phtml
app/design/frontend/theme/template/catalog/navigation/left.phtml
$categoty = Mage::registry('current_category');
$categories = $category->getChildrenCategories();
//$_categories = $this->getCurrentChildCategories();
if(count($categoty->getChildrenCategories())){
$_categories = $categoty->getChildrenCategories();
}else{
$_categories = $categoty->getParentCategory()->getChildrenCategories();
}
and it worked like a charm! Hope it will help someone else!

Related

How to show specific attribute under product name on Woocommerce Shop Page

I've been struggling to find information about how to achieve this:
I want for one specific attribute (Brand), to be visible and present under the product name on the Shop Page.
How can I achieve this?
Theme: Zakra
Thanks in advance.
Alex.
Use the below code display the attribute below the product title on the shop page.
I guess the taxonomy name for the brand should be (pa_brand). If the name is different then replace the taxonomy name of BRAND and replace(pa_brand) it in the below code.
add_action( 'woocommerce_after_shop_loop_item_title', 'display_brand_attribute', 5 );
function display_brand_attribute() {
global $product;
$taxonomy = 'pa_brand';
echo '<span class="attribute-brand">' . $product->get_attribute($taxonomy) . '</span>';
}

How to add pagination for wishlist products in magento 1.9

I am trying to get products on the page-wise for wishlist collection. I was getting a count for product collections in the below code. I have set the page size to one but it's showing the product count two. Please help me to fix this. Thanks.
$wishlist1 = Mage::getModel('wishlist/wishlist')->loadByCustomer($customer, true)
->setCurPage($page_num)
->setPageSize(1)
->load();
foreach($wishlist1->getItemCollection() as $product){
$_product = Mage::getModel('catalog/product')->load($product['product_id']);//product id here
echo $product['product_id'];
}
Please try with this code:
<?php
$customer = Mage::getModel("customer/customer")->setWebsiteId(Mage::app()->getStore()->getWebsiteId())->loadByEmail('mohit#streammarket.co.uk');
$wishlistItems = Mage::getModel('wishlist/item')->getCollection()
->setWebsiteId($customer->getWebsiteId())
->setCustomerGroupId($customer->getGroupId())
->setCurPage(1)
->setPageSize(1)
->load();
?>

Get number of swatches of an attribute that has a product

I have configurable products with a swatch attribute, in this case is color.
I need to know how many colors (the number) has a product, or maybe how many single products compose this configurable product.
As a matter of fact, I need to know when there is more than one color in a product.
Finally I found it, maybe it will help somebody in the future:
$productAttributeOptions = $_product->getTypeInstance(true)->getConfigurableAttributesAsArray($_product);
//in this case the attribute color that I needed is in [0] position
$available_colors = sizeof($productAttributeOptions[0]["values"]);
if ($available_colors >1):
//custom code
endif;
Edit: This solution works for one product, or at least for a few products, but if you need it in a list of products it is really slow to run this code in each of the products. Sometimes it drives to a timeout and closes the db connections, so the web crashes with an error.
Finally I got a solution, maybe it is not the best one, but it is pretty fast compared to the one I was using before:
$_idsForTheQuery = $_productCollection->getAllIds();
$read = Mage::getSingleton('core/resource')->getConnection('core_read');
$sql_query = "SELECT parent_id, COUNT(parent_id) AS colors FROM
(SELECT cpr.parent_id FROM `eav_attribute` a
LEFT JOIN `catalog_product_entity_int` cpei ON cpei.attribute_id=a.attribute_id
LEFT JOIN `catalog_product_relation` cpr ON cpr.child_id=cpei.entity_id
WHERE attribute_code = 'color' AND cpr.parent_id IN (".implode (", ", $_idsForTheQuery).")
GROUP BY cpr.parent_id, cpei.value) colors
GROUP BY parent_id";
$results = $read->query($sql_query);
$number_of_colors_by_id_array = array();
foreach($results as $r)
{
$number_of_colors_by_id_array[$r["parent_id"]] = $r["colors"];
}
and then in the foreach loop of the products
<?php if ($number_of_colors_by_id_array[$_product->getId()]>1): ?>
<div class="aditional-colors-message">
<?php echo __('more colors available'); ?>
</div>
<?php endif; ?>

MediaWiki: changing the label of a category at the bottom of the page

In mediawiki, is it possible to change the label of a 'Category' at the bottom of an article.
For example for the following article:
=Paris=
blablablablablabla
[[Category:place_id]]
I'd like to see something more verbose like (the example below doesn't work):
=Paris=
blablablablablabla
[[Category:place_id|France]]
Note: I don't want to use a 'redirect' and I want to keep my strange ids because they are linked to an external database.
I do not think mediawiki is supporting this feature.
However, how about using:
[[Category:France]]
in your page, and set it into the category named with your id? France would just be a subcategory of "place_id", and you could use more terms all linked to the parent category. For this, you just need to edit the category page for "France", inserting:
[[Category:place_id]]
An alternative would be to put your page in both categories, but in this case, the id would still be displayed:
[[Category:place_id]]
[[Category:France]]
You could do this with an OutputPageMakeCategoryLinks hook. Alas, the interface for that hook seems to be a bit inconvenient — as far as I can tell, it's pretty much only good for replacing the standard category link generation code entirely. Still, you could do that is you want:
function myOutputPageMakeCategoryLinks( &$out, $categories, &$links ) {
foreach ( $categories as $category => $type ) {
$title = Title::makeTitleSafe( NS_CATEGORY, $category );
$text = $title->getText();
if ( $text == 'Place id' ) {
// set $text to something else
}
$links[$type][] = Linker::link( $title, htmlspecialchars( $text ) );
}
return false; // skip default link generation
}
$wgHooks['OutputPageMakeCategoryLinks'][] = 'myOutputPageMakeCategoryLinks';
(The code above is based on the default category link generation code in OutputPage.php, somewhat simplified; I assume you're not using language variant conversion on your wiki, so I removed the parts that deal with that. Note that this code is untested! Use at your own risk.)

MySQL question: Appending text to a wordpress post, but only if it's in a certain category

I need to amend (via CONCAT, presumably) something to every wordpress post if it belongs to a certain category (say, category ID 7), but I'm struggling to get it to work.
To test, I'm first trying to select all the relevant posts. So far, I have the following:
SELECT post_title
FROM cruise_wp_posts
LEFT JOIN cruise_wp_term_relationships
ON cruise_wp_term_relationships.object_id = cruise_wp_posts.ID
WHERE term_taxonomy_id = 87;
However, it only lists posts that are only in category 87 - I need all posts that are in category 87 (and possibly other categories too)
I'm a MySQL newbie, and this is really breaking my brain.
Any pointers would be passionately welcomed.
The best way to do it is to filter it in as needed. This way the addition is made everywhere the_content is used and not just in the templates you modify.
<?php
function my_content_concat($the_content) {
if (in_category(7)) {
$the_content .= '<br /><br />foo!';
}
return $the_content;
}
add_filter('the_content', 'my_content_concat', 9);
?>
in_category can take the id, name or slug of your target category.
I put the filter at 9 so that it runs before WordPress texturizes the content. If you don't need that run it at 11.
Why not just use get_the_category( $id ) and ammend the text when you output the post?
$cat = get_the_category( $postID );
if ($cat == 7) {
//Add text here
}