Using magento product model in footer - php

I am trying to display the Swatch attributes of all the associated simple products to the configurable product that the user is viewing.
I need to do this in the footer which is proving more difficult than I thought as a lot of methods etc are not available in the footer.
I have this code which just shows the Swatch attribute for the configurable product, I need this modified to show the Swatch attribute for all the simple products associated to this configurable.
<?php
$SKU = "2726578";
$product = Mage::getModel('catalog/product')->loadByAttribute('sku',$SKU);
echo $product->getSwatch();
?>

$sku = "2726578";
$product = Mage::getModel('catalog/product')->loadByAttribute('sku',$sku);
if($product->getTypeId() == "configurable"){
$childs = $product->getTypeInstance()->getUsedProducts();
}
Hope this helps! Just iterate over the childs and fetch the values.
cheers!

Related

Magento - Get the associated product attributes of an item in the wishlist

In app/code/local/Mage/Catalog/Product/Type/Configurable/Price.php, I am trying to get the attribute values of an associated product within the wishlist. I've attempted several approaches but I can only seem to produce data for the parent product.
Latest attempt
$customer = Mage::getSingleton('customer/session')->getCustomer();
if($customer->getId()) {
$wishlist = Mage::getModel('wishlist/wishlist')->loadByCustomer($customer, true);
$wishListItemCollection = $wishlist->getItemCollection();
foreach ($wishListItemCollection as $wlitem) {
$wishitem = Mage::getModel('catalog/product')->setStoreId($wlitem->getStoreId())->load($wlitem->getProductId());
//echo $wishitem->getId() . '<br>';
if($product->getId() == $wishitem->getId()) { //if the current product id equals the wishlist product id
echo $wishitem->getSku()."</br>";
}
}
}
That only gets me the parent product's sku. What I ultimately want to get is the attribute value for 2 attributes that I added for configurable products (not super attributes) but it seems that $product in Price.php only has the parent product collection.
Other Attempts:
$item_s = Mage::getModel('wishlist/item')->loadWithOptions($product->getId(), 'simple_product')->getOptionsByCode();
$simple_product = $item_s['simple_product']->getData();
$simple_product_id = $simple_product['product_id'];
$sim_product = Mage::getModel('catalog/product')->load($simple_product_id);
print_r($sim_product);
This only resulted in an error on the page.
Also:
$_item = Mage::getModel('catalog/product')->load($product->getId());
//echo $_item->getData('ppuom');
//print_r($_item);
$simpleProduct = $_item->getOptionsByCode()['simple_product']->getItem()->getProduct();
print_r($simpleProduct);
Seems as if you were most of the way there. I've tested this on my Magento site and it worked for me. It's pretty simple actually, you just have to grab the right model for that collection. Also, it seems like you're changing the pricing?!?! Be careful that your wishlist items contain the necessary attributes used in your logic.
$_item = Mage::getModel('catalog/product')->load($product->getId());
$attribute1 = $_item->getData('attribute1_code'); //see admin for attribute code
$attribute2 = $_item->getData('attribute2_code'); //see admin for attribute code
OR
Make changes to your template's wishlist files rather than the pricing logic in the code folder. You'll have access to all the data you need and it won't interfere with the price.php file which is relied on heavily in the cart and other critical areas of the website. The price in the wishlist is recalculated when it's moved to the cart anyway.

How can I call programmatically a product description in Magento 1.7.0.2?

I am new in Magento 1.7.0.2
I would like to make some custom html divs inside the product's phtml page and to call
1) the product description ,
2) another div with product tags and
3) another div with reviews of this specific product.
4) another div which will contain a specific cms page (ask about this product)
Do you know how should I write php inside each div in order to call these
specific attributes of a product in Magento 1.7.0.2 ?
Thank you very much
here i am giving your code to do with your above specification you can set it in to your html
1. Product description
$_product = Mage::getModel('catalog/product')->load(PRODUCT_ID);
echo $_product->getShortDescription(); //product's short description
echo $_product->getDescription(); // product's long description
2. product tags
$model=Mage::getModel('tag/tag');
$tags= $model->getResourceCollection()
->addPopularity()
->addStatusFilter($model->getApprovedStatus())
->addProductFilter(PRODUCT_ID)
->setFlag('relation', true)
->addStoreFilter(Mage::app()->getStore()->getId())
->setActiveFilter()
->load();
if(isset($tags) && !empty($tags)):
foreach($tags as $tag):
echo '<span class="tag">'.$tag->getName().'</span>';
endforeach;
3. Product Reviews
$productId = $product->getId();
$reviews = Mage::getModel('review/review')
->getResourceCollection()
->addStoreFilter(Mage::app()->getStore()->getId())
->addEntityFilter('product', $productId)
->addStatusFilter(Mage_Review_Model_Review::STATUS_APPROVED)
->setDateOrder()
->addRateVotes();
4.contain a specific cms page
I would like to suggest you if you want to display product specific content you can create block for product and in phtml file you can call like below
echo $this->getLayout()->createBlock('cms/block')->setBlockId('your-block-id')->toHtml();
Also you can refer detail page
hope this will sure help you.

Get product id in magento

In magento,i want to add quick look feature like this http://www.timberlandonline.co.uk/on/demandware.store/Sites-TBLGB-Site/default/Link-Category?cgid=men_footwear_boots.I have added a hidden input & a div in list.phtml.If i click the div of any product javascript returns product id of first product in that category page.But it should return product id of the selected div.
You need to look into this page (<path_to_your_template_folder>/template/catalog/product/list.phtml) carefully. You will find the following lines of code in different places of this page only:-
$_productCollection = $this->getLoadedProductCollection();
foreach ($_productCollection as $_product):
$reqProductId = $_product->getId();
endforeach;
If you carefully match the above code & the code in the above-mentioned page, you will know that you need to use the variable "$reqProductId" properly in your required "INPUT" element of type "hidden". So you will require it to do your part in the main "foreach" loop.
Hope it helps.
Try below code to get currently loaded product id:
$product_id = $this->getProduct()->getId();
When you don’t have access to $this, you can use Magento registry:
$product_id = Mage::registry('current_product')->getId();
Also for product type i think
$product = Mage::getModel('catalog/product')->load($product_id);
$productType = $product->getTypeID();

Magento: using getModel to get product and category

I have some code that returns an array of product details including a url_path. To create a link to this product I have to know the category and subcategory of the product. Unfortunately out all the data this method returns neither the category or sub category are pulled out.
Here is the code I have that gets a product:
$product_id = array(231, 230,229,228);
foreach ($product_id as $id){
$productDetails = Mage::getModel('catalog/product')->load($id)->getData();
echo $productDetails['url_path'].'<br />';
}
Is it possible to get the category and subcategory for each product?
you are looking for
foreach ($product_id as $id){
$categoryIds = Mage::getModel('catalog/product')->load($id)->getCategoryIds();
}
which will return you an array of category ids that the product belongs to.
Better way not to miss something with rewrites is to use core functionality.
Mage_Catalog module has own url model, with which product urls can be modified.
So, the code will be:
$product_id = array(231, 230,229,228);
$urlModel = Mage::getSingleton('catalog/url');
foreach ($product_id as $id) {
$urlModel->refreshProductRewrite($id);
}
And it is no need to retrieve category ids for this purpose.

Checking if a Magento product is a child of a configurable product

I have the following code to grab a list of Products
$collection = Mage::getModel('catalog/product')->getCollection();
$collection->addAttributeToSelect('name')
->addAttributeToFilter("category_ids", array('finset'=>$this->category_id));
foreach($collection as $product) {
echo $product->getName();
}
My question is, how can I NOT echo products that are 'simple' but belong to a parent 'configurable' product. (for example don't show "Red Shirt Medium" as it belongs to "Red Shirt")
I have worked out that this association lives in 'catalog_product_super_link' but I have only just started with Magento and unfortuantely don't know how to do the filtering :)
Cheers guys,
Chris.
I don't know a direct way to add this condition to the collection, I'd be interested in such a solution too. But you can always check inside the loop for each product:
if (empty(Mage::getModel('catalog/product_type_configurable')->getParentIdsByChild($product->getId())))
{
echo $product->getName();
}
I've done something similar for our google feed.
This excerpt of code is what I use to check the products inheritance:
$products = Mage::getModel('catalog/product')->getCollection();
$products->addAttributeToSelect('*');
$products->addAttributeToFilter('status', 1);//enabled
$products->addAttributeToFilter('price', array('gt' => 0) );//price not 0
//$products->addAttributeToFilter('visibility', 4); //catalog, search - comment out to show all items (configurable products simple product breakdowns)
Mage::getSingleton('catalog/product_status')->addVisibleFilterToCollection($products);
$prodIds=$products->getAllIds();
try {
foreach($prodIds as $productId) {
$product = Mage::getModel('catalog/product');
$product->load($productId);
// SIMPLE PRODUCTS
if($product->getTypeId() == 'simple' ) {
$prodName = trim($product->getName());
$parentIds = Mage::getModel('catalog/product_type_grouped')->getParentIdsByChild($productId);
if(!$parentIds)
$parentIds = Mage::getModel('catalog/product_type_configurable')->getParentIdsByChild($productId);
if($parentIds) {
$parentProd = Mage::getModel('catalog/product')->load($parentIds[0]);
/*
* do something if this product has a parent or do some checks against $parentProd
*/
} // end parent check
}//if SIMPLE
} // foreach
} catch(Exception $e) {
die($e->getMessage());
}
There's a function called isConfigurable in the product class.
That may be of help to you.
$product->isConfigurable();
// if its the parent object it'll be true, if its the child it'll be false.
The quickest way might be to check if the product's visibility is set to "Not Visible Individually", since simple products associated with configurable products are typically set to this. Unfortunately I don't know the precise syntax but hopefully someone else who's willing to chime in does!
Since simple products that are part of configurable products usually have a visibility value of Not Visible Individually, it probably suffices to add a visibility filter to the collection that checks for visibility of the products in the catalog:
$collection->setVisibility(Mage::getModel('catalog/product_visibility')->getVisibleInCatalogIds());
In the unlikely circumstance that the resulting products are part of a configurable product, you can use the method Mage_Catalog_Model_Product_Type_Configurable::getParentIdsByChild to check if a product is used as part of a configurable product.

Categories