Find parent id of product in magento - magento

here is my code.
$storeId = Mage::app()->getStore()->getId();
$session = Mage::getSingleton('checkout/session');
foreach($session->getQuote()->getAllItems() as $item)
{//how to find parent id here}
At the above mentioned comment I want to access parent id of that particular product.
Please Help ..

Try below code, may be it will help you
if($item->getTypeId() == "simple"){
$parentIds = Mage::getModel('catalog/product_type_grouped')->getParentIdsByChild($item->getId()); // check for grouped product
if(!$parentIds)
$parentIds = Mage::getModel('catalog/product_type_configurable')->getParentIdsByChild($item->getId()); //check for config product
}

You can use this to get the parent
foreach(...) {
$parents = $item->getTypeInstance()->getParentIdsByChild($item->getId());
//if you need to load the parent
if(!empty($parents)) {
$parentProd = Mage::getModel('catalog/product')->load($parents[0]);
//do something
}
}

this might help you:
$product = Mage::getModel('catalog/product');
$product->load($productId);
$grouped_product_model = Mage::getModel('catalog/product_type_grouped');
$groupedParentId = $grouped_product_model->getParentIdsByChild($product->getId());

Related

How to get correct quantity for child product in cart

I get cart items information using following code:
$cart_items = Mage::getSingleton('checkout/session')->getQuote()->getAllItems();
foreach( $cart_items as $items )
{
$items->getQty();
}
In above code $items->getQty() always return "float(1)" while more than 1 quantity add in cart for child product.
How to get correct quantity for child product?
Thanks in advance.
Finally I found my solution:
$cart_items = Mage::getSingleton('checkout/session')->getQuote()->getAllItems();
foreach( $cart_items as $items )
{
STATIC $qty='';
if($items->getProductType() == 'configurable') //configurable products
{
$qty = $items->getQty();
continue;
}
else // non-configurable product
{
if (!$items->getParentItem()) // product which has not parent product
{
$qty = $items->getQty();
}
}
echo $qty;
}
For simple products your code should work.
Try using a model call to see if it makes a difference. Also check sales_flat_quote table and see what items/quantity is found under the sales_flat_quote_item table and if they mismatch with the frontend display.
$oQuote = Mage::getModel( 'checkout/cart' )->getQuote();
// For all items.
$iTotalItemQty = $oQuote->getItemsQty();
echo $iTotalItemQty;
Also are you seeing this show up from a simple product with quantity > 1 or a different product type?
Did you tried getAllVisibleItems() instead of getAllItems()?
$cart_items = Mage::getSingleton('checkout/session')->getQuote()->getAllVisibleItems();
foreach( $cart_items as $items )
{
$items->getQty();
}

Magento Multiple Wishlist creation programmatically

I know how to add products to a customer's wishlist programmatically however it only adds to one wishlist. I have the multiple wishlist option set to enabled however I do not know how to create a new wishlist instead of merging products into the existing wishlist.
public function submitQuote(Mage_Adminhtml_Model_Session_Quote $quote)
{
$currentQuote = $quote->getQuote();
$customer = $currentQuote->getCustomer();
$items = $currentQuote->getAllVisibleItems();
//$wishlist = Mage::helper('wishlist')->getWishlist();
//Mage::register('wishlist', $wishlist);
$wishlist = Mage::getModel('wishlist/wishlist');
$curretDate = date('m/d/Y', time());
$wishlist->setCustomerId($customer->getId());
$wishlist->setName('Quote ' . $curretDate)
->setVisibility(false)
->generateSharingCode()
->save();
foreach ($items as $item)
{
$productId = $item->getProductId();
$product = Mage::getModel('catalog/product')->load($productId);
$buyRequest = $item->getBuyRequest();
$result = $wishlist->addNewItem($product, $buyRequest);
if(is_string($result))
{
Mage::throwException($result);
}
$wishlist->save();
}
//Mage::unregister('wishlist');
}
I've had this problem before. To add a product to a customer's wishlist you need to start a wishlist model and call the addNewItem method passing the product object. Here is how I do it.
$customer = Mage::getModel('customer/customer');
$wishlist = Mage::getModel('wishlist/wishlist');
$product = Mage::getModel('catalog/product');
$customer_id = 1;
$product_id = 1;
$customer->load($customer_id);
$wishlist->loadByCustomer($customer_id);
$wishlist->addNewItem($product->load($product_id));
Hope this helps!
EDITED:
$customerid= "YOUR CUSTOMER ID "; // Modify This
$customer=Mage::getModel("customer/customer")->load($customerid);
$wishlist=Mage::getModel("wishlist/wishlist")->loadByCustomer($customer,true);
Check for reference this: app/code/core/Mage/Wishlist/Model/Wishlist.php
LATEST EDIT:
I see that your problem is here:
$result = $wishlist->addNewItem($product, $buyRequest);
FINAL EDIT:
Then create another function and add the items and wishlist you want to add it to (as parameters). This should add items to the wishlist that you have passed as an argument when calling the function. Its the only solution I can come up with. Hope this helps!
I just stumbled across this question because I'm trying to do something similar - your code part helped me to create a new wishlist, and I've managed to do the rest so thought I'd share.
This code will;
Create a new wishlist with the name 'Quote dd/mm/YY h:m:s'
Load the wishlist collection for the current user, ordered by highest ID first
Load the newest wishlist
Add all basket items to it
Redirect the user to the new wishlist view.
public function addToQuoteAction() {
if(Mage::getSingleton('customer/session')->isLoggedIn()) {
$customerData = Mage::getSingleton('customer/session')->getCustomer();
$customerId = $customerData->getId();
} else {
Mage::getSingleton('core/session')->addError($this->__('Please login to use this feature'));
return $this->_redirectUrl($this->_getRefererUrl());
}
// Create the wishlist
$newWishlist = Mage::getModel('wishlist/wishlist');
$curretDate = date('d/m/Y h:m:s', time());
$newWishlist->setCustomerId($customerId);
$newWishlist->setName('Quote ' . $curretDate)
->setVisibility(false)
->generateSharingCode()
->save();
// Find the newly create list and load it
$wishlistCollection = Mage::getModel('wishlist/wishlist')->getCollection()
->addFieldToFilter('customer_id',$customerId)->setOrder('wishlist_id');;
$firstWish = $wishlistCollection->getFirstItem();
$wishlist = Mage::getModel('wishlist/wishlist')->load($firstWish->getWishlistId());
$cart = Mage::getModel('checkout/cart')->getQuote();
//getAllItems
foreach ($cart->getAllVisibleItems() as $item) {
$productId = $item->getProductId();
$buyRequest = $item->getBuyRequest();
$result = $wishlist->addNewItem($productId, $buyRequest);
$wishlist->save();
}
Mage::getSingleton('core/session')->addSuccess($this->__('All items have been moved to your quote'));
return $this->_redirectUrl(Mage::getBaseUrl().'wishlist/index/index/wishlist_id/'.$firstWish->getWishlistId().'/');
}

Magento - get cart items for a given product id

I try to get the cart items for a given product;
I have tried this code :
$product = Mage::getModel('catalog/product')
->setStoreId(Mage::app()->getStore()->getId())
->load('2784');
$quote = Mage::getSingleton('checkout/cart')->getQuote();
$cartItems = $quote->getItemByProduct($product);
foreach ($cartItems as $item) {
echo $item->getId()."<br/>";
}
but it don't gives anything.
How can I modify my code to use "getItemByProduct" in the right form ?
Thanks for help.
getItemByProduct() returns the first matching Mage_Sales_Model_Quote_Item so there is no need for an extra loop.
$item = $quote->getItemByProduct($product);
if ($item !== false) echo $item->getId();
I'd use
foreach ($quote->getItems() as $item) {
if ($item->getProductId() == $product->getId()) {
print_r($item->getData());
}
}
You can not use getItemByProduct() function on the checkout/cart or checkout/quote model as that method is of the sales/quote model.
You can find this method at the Mage_Sales_Model_Quote class. so it is used withe sales/quote. hope this is useful.

get custom attributes from an Mage_Sales_Model_Order_Item object

I’m writing an Observer for manage the order’s items, I need to send an email for every order based on some custom attributes.
The item object is Mage_Sales_Model_Order_Item and searching around I’ve tried methods like getData(’my_code’), getCustomAttribute, getAttributeText without success.
I need to get the category, size, color and some custom attributes…
Here my little code
class Example_OrderMod_Model_Observer{
public function doSomething($observer){
$order = $observer->getEvent()->getOrder();
$id_ordine = $order->getRealOrderId();
$cliente = $observer->getEvent()->getOrder()->getCustomerName();
foreach ($order->getAllItems() as $item) {
//$item is an instance of Mage_Sales_Model_Order_Item
$quantita = $item->getQtyOrdered();
$codice_giglio = $item->getSku();
//echo $item->getData('size');
var_dump($item->getAttributeText('size'));
var_dump($item->getProductOptionByCode('size'));
var_dump($item->getProductOptionByCode('famiglia'));
}
// die();
}
}
any ideas?
many thanks
You'll probably want to load up the product object, and then get your data off of that object. That will allow you to utilize all the methods you are looking for:
$product = Mage::getModel('catalog/product')->load($item->getProductId());
$size = $product->getAttributeText('size');
If $item is instance of Mage_Sales_Model_Order_Item you could simply use:
$product = $item->getProduct();
solution of display custom attributes
function getAttr($product_id, $attributeName) {
$product = Mage::getModel('catalog/product')->load($product_id);
$attributes = $product->getAttributes();
$attributeValue = null;
if(array_key_exists($attributeName , $attributes)) {
$attributesobj = $attributes["{$attributeName}"];
$attributeValue = $attributesobj->getFrontend()->getValue($product);
}
return $attributeValue;
}
When i tried the above methods it didn't worked somehow in my observer class. Actually the product model is unable to load with the above code block. I found this code and it worked. It loads the product model in observer.
$product = Mage::getModel('catalog/product');
$productId = $product->getIdBySku($item->getSku());
$product->load($productId);

Magento - Move a category programmatically

How do I move a category to another category with all child categories?
I have tried the following solution:
$nodeId = 2269;
$parentId = 2268;
$tree = Mage::getResourceModel('catalog/category_tree')->load();
$node = $tree->getNodeById($nodeId);
$parentNode = $tree->getNodeById($parentId);
$parentChildren = explode(',', $parentNode->getChildren());
$afterId = array_pop($parentChildren);
$prevNode = $tree->getNodeById($afterId);
if (!$prevNode || !$prevNode->getId()) {
$prevNode = null;
}
$tree->move($node, $parentNode, $prevNode);
However my result is somewhat twisted. If I move to the root-category the move works, but if I move to a child-category I get faulty results and disappearing categories.
These are the values of the field path in the database:
Old: 1/2/3/2175/2269
New: 1/2/3/2175/2226/2268/2269
Correct: 1/2/3/2226/2268/2269
The solution was quite simple, this one doesn't mess up the paths. However it feels like this method is slower.
$categoryId = 2269;
$parentId = 2268;
$category = Mage::getModel('catalog/category')->load($categoryId);
$category->move($parentId, null);
$parent = Mage::getModel('catalog/category')->load('REPLACE_WITH_PARENT_ID');
$category = Mage::getModel('catalog/category');
$category->addData(array('name' => 'YOUR_CATEGORY_NAME', 'path' => $parent->getPath()));
$category->save();

Resources