Observer Magento - Order Cancel - magento

I wonder what the correct event that I observe when a sale is canceled.
I'm trying to "sales_order_item_cancel" but I do not know if that's correct. Besides how can I get the ID of the sale?
I'm trying this way:
$order = $observer->GetEvent()->GetOrder()->getID();
but not working.
Thank You

Should always be camel case ..->get[Xyz]()
$order = $observer->getEvent()->getOrder()->getId();
See
app/code/core/Mage/Sales/Model/Order.php 1139 order_cancel_after
app/code/core/Mage/Sales/Model/Order/Item.php 512 sales_order_item_cancel
Mage::dispatchEvent('sales_order_item_cancel', array('item'=>$this));
Since sales_order_item_cancel dispatch item try
print_r($observer->getEvent()->getItem())

i think it is better to use sales_order_payment_cancel if you want to observe the whole order and not just single items.
You can access the order by $order = $observer->getPayment()->getOrder();
Cheers
Simon

Related

How to exclude product(s) from search result programatically in Magento

I'm trying to exclude products from populating the search result.
It seems to be working fine on my localhost but not on clients dev server.
I'm observing the event
catalog_block_product_list_collection
and in observer method, in the end is this code:
$observer->getCollection()
->addFieldToFilter('entity_id', array('nin' => array_keys($_excludeProducts)))
->clear()
->load();
which works for catalog as well and search result list but for the moment not on search result list on clients dev server.
Any guidance/help is greatly appreciated.
Edit: Debugging this method gives me an empty collection but still the data is populating from somewhere.
I've changed the approach and used another event: catalog_product_collection_load_before
found better method to implement the approach with less code. #optimization
$excludeIds = array(2,3,4); //$excludeIds mixed
$observer->getCollection()
->addIdFilter($excludeIds, true); //exclude = true
The event also helps in keeping the correct items count on toolbar as it is dispatched before collection load.
I ran into a similar issue when trying to filter this collection using this event.
Do you have flat categories and flat products set the same in both environments? In my case, my code would only work with flat tables OFF, since I was using joining other EAV attributes.
In your case, if you are using flat, I think you just need to do addAttributeToFilter() instead.
In my case, here is what my observer looks like:
function onCategoryCollectionLoad($observer) {
$collection = $observer->getEvent()->getCategoryCollection();
$customerGroupId = (int) Mage::getSingleton('customer/session')->getCustomerGroupId();
// hidden_from_groups is an EAV attribute; I couldn't figure out how to make it work with flat because it has a backend_model
$collection->addAttributeToSelect('hidden_from_groups');
$collection->addExpressionAttributeToSelect('should_be_hidden', "COALESCE(FIND_IN_SET($customerGroupId, {{attribute}}), 0)", 'hidden_from_groups');
// should_be_hidden is not a real db field (nor EAV attribute); it only exists because of the addExpressionAttributeToSelect() above.
$collection->addFieldToFilter('should_be_hidden', array('lt' => 1));
// I don't call $collection->load(); that will get called further down the line.
}

How To Efficiently Loop Through Product Collection and Set Attribute Data

I am trying to bulk update some of my product attributes. Effectively I am using this code:
foreach ($outOfStock as $product) {
$product->setData('attribute',20);
$product->save();
}
However I am getting a php 60 second timeout, when I check, it only got as far as about 15 products. It seems to me that this is not the most efficient way to achieve this.
What is the correct way to update products in bulk?
If your attribute is mass-updatable, you may refer to the corresponding core code from Mage_Adminhtml_Catalog_Product_Action_AttributeController::saveAction(), especially this method :
Mage::getSingleton('catalog/product_action')
->updateAttributes($productsIds, $attributesData, $storeId);

Magento my processing order count

How can I get count of customer's processing order status ??
Like Mage::getModel('checkout/cart')->getQuote()->getItemsCount().
Please let me know.
Thank you.
To get only the number of orders in a processing stateā€¦
$processingOrdersCount = Mage::getModel('sales/order')->getCollection()
->addFilter('status', Mage_Sales_Model_Order::STATE_PROCESSING)
->getSize()
;
Here is a similar question that loads the order collection with the 'status' field you are after:
Get order ids with status = 'Complete' in Magento

Get ordernumber on the event sales_order_payment_pay

I'm trying to get my order number on the event sales_order_payment_pay
But somehow I got nothing .. maybe you can help me ?
Here is the sample of my code in my Observer.php
public function functioninobserver($observer) {
$orderid = $observer->getEvent()->getInvoice()->getIncrementId();
}
First of all it returns nothing and I think Incrementid is not the same as orderthe number ...
The invoice isn't the same as the order, you can get the order in a slightly roundabout way...
$orderId = $observer->getPayment()->getOrder()->getId();
$orderNumber = $observer->getPayment()->getOrder()->getIncrementId();
Just to clarify; The order ID is used internally in the database. The order number is what's displayed on screen and looks like #100000123.

Magento Product Insert Function

I need to add a custom option to all products as they get saved. For that I need to find the function that inserts the products into the database, which I'm not able to find.
Please, any help would be appreciated.
thanx
$client = new SoapClient('http://www.magentolocal.it/api/?wsdl');
$session = $client->login('productloader', '1234567890');
$sku = "123456";
$attrs['name'] = "Template #1";
$attrs['description'] = "This is the first template.";
$attrs['short_description'] = "This is the short description of the template";
$attrs['websites'] = array('1');
$attrs['price'] = "11.53";
$attrs['categories'] = array('35');
$attrs['images'] = array()
$result = $client->call($session, 'catalog_product.create', array('simple', '63', $sku, $attrs));
echo $result;
$client->endSession($session);
Magento's EAV system is pretty strung out among several files, so you won't find a single function that accomplishes what you want. If you did go looking for it, and changed it, you would also be changing the same save method that mostly every other object in Magento uses, which is probably not what you want.
To do what you want, try setting up an observer/listener on the events that catalog products use when saving, namely catalog_product_save_before or catalog_product_save_after. That way, you don't have to hack the framework.
Hope that helps!
Thanks,
Joe
How about http://www.magentocommerce.com/wiki/doc/webservices-api/api/catalog_product#catalog_product.create?

Resources