Magento Credit Memos Model - magento

Can anyone help with the model/collection to search through all credit memos within magento as opposed to orders?
I can use Mage::getModel('sales/order') for orders but not seeing for credit memos
Kind regards

I put this together quickly. For example, let's say you wanted to grab the comments, you can do something like this.
Get CreditMemo Comment Based on Order
$incrementId = 100000002;
$collection = Mage::getResourceModel('sales/order_creditmemo_collection')
->addAttributeToFilter('increment_id', $incrementId);
foreach($collection as $item) {
$creditMessage = Mage::getResourceModel('sales/order_creditmemo_comment_collection')
->addAttributeToFilter('entity_id', $item->getEntityId());
foreach($creditMessage as $message) {
echo $message->getComment();
}
}
You can filter the collection with the increment_id or even the order_id :)
Get All Comments
$collection = Mage::getResourceModel('sales/order_creditmemo_comment_collection');
Below is a list of tables for creditmemo
sales_flat_creditmemo
sales_flat_creditmemo_comment
sales_flat_creditmemo_grid
sales_flat_creditmemo_item
Hope this helps!

right way to get Credit Memo comment collection
$collection = Mage::getResourceModel('sales/order_creditmemo_collection')
->addFieldToFilter('order_id', $order->getId()); /* here we have no load memo by order id not increment id.Increment id is the creditmemo increment id not order increment id. */
foreach($collection as $item) {
$creditMessage = Mage::getResourceModel('sales/order_creditmemo_comment_collection')
->addAttributeToFilter('parent_id', $item->getId()); /* here we need to use parent_id not entity_id.*/
}
var_dump($creditMessage->getData());
exit();

Related

getStoreCategories() to get categories by ids

I would like to get an array of categories for my magento store.
I need to use following unit for my website to work:
$categories = $helper->getStoreCategories('name', true, true);
However this lists all categories.
I would like to select categories that are important for me. I think I could do that by selecting ids of the categories or names of the categories but I don't know how to do it.
Could anybody help me please?
Let's say you have an array with category ids like this:
$ids = array(6,8,99);
You can get the category objects like this:
$collection = Mage::getModel('catalog/category')->getCollection()
->addAttributeToSelect('*')
->addAttributeToFilter('entity_id', $ids);
if you want only active categories add this line also
$collection->addAttributeToFilter('is_active', 1);
Use this function and just pass category Id:
function getCategoryData($_categoryId=null) {
// For category Collection
$category = Mage::getModel('catalog/category')->load($_categoryId);
// For product Collection category wise
$prodCollection = $category->getProductCollection();
return $prodCollection;
}

To find customer who have purchased only one time in magento

I know how to get order details of customer in magento with the following code
$salesModel=Mage::getModel("sales/order");
$salesCollection = $salesModel->getCollection()
->setOrder('increment_id','DESC');
But i need to display the customer details who have purchased only one time. I don't know how to resolve it. If anybody know, please save me guys!
If you go with the query, bellow query will help you,
SELECT * FROM sales_flat_order GROUP BY customer_id HAVING COUNT(customer_id) = 1
or This can be used,
$collection = Mage::getModel('customer/customer')->getCollection();
foreach ($collection as $user){
$orders = Mage::getModel('sales/order')
->getCollection()
->addFieldToSelect('increment_id')
->addFieldToFilter('customer_id',$user->getId());
if($orders->getSize() == 1){
echo $user->getId();
}
}

Last order details are displaying duplicate values for configurable products

I have added a simple code to track each purchase on the site. All products have different types of rules based on categories to subtract a predefined percent of amount of product price.
But when there are configurable products in the cart then it is displaying me both products, original product price and the price of the selected option for the product details in the order detail.
If I have a lipstick in my cart and the color I selected is "RED" then lipstick price will change. But it is displaying me the original lipstick price as well as the red lipstick price in the order details.
I have added the code on order success page.
$orders = Mage::getModel('sales/order')->getCollection()
->setOrder('created_at','DESC')
->setPageSize(1)
->setCurPage(1);
$orderId = $orders->getFirstItem()->getEntityId();
$order = Mage::getModel('sales/order')->load($orderId);
$items = $order->getAllItems();
foreach ($items as $itemId => $item)
{
$pid = $item->getProductId();
$product = Mage::getModel('catalog/product')->load($pid);
$_finalPrice = $product->getFinalPrice();
echo $price = Mage::helper('core')->currency($_finalPrice,true,false);
// Some Code
}
I have also tried
$tempmain = Mage::getModel('sales/order')->load(Mage::getSingleton('checkout/session')->getLastOrderId());
$temp = $tempmain->getAllItems();
$total = $tempmain->getGrandTotal();
foreach ($temp as $itemId => $item)
{
$pid = $item->getProductId();
$product = Mage::getModel('catalog/product')->load($pid);
$_finalPrice = $product->getFinalPrice();
echo $price = Mage::helper('core')->currency($_finalPrice,true,false);
// Some Code
}
With the same results.
How to get only "RED" lipstick price in Order details?
Thanks in advance.
This is the details for all the availabel lipstick and their values:
when I select "Tango" as the color for lipstick on front end:
I am getting the Price of both products on success page:
Here is the Order Details Screen shot:
In frontoffice, when a customer add a configurable produt in his cart and order it, you'll see (in Database) 2 lines (2 quote_items and 2 order_item).
It's normal because magento needs to store the configurable product and the linked simple produt corresponding to the user selection (and linked to the master configurable product).
When displaying order details, Magento handles that difference by checking if a product has a parent id. You can do the same to ignore some order_item :
foreach($items as $item){
if ($item->getParentProductId() {
continue; // ignoring simple product associated to master order item (configurable)
}
// your code
}
I'm NOT 100% sure what your tying to do, but if you are on the order success page then this is the way to retrive the current order info (get the the last order for the db may not always be the current order on the success page if two user make a purchase millisecond apart)
Try adding this to Success.phtml
$order = Mage::getModel('sales/order')->loadByIncrementId($this->getOrderId());
$items = $order->getItemsCollection();
foreach($items as $item){
if ($item->getParentItem()) {
continue;
}
echo Mage::helper('core')->currency($item->getPrice(),true,false);
}

How to get grouped product ID from purchased or cart product

I'm using grouped products to track promotions. Simple products will at times belong to multiple grouped products, so checking parentProductIds is of no use. I'm wondering how to track the grouped product ID when a product is purchased through the grouped (promotion) SKU. I can see it's being stored in info_buyRequest and super_product_config within the orders, but how do I get that information back out? And is there a way to get it out in the cart/quote?
I was able to get it with the following code in cart.phtml, in the foreach($this->getItems() as $_item):
$values = unserialize($_item->getOptionByCode('info_buyRequest')->getValue());
$parentId = $values['super_product_config']['product_id'];
Depending on where you want to get this information, you could get it after a checkout process when the sales is saved. Then you could use the events sales_order_save_after and create a method in a class to get the items of a grouped product.
What is important here is the object of a class Mage_Sales_Model_Order_Item which has information about the product and the parents of a product
Here is an example:
public function processSalesOrder($observer)
{
$order = $observer->getOrder()
$quoteItems = $order->getItemsCollection(null, true);
/*#var $item Mage_Sales_Model_Order_Item */
foreach ($quoteItems as $item) {
$parent = $item->getParentItem();
if(!is_null($parent)){
// do your stuff - you have a product parent which has children product
// $item is the children
echo 'The parent product is ' . $parent->getSku();
echo 'One of the children product is' .$item->getSku();
}
}
On cart page grouped product is treated as a simple product. In Magento 2 you can get the parent id of these simple products from session. This worked for me:
<?php
$catalogSession = $_SESSION['catalog'];
$parentId = $catalogSession['last_viewed_product_id'];
?>

How to load products which is not in particular category in magento

I have to load all available products from the particular attribute-set value additionally it needs to filter only the products which are not under particular category/categories. How can i load product collection here. Please help me
<?php
$storeId = Mage::app()->getStore()->getId();
$catalog = $this->getLayout()->createBlock('catalog/product_list')->setStoreId($storeId);
$collection = $catalog->getProductCollection()->addAttributeToSelect('*');
$collection->getSelect()->where(category_id != { cat_id } )->limit($this->products_count);
$_productCollection = $collection;
?>
This isn't teste yet and I'm not sure if category_id is the correct value that is used in the database. But it should give you a good start.

Resources