Magento: get desription and image of child category - magento

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

Related

Magento hreflang Implementation

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

Magento Get Option Label by Option Code

I need to get the Custom Option Label based on either the Option ID or Option Code. Here's what I have so far:
$product_id = $product->getId(); //Get the Product Id
echo '<br /><strong>Product ID:</strong> ' . $product_id;
foreach($product->getCustomOptions() as $o) {
echo '<br />[Item ID] ' . $o->getItemId() . '<br />[Value ID] ' . $o->getId() . '<br />[Value] ' . $o->getValue() . '<br />[Code] ' . $o->getCode() . '<br />';
}
So what I have to work with at this point is:
Item ID
Value ID
Value
Code
My end goal is to use the option frontend label to see if the label is equal to "Length" as I am trying to use the "Length" value but seeing how custom options all have unique codes, the code is unreliable in terms of fetching just the "length" values for each item in the cart.
How can I use what I have to get the label for the custom options?
Thank you in advance!
You may follow these steps:
- create a test.php file in your magento root directory and run the codes below.
- it returns an array for each custom option with details.
<?php
require_once "app/Mage.php";
Mage::app('default');
echo "<pre>";
$productId = 13; // change the product id as your need
$product = Mage::getModel('catalog/product')->load($productId);
$productOptionCollection = $product->getProductOptionsCollection();
$customOptions = $productOptionCollection->getData();
print_r($customOptions);
?>

Magento order grid

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.

How can I get just the parent item and not the children in an invoice

I'm trying to make our packing slips and invoices more useful, especially when we have bundled products.
How can I show just the parent item and not the children in an invoice?
When I use $order->getAllItems() I get two lines in output:
parent-child sku
child sku
If I use $order->getAllVisibleItems() I only get the parent, which is what I want.
Parent sku
Now invoice->getAllItems with produces the two lines Parent-Child and Child
But invoice->getAllVisibleItems with produces no lines
So it will show qty and amt on the invoice twice. not good for customers or packers.
<?php
require_once '../mage1/app/Mage.php';
require_once('Zend/Pdf.php');
$app = Mage::app();
Mage::register('isSecureArea', true);
$orderId = '500000555' ;
$order = Mage::getModel('sales/order')->loadByIncrementId($orderId);
foreach ($order->getAllVisibleItems() as $item){
echo 'ITEM: ' . $item->getSku() . '<br />';
}
//vs invoice
echo 'Invoice Section <br />' ;
if ($order->hasInvoices()) {
echo 'Order has invoices' . '<br />';
foreach ($order->getInvoiceCollection() as $_eachInvoice) {
foreach ($_eachInvoice->getAllVisibleItems as $invitem){
//foreach ($_eachInvoice->getAllItems() as $invitem){
//echo 'Object <br />';
var_dump(get_object_vars($invitem));
//print_r($invitem); echo '<br /><br /><br /><br />';
echo 'INVOICE ITEM: ' . $invitem->getSku() . '<br />';
}
}
} else { echo 'no invoices' ; };
?>
Just Use $invoice->getAllVisibleItems() and for order use $order->getAllVisibleItems()
Just a word of Advise try to use get_class_method, get_class, get_class_vars for getting familiar with a class.

Wordpress query post get Image unless blank

Hi I am creating a wordpress custom page theme, I have included the code. Is there a way to dynamically add the list items. In the code I grab each image related to its post with screenshot1 screenshot2 so on... This all currently works. My problem is at the current moment if I upload 2 screen shots 3 list items will show up and the third will just be blank. So how could I dynamically add them based on the number of images in the post?
query_posts('cat=3'); /*--Query to grab Projects categorie--*/
while (have_posts()) : the_post(); /* --- loop through the posts in that categorie */
<ul>
<?php
$screen1 = get_post_meta($post->ID, 'screenshot1', true);
$screen2 = get_post_meta($post->ID, 'screenshot2', true);
$screen3 = get_post_meta($post->ID, 'screenshot3', true);
echo "<li>" . wp_get_attachment_image($screen1, 'large') . "</li>";
echo "<li>" . wp_get_attachment_image($screen2, 'large') . "</li>";
echo "<li>" . wp_get_attachment_image($screen3, 'large') . "</li>";
?>
</ul>
Loop through the possible meta values and only output it if it's not empty.
Something like:
<ul>
<?php
$i = 1;
while ($screen = get_post_meta($post->ID, 'screenshot'.$i , true)) {
echo "<li>" . wp_get_attachment_image($screen, 'large') . "</li>"
$i++;
}
?>
</ul>

Resources