Using code from a previous discussion (link:https://magento.stackexchange.com/questions/12504/how-to-add-hreflang-tags-or-other-meta-tags-to-pages-in-magento), I was able to implement the hreflang links into our Magento site.
Here is the code that worked for me:
<?php foreach (Mage::app()->getWebsites() as $website) {
foreach ($website->getGroups() as $group) {
$stores = $group->getStores();
foreach ($stores as $store) {
$storeId = $store->getId();
$storeCode = substr(Mage::getStoreConfig('general/locale/code', $storeId),0,2);
if (Mage::registry('product')) {
$productId = Mage::registry('product')->getId();
$base_url = Mage::app()->getStore($storeId)->getBaseUrl(Mage_Core_Model_Store::URL_TYPE_LINK);
$url = Mage::getModel('catalog/product')->setStoreId($storeId)->load($productId)->getProductUrl();
$url = preg_replace('/\?.*/', '', $url);
echo '<link rel="alternate" hreflang="' . $storeCode . '" href="' . $url . '"/>';}
elseif(Mage::registry('current_category')) {
$categoryId = Mage::registry('current_category')->getId();
$base_url = Mage::app()->getStore($storeId)->getBaseUrl(Mage_Core_Model_Store::URL_TYPE_LINK);
$url = Mage::getModel('catalog/category')->setStoreId($storeId)->load($categoryId)->getUrlPath();
echo '<link rel="alternate" hreflang="' . $storeCode . '" href="' . $base_url . $url . '"/>' . "\n";
}}}}
?>
<?php
$storeId = 1;
if (Mage::registry('product')) {
$productId = Mage::registry('product')->getId();
$base_url = Mage::app()->getStore($storeId)->getBaseUrl(Mage_Core_Model_Store::URL_TYPE_LINK);
$url = Mage::getModel('catalog/product')->setStoreId($storeId)->load($productId)->getProductUrl();
$url = preg_replace('/\?.*/', '', $url);
echo '<link rel="alternate" hreflang="x-default" href="' . $url . '"/>';
}
elseif(Mage::registry('current_category')) {
$categoryId = Mage::registry('current_category')->getId();
$base_url = Mage::app()->getStore($storeId)->getBaseUrl(Mage_Core_Model_Store::URL_TYPE_LINK);
$url = Mage::getModel('catalog/category')->setStoreId($storeId)->load($categoryId)->getUrlPath();
echo '<link rel="alternate" hreflang="x-default" href="' . $base_url . $url . '"/>' . "\n";
}
?>
I'm having an issue with layered navigation URL's and canonical links disappearing on category pages.
Is there something I can add to this code to make sure that layered navigation URL's that contain "?" after .html get written as shown in the browsers address bar?
Also, on these types of category pages the canonical link does not show.
The code works perfectly on product pages.
Any help would be greatly appreciated!
Thanks in advance.
To solve this I need to know:
Where have you put this code? Is executed in categories?
Is the current_category empty?
Regards.
I missunderstood the question. Could you show an example?
The code only shows the category base URLs. When applying filters the module change the URL adding parameters filtered but your code doesn't have those parameters.
Usually people do not position the filters because it gives many problems of duplicate content. Take a look at this link:
https://amasty.com/blog/magento-layered-navigation-best-settings-for-seo/
In my opinion, I would left canonical URLs in categories using its base URL and disallow to index anything layered because could give duplicated content issues.
Kind regards
Related
I want to show filter of category on submenu, my code works!!
My problem is that if page are already filtered, my code does not return the options
I believe it has to do something in the code that bypasses the filter page and again bring the options in the submenu even if already have the filter on page
HTML of submenu:
{{block type="core/template" category="3" template="page/html/icons_submenu.phtml"}}
Content of page icons_submenu.phtml:
<?php
$layer = Mage::getModel("catalog/layer");
$category = Mage::getModel('catalog/category')->load($this->getCategory());
$layer->setCurrentCategory($category);
$attributes = $layer->getFilterableAttributes();
foreach ($attributes as $attribute) {
if ($attribute->getAttributeCode() == 'color') {
$filterBlockName = 'catalog/layer_filter_attribute';
$result = Mage::app()->getLayout()->createBlock($filterBlockName)->setLayer($layer)->setAttributeModel($attribute)->init();
echo '<strong>Color:</strong><br />';
foreach($result->getItems() as $option) {
echo ' ' . $option->getValue() . ' - ' . $option->getLabel() . '<br />';
}
}
}
?>
Example:
I would really suggest you to actually move all that logic into a proper module, a proper block and a proper model and not in a template like you are doing right now.
If you actually want further help for that, feel free to ask, making something according to the coding guide lines of Magento would make you even happier of your job, I can assure you.
This being said, what you actually want is a current filter model based on the current category and a specify attribute.
You don't need to go by the block catalog/layer_filter_attribute in a way to do this, you can directly go by the model based on the layer you already load.
So, this way of doing it should work, although it should not be in a template or view, once again :
<?php
$category = Mage::getModel('catalog/category')
->load($this->getCategory());
$layer = Mage::getModel('catalog/layer')
->setCurrentCategory($category);
$attributes = $layer->getFilterableAttributes();
foreach ($attributes as $attribute) {
if ($attribute->getAttributeCode() == 'color') {
// $filterBlockName = 'catalog/layer_filter_attribute';
/** This is actually your only problem in your code **/
// $result = Mage::app()->getLayout()->createBlock($filterBlockName)->setLayer($layer)->setAttributeModel($attribute)->init();
/** But would work with this line **/
$result = Mage::getModel('catalog/layer_filter_attribute')
->setLayer($layer)
->setAttributeModel($attribute);
echo '<strong>Color:</strong><br />';
foreach($result->getItems() as $option) {
echo ' ' . $option->getValue() . ' - ' . $option->getLabel() . '<br />';
}
}
}
?>
Then you can see it still works based on only the colours I have in the current category
But also when the category is already filtered on a specific colour
Hi I need to display Order Details grid in my Magento FRONT END page. I tried different ways, but nothing worked fine.
Also how to find <?php echo $this->getChildHtml('customer.account.dashboard.extra') ?> block.
Any idea please.
Here is the quick way,
<?php
require_once('app/Mage.php'); //Path to Magento
umask(0);
Mage::app()->getStore();
echo '<pre>';
$orders = Mage::getModel('sales/order')->getCollection()
//->addFieldToFilter('status', 'complete')
->addAttributeToSelect('customer_email')
->addAttributeToSelect('status')
->addAttributeToSelect('increment_id')
;
//echo $orders->getSelect();
//exit;
foreach ($orders as $order) {
$email = $order->getCustomerEmail();
echo $order->getIncrementId() . ": '" . $order->getStatus() . "', " . $email . "\n";
}
It fetches all order details. Please comment here if you have any doubt.
Attempting to work out a solution to incorporate rel="alternate" tags into a multi-language Mage store. Note: categories and products have customised URL keys for each store
The below code works properly and gives me the correct full URL for a product, in the format of http://www.storename.com/storecode/product.phtml
$url = Mage::getModel('catalog/product')->setStoreId($storeId)->load($product->getId())->getProductUrl();
But the below does not work for categories. It gives the same store code for each URL, instead of the correct store code for each store in the foreach:
$url = Mage::getModel('catalog/category')->setStoreId($storeId)->load($category->getId())->getUrl();
In head.phtml I've so far got:
<?php
$url = '';
$pageType = Mage::app()->getFrontController()->getRequest()->getControllerName();
foreach (Mage::app()->getWebsites() as $website) {
foreach ($website->getGroups() as $group) {
$stores = $group->getStores();
foreach ($stores as $store) {
$storeId = $store->getStoreId();
switch ($pageType) {
case 'product':
$product = Mage::registry('current_product');
$url = Mage::getModel('catalog/product')->setStoreId($storeId)->load($product->getId())->getProductUrl();
break;
case 'category':
$category = Mage::registry('current_category');
/* Below is the code that isn't working properly */
$url = Mage::getModel('catalog/category')->setStoreId($storeId)->load($category->getId())->getUrl();
break;
default:
$url = 'def';
break;
}
echo '<link rel="alternate" href="' . $url . '" hreflang="' . $store->getConfig('general/locale/code') . '"/>' . "\n";
}
}
}
?>
Any advice or help would be appreciated.
Here's the quickest solution I found:
$url = $store->getCurrentUrl(false) . Mage::getModel('catalog/category')->setStoreId($storeId)->load($category->getId())->getUrlKey() . '.html';
i am trying to built category tree for the categories and sub categories in custom admin module, if possible to override the default category tree present in edit tab of product.
Below is the code which i am working, it is able to build category tree but it lack the checkbox ability. any sugestion would be appreciated
<?php
$rootcatId= Mage::app()->getStore()->getRootCategoryId();
$categories = Mage::getModel('catalog/category')->getCategories($rootcatId);
function get_categories($categories) {
$array= '<ul>';
foreach($categories as $category) {
$cat = Mage::getModel('catalog/category')->load($category->getId());
$count = $cat->getProductCount();
$array .= '<li>'.
'<a href="' . Mage::getUrl($cat->getUrlPath()). '">' .
$category->getName() . "(".$count.")</a>\n";
if($category->hasChildren()) {
$children = Mage::getModel('catalog/category')->getCategories($category->getId());
$array .= get_categories($children);
}
$array .= '</li>';
}
return $array . '</ul>';
}
echo get_categories($categories); ?>
Please clarify your question as it's bad idea to override core functionality because same function is used by different modules instead you can check functionality of these
app/design/adminhtml/default/default/template/catalog/product/edit/categories.phtml app/code/core/Mage/Adminhtml/Block/Catalog/Product/Edit/Tab/Categories.php
and then reflect these to your template files
I'm using this code to display the child categories of a specific category in Magento:
$parentCategoryId = 3;
foreach Mage::getModel('catalog/category')->load($parentCategoryId)->getChildrenCategories() as $childCategory) {
echo $childCategory->getName() . '<br />';
echo $childCategory->getUrl() . '<br />';
}
That works quiet fine. But now I like to display description and category image of these child categories. I've tried it with the descriptions and added this line:
echo $childCategory->getDescription() . '<br />';
But the output is empty. Does anybody has an idea what I can do to display the description and later the category image?
Thank you for your help.
Please try this one, its working fine at my end
<?php
$parentCategoryId = 10;
$categories = Mage::getModel('catalog/category')->load($parentCategoryId)->getChildren();
$catArray = explode(',', $categories);
foreach($catArray as $child){
$_child = Mage::getModel( 'catalog/category' )->load( $child );
echo $_child->getName() . '<br />';
echo $_child->getUrl() . '<br />';
echo $_child->getDescription() . '<br />';
}
?>
we will not get category description attribute from this function getChildrenCategories(). A better explanation about this function can be found here on Stackoverflow answer