Magento - Product Collection with the current user's Wishlist - magento

Within a Magento php Controller, how can I get a Product Collection containing the products listed in the logged in user's (ie current user's) Wishlist.
I am getting the Wishlist using:
$wishList = Mage::getModel('wishlist/wishlist')->loadByCustomer(Mage::getSingleton('customer/session')->getCustomer());
and this contains the correct number of items.
But I would like to get a Product Collection. I have tried:
$productCollection = $wishList->getProductCollection();
and
$productCollection = $wishList->getProductCollection()->addAttributeToSelect('id')->load();
but the Product Collection I get has a length of 0.
How do I get the Product Collection?

You can use the getWishlistItemCollection (see link for more details) off the wishlist helper to return a collection of items, you then need to get the product from the item.
I have been using the following code to create an associative array of the products, which I then use to determine if a product I am displaying in the list page is in the wishlist...hopefully this will help:
public function getWishList() {
$_itemCollection = Mage::helper('wishlist')->getWishlistItemCollection();
$_itemsInWishList = array();
foreach ($_itemCollection as $_item) {
$_product = $_item->getProduct();
$_itemsInWishList[$_product->getId()] = $_item;
}
return $_itemsInWishList;
}

Try this with product all details like name, images etc...
<?php
$customer = Mage::getSingleton('customer/session')->getCustomer();
if($customer->getId())
{
$wishlist = Mage::getModel('wishlist/wishlist')->loadByCustomer($customer, true);
$wishListItemCollection = $wishlist->getItemCollection();
foreach ($wishListItemCollection as $item)
{
echo $item->getName()."</br>";
echo $item->getId()."</br>";
echo $item->getPrice()."</br>";
echo $item->getQty()."</br>";
$item = Mage::getModel('catalog/product')->setStoreId($item->getStoreId())->load($item->getProductId());
if ($item->getId()) :
?>
<img src="<?php echo Mage::helper('catalog/image')->init($item, 'small_image')->resize(113, 113); ?>" width="113" height="113" />
<?php endif; } } ?>

$customer = Mage::getSingleton('customer/session')->getCustomer();
$wishlist = Mage::getModel('wishlist/wishlist')->loadByCustomer($customer, true);
$wishListItemCollection = $wishlist->getItemCollection();
foreach ($wishListItemCollection as $item)
{
// do things
}

Related

Magento Show custom attribute dropdown option for a simple product in cart page

how to Show custom attribute dropdown option for a simple product in cart page
my attribute name is meal_time
<?php
$product = Mage::getModel('catalog/product')->load($_item->getProductId());
echo $product->getAttributeText('meal_time');
?>
To get the custom attribute option in the cart , first we need to get quote visible product items and then load the product to get its options.
$cart = Mage::getSingleton('checkout/session')->getQuote()->getAllVisibleItems();
foreach ($cart as $item) {
$product_id = $item->getProduct()->getId();
$_product = Mage::getModel('catalog/product')->load($product_id);
echo $_product->getMealTime();
}
To display all the option of an attribute
$cart = Mage::getSingleton('checkout/session')->getQuote()->getAllVisibleItems();
foreach ($cart as $item) {
$product_id = $item->getProduct()->getId();
$_product = Mage::getModel('catalog/product')->load($product_id);
$attribute = Mage::getModel('eav/config')-getAttribute('catalog_product', 'meal_time');
foreach ($attribute->getSource()->getAllOptions(true, true) as $instance) {
echo $myattribute[$instance['value']] = $instance['label'];
}
}
If you want to display as select dropdown , use select tag including the label and value as options

How can i update my products?

I want to update from my script the meta title, the meta description and the meta keywords for all of my products.
The meta title = the name of the product
The meta description = the short description of the product
The meta keywords is the meta keywords i've put from the category of the product.
I can loop and get the informations i want to make the update (except the meta keywords of the description) and after how i can make the update inside the loop ?
Thanks
<?php require_once 'app/Mage.php';
umask(0);
set_time_limit(0); // ignore php timeout
ignore_user_abort(true); // keep on going even if user pulls the plug*
while(ob_get_level())ob_end_clean(); // remove output buffers
ob_implicit_flush(true); // output stuff directly
//error_reporting(E_ALL);
/* not Mage::run(); */
Mage::app('default');
// get product collection (All product)
$storeId = Mage::app()->getStore()->getId();
$visibility = array(
Mage_Catalog_Model_Product_Visibility::VISIBILITY_BOTH,
Mage_Catalog_Model_Product_Visibility::VISIBILITY_IN_CATALOG);
$_productCollection = Mage::getResourceModel('catalog/product_collection')
->addAttributeToSelect('*')
->addAttributeToFilter('visibility', $visibility)
->setStoreId($storeId)
->addStoreFilter($storeId);
foreach ($_productCollection as $pro) { // loop each product
$meta_title = $pro['meta_title'];
$meta_description = $pro['meta_description'];
$name = $pro['name'];
$short_description = $pro['short_description'];
// I want to update the meta_title, meta_description, meta_keywords
// meta_description = short_description
// meta_title = name
// meta_keywords = meta keywords from the category of the product
}
?>
You can easy set keyword and description by using standard setters.
Inside foreach loop just use :
foreach ($_productCollection as $pro):
$product = Mage::getModel('catalog/product')->load($pro->getId());
$product->setMetaTitle('Product Title')
->setMetaKeyword('Product keywords')
->setMetaDescription('Product Description')
->save();
endforeach;
Try this,
foreach ($_productCollection as $pro):
$product = Mage::getModel('catalog/product')->load($pro->getId());
$cats = $product->getCategoryIds();
$meta_keys = '';
foreach ($cats as $category_id) {
$_cat = Mage::getModel('catalog/category')->load($category_id) ;
if($_cat->getMetaKeywords())
$meta_keys .= $_cat->getMetaKeywords().', ';
}
$product->setMetaTitle($pro->getName())
->setMetaKeyword($meta_keys)
->setMetaDescription($pro->getShortDescription())
->save();
endforeach;

Geting quantity for a single item in cart magento

Hello i need to get the quantity of the item added in cart for the item i am viewing in product detail
atm i am using this
<?php $cart = Mage::getModel('checkout/cart')->getQuote();
$result = array();
foreach ($cart->getAllItems() as $item) {
$result['qty'] = $item->getQty();
}
$qty=$result['qty'];
//echo $qty ;
if(!$qty == 0){
echo '<span>'.$qty.'</span>';
}else{
echo "<span>0</span>";
}?>
but it returns only the last qty added/updated in the cart, and i need the specific single product qty
Try this:
//$product is current product that you are viewing
$qty = null;
foreach ($cart->getAllItems() as $item) {
if ($product->getId() == $item->getProductId()) {
$qty = $item->getQty();
}
}
if (!is_null($qty)) {
//$qty is what you are looking for
}
else {
//the current product is not in the cart
}

unable to get product price in custom file magento

Hi i am new to magento and i need to get the products from a particular category
for this i have use
<?php
$id1=4;
$category1 = Mage::getModel('catalog/category')->load($id1);
$collection1 = $category1->getProductCollection();
$collection1->addAttributeToSelect('name');
$collection1->addAttributeToSelect('description');
$collection1->addAttributeToSelect('image');
$collection1->addAttributeToSelect('producturl');
$collection1->addAttributeToSelect('prlce');
$products1 = $collection1->getItems();
$_helper1 = $this->helper('catalog/output'); ?>
<?php foreach ($products1 as $product1){ ?>
<?php echo $this->htmlEscape($product1->getPrice()) ?>
<?php } ?>
in this it is showing the name , image and url but when i am trying to echo price it doenot show anyhing.Please suggest me where i am doing mistake
This is how you do it:
<?php
// Test Params
$cat_id = 4;
$store_id = 1;
// Load Category
$category = Mage::getModel('catalog/category')->load($cat_id);
// Load Category Products
$categoryProducts = $category->getProductCollection();
// Iterate Through Product Collection
foreach ($categoryProducts as $categoryProduct)
{
// Load Product At Specified Store Id
$product = Mage::getModel('catalog/product')
->setStoreId($store_id)
->load($categoryProduct->getId());
// Debug
print_r($product->getData());
// Get Price (e.g.)
echo 'Product Price: '. $product->getPrice()
}
?>

Magento - Accessing a customer's wishlist

I would like to be able to load a customer's wishlist and return the product id's of the products within the list
I am using:
$wishList = Mage::getSingleton('wishlist/wishlist')->loadByCustomer($customer);
$wishListItemCollection = $wishList->getItemCollection();
The problem is that the arrays in the Item Collection are protected and I can't find any methods to extract the data.
Is there another way to do this?
You're very close to your target.
$wishList = Mage::getSingleton('wishlist/wishlist')->loadByCustomer($customer);
$wishListItemCollection = $wishList->getItemCollection();
if (count($wishListItemCollection)) {
$arrProductIds = array();
foreach ($wishListItemCollection as $item) {
/* #var $product Mage_Catalog_Model_Product */
$product = $item->getProduct();
$arrProductIds[] = $product->getId();
}
}
The array variable $arrProductIds will now contain the list of all Product IDs that have been wishlisted by that specific Customer.
Your code is correct. There may be customer is not loaded. here is code.
$customer = Mage::getSingleton('customer/session')->getCustomer();
$wishlist = Mage::getModel('wishlist/wishlist')->loadByCustomer($customer, true);
$wishListItemCollection = $wishlist->getItemCollection();
foreach ($wishListItemCollection as $item)
{
// do things
}
In any template, using magento 1.8, this works
Total: <?php echo $this->helper('wishlist')->getItemCount() ?>
// Items
$this->helper('wishlist')->getWishlist()->getItemCollection();
$wishList = Mage::getSingleton('wishlist/wishlist')->loadByCustomer($customer);
$wishListItemCollection = $wishList->getItemCollection();
foreach ($wishListItemCollection as $item)
{
//do your thing e.g. echo $item->getName();
}
Try this with product all details like name, images etc...
<?php
$customer = Mage::getSingleton('customer/session')->getCustomer();
if($customer->getId())
{
$wishlist = Mage::getModel('wishlist/wishlist')->loadByCustomer($customer, true);
$wishListItemCollection = $wishlist->getItemCollection();
foreach ($wishListItemCollection as $item)
{
echo $item->getName()."</br>";
echo $item->getId()."</br>";
echo $item->getPrice()."</br>";
echo $item->getQty()."</br>";
$item = Mage::getModel('catalog/product')->setStoreId($item->getStoreId())->load($item->getProductId());
if ($item->getId()) :
?>
<img src="<?php echo Mage::helper('catalog/image')->init($item, 'small_image')->resize(113, 113); ?>" width="113" height="113" />
<?php endif; } } ?>

Resources