Magento - How to get attributes of a child product of a bundled item - magento

I need to retrieve a simple text attribute from a child product belonging to a bundled product if it exists. This needs to be done on the shopping cart page. The below is the code I've been messing around with to see what I can retrieve.
The file is template/checkout/cart/item/default.phtml in the theme folder.
$_item = $this->getItem();
$_product = $this->getProduct();
$_product = Mage::getModel('catalog/product')->load($_product->getId());
$isVisibleProduct = $_item->getProduct()->isVisibleInSiteVisibility();
$itemsCollection = Mage::getSingleton('checkout/session')->getQuote()->getAllItems();
foreach($itemsCollection as $item) {
echo $item->getId();
$_bProduct = Mage::getModel('catalog/product')->load($item->getId());
echo '<pre>';
var_dump($_bProduct);
echo '</pre>';
echo '<br>';
echo $_bProduct->getData('backorder_shipment_date');
}

To access custom attributes on all pages, you should use :
getItemCollection()
or
getProductCollection()
also, check this link Accessing Custom Product Attributes in the Cart/Checkout Area

Related

Magento getAttribute not working with listing product

echo $_product->getResource()->getAttribute($attribute)->getFrontend()->getValue($_product);
This code is not working fine in view.phtml it is not return first attribute code value.
when i write this code on view page, it is not showing first product attribute and all after first in loop are showing fine.
This is my all code
<?php
$productAttributeTh = array('Color','Item','Size');
$configurableProduct = Mage::getModel('catalog/product')->load($_product->getId());
$childProducts = Mage::getModel('catalog/product_type_configurable')->getUsedProducts(null,$configurableProduct);
foreach($childProducts as $child) {
$product_id = $child->getId();
$obj = Mage::getModel('catalog/product');
$_childProduct = $obj->load($product_id); // Enter your Product Id in $product_id
foreach ($productAttributeTh as $key => $productAttributeValue){
$productAttribute = $_childProduct->getResource()->getAttribute($productAttributeValue)->getFrontend()->getValue($_childProduct);
echo $productAttribute;
}
} ?>
You need to make sure your attribute is set to be used in list. Go to;
Catalog > Attributes > Manage Attributes
Find your attribute, and open it. Scroll down to the option 'used in product listing' and set it 'yes'. Save and then reindex attributes.
Try this.
Mage::getResourceModel('catalog/product')->getAttributeRawValue($productId, 'attribute_code', $storeId);
Or
$attribute_value = $product->getResource()->getAttribute($attribute_code)->getFrontend()->getValue($product);
i hope this will help you.

Show Product's Custom Attributes to Homepage or static page

I want to echo the custom attributes to any other page, except Product Page or Category Listing
One way of getting custom attribute in nearly any page can be done as following (in PHP):
$product_id = $this->getProduct()->getId(); // note that this changes depending on how you load your product
$storeId = Mage::app()->getStore()->getStoreId();
$value = Mage::getResourceModel('catalog/product')->getAttributeRawValue($product_id, 'your_power_attribute_id', $storeId);
echo $value;

How to get correct product url for multistore magento?

I am running following code in Magento root to get the product urls
<?php
require_once('app/Mage.php');
umask(0);
Mage::app(1);
$collection = Mage::getModel('catalog/product')
->setStoreId(1)
->getCollection();
foreach( $collection as $product )
{
echo $product->getProductUrl();
echo "<br>";
}
?>
I am getting product urls like http://example.com/catalog/product/view/id/5/ , But these urls are invalid.
The product urls are as following in front end http://example.com/product.html
How do I get the correct product urls? I have multi store Magento set-up.
You need to get store url for each product separetly. In other words, you need to use something like this:
$collection = Mage::getModel('catalog/product')
->getCollection();
foreach( $collection as $product )
{
echo $product->setStoreId(5)->getProductUrl();
echo "<br>";
}

magento custom attribute label disappears

I've created a new phtml page to put inside the view page of the product, in this phtml page I called a new custom attribute that I have associated at the product, whit this code (I've the suggestion in this website, and this was working wonderfully!):
<?php $product_id = Mage::registry('current_product')->getId();
$_product=Mage::getModel('catalog/product')->load($product_id);
echo $_product->getAttributeText('video'); ?>
but now I have made the new magento update and my custom attribute is disappeared, this is strange because I don't work in the base file of Magento but I've created a my theme to work.
It's possible that something is changed in Magento?
Thank you for the help!
Try this way:
$_helper = $this->helper('catalog/output');
$_product = $this->getProduct();
//Display custom attribute.
//Attribute code = video;
$product = Mage::getModel('catalog/product')->load($_product->getId());
$attribute = $product->getResource()
->getAttribute('video')
->getFrontend()->getValue($product);
echo $attribute;
From My blog

Updating Meta Title in Magento via PHP script causes unwanted events

On our Magento 1.5.0.1 I wrote a PHP script to update all of our product meta titles to be based on the name and SKU of each product. The script is run by calling PHP via SSH.
After running the script I noticed in our M2E Pro (the eBay syncing module) that every single product appears to have become disabled.
Looking at the actual products they aren't disabled, so it appears my script is somehow faking the event.
Out of our 3,000+ products, though, one of them was disabled and subsequently M2E Pro removed the listing from eBay.
Here is my script:
<?php
set_time_limit(0);
define('MAGENTO', "/home/discount/public_html");
require_once MAGENTO . '/app/Mage.php';
error_reporting(E_ALL);
ini_set('display_errors', '1');
umask(0);
Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);
$storeId = Mage::app()->getStore('default')->getId();
$products = Mage::getModel('catalog/product')
->getCollection()
->addAttributeToSelect('sku')
->addAttributeToSelect('name')
->addAttributeToSelect('meta_title');
$total = count($products);
$count = 1;
foreach ($products as $product)
{
$mt = sprintf("%s [%s]", $product->getName(), $product->getSku());
$sku = $product->getSku();
if ($product->getMetaTitle() != $mt)
{
$percent = $count / $total;
echo $sku." ".$percent."\n";
$product->setMetaTitle($mt);
$product->save();
}
}
?>
I'd like to know what I need to do to my script to make it correctly update the meta title without causing odd events?
Stab in the dark here, but it's a good chance the M2E extension has an event listener setup for model/product saves, and in that event listener does things depending on product state.
By itself this isn't a problem, but when you load an EAV model via its collection it loads the bare minimum of attributes. Since you haven't explicitly loaded product status via addAttributeToSelect this may be confusing the M2E extension. Trying adding the status attribute. If that doesn't work try addAttributeToSelect('*') — M2E may be checking other attributes as well to decide when to pull things.
Finally, when you load an EAV model via a collection their afterLoad isn't called. It's possible the M2E extension (naively) assumes something that happens in afterLoad will have happened, so you may want to call it explicitly for each product.
$product->afterLoad();
Short of that, you'll need to reverse engineer M2E to the point in the code where it's doing it's above logging and determine why it thinks it needs to disable a product. Since this is a commercial extension, I'd reach out to the developers. If you pay for something you deserve some level of support.
<?php
//increase the max execution time
#ini_set('max_execution_time', -1);
//memory_limit
#ini_set('memory_limit', -1);
error_reporting(E_ALL);
ini_set('display_errors', '1');
// Start Despaly All Product Meta Title And Description
require_once('app/Mage.php'); //Path to Magento
umask(0);
Mage::app();
$categories = Mage::getModel('catalog/category')
->getCollection()
// magic is prepared here..
->addAttributeToSelect('*')
// then the magic happens here:
->addAttributeToFilter('level', array('eq'=>4))
->load();
if (count($categories) > 0):
foreach($categories as $category):
$catId = $category->getId();
$category = Mage::getModel('catalog/category')->load($catId);
$resource = Mage::getResourceModel('catalog/category');
//if($catId==1465): //If Update Specific category Value by Id
$CategoryName = $category->getName();
$metaTitle = "Buy ".$CategoryName." Test Title";
$metaDescription = "Shop your favourite ".$CategoryName." Test Description";
$category->setData('meta_title', $metaTitle);
$resource->saveAttribute($category, 'meta_title');
$category->setData('meta_description', $metaDescription);
$resource->saveAttribute($category, 'meta_description');
$check = $category->getMetaTitle();
echo "<pre>";
print_r($catId);
echo "<pre>";
print_r($check);
echo "\n";
//endif;
endforeach;
else: echo "No Results";
endif;
?>

Resources