Magento observer - after/before order place - magento

In my website there is a product having qty as 500 (For Example), if a user ordered 600 qty the payment must process for available and remaining 100 qty must get paid after the stock avail. At present user can only pay for 500 qty.
Can anybody help to update the qty to 600 after placing an order programmatically? So that admin can manage invoice/shipment for first 500 qty and after user pay for the 100 again invoice/shipment.
Any other option to manage this?
Current observer code (not working):
public function myfucn($observer){
$data = $observer->getEvent()->getOrder();
$id = '40';
$qty = 600;
$_product = Mage::getModel('catalog/product')->load($id);
$cart = Mage::getModel('checkout/cart');
$cart->init();
$cart->addProduct($_product, array('qty' => $qty));
$cart->save();
Mage::getSingleton('checkout/session')->setCartWasUpdated(true);}
Config.xml
<modules>
<Ajt_PlaceOrder>
<version>0.0.1</version>
</Ajt_PlaceOrder>
</modules>
<global>
<models>
<placeprder>
<class>PlaceOrder_Model</class>
</placeprder>
</models>
<events>
<sales_quote_save_before>
<observers>
<ajt_placeorder_model_observer>
<class>Ajt_PlaceOrder_Model_Observer</class>
<method>mymethod</method>
</ajt_placeorder_model_observer>
</observers>
</sales_quote_save_before>
</events>
</global>
The above observer code im trying is to add product to cart. But if you have solution to update the qty of already existed item at cart before place an order please guide me.

Related

Shipping costs remove in cart grand total in Magento 1.9

I have add product in cart after that I have checkout page, now in checkout page I will fill the all details also select shipping method but not proceed order.
After that I have going on cart then my grand total showing with shipping cost so I have to remove this shipping cost in my cart grand total because customer can confusion how that total will increase.
So I will only show product total with text in cart if I am select shipping method they can't be display in cart only show at checkout time.
Is this possible in Magento 1.9?
Try this Create an observer in checkout_cart_save_before
<frontend>
<events>
<checkout_cart_save_before>
<observers>
<your_module_shipping_observer>
<type>singleton</type>
<class>Your_Module/observer</class>
<method>setShipping</method>
</your_module_shipping_observer>
</observers>
</checkout_cart_save_before>
</events>
</frontend>
And in your observer try this
public function setShipping($observer) {
$event = $observer->getEvent();
$cart = $event->getCart();
$shippingaddress = $cart->getQuote()->getShippingAddress();
$shippingaddress->setShippingMethod('')->save();
return;
}

Minimum total of items in shopping cart to allow checkout

I'm trying to find a configuration that allows one of my customer groups (wholesale) to add any given number of items to their shopping carts, but have a restricted checkout equal or greater than 16 items in total.
For example:
8 items from product A
2 items from product B
6 items from product C
That would be a total of 16 items and it would be possible for them to checkout.
I tried configuring the Minimum Qty Allowed in Shopping Cart, but then, they have to get 16 items of each product.
Do you know if there is a way to configure, add an extension or hardcode something to solve this problem?
Thank you for your time!
Easiest way is to set a minimum order amount
/admin/system_config/edit/section/sales
To implement a minimum item restriction, you can place an observer to fire on one page checkout which checks the item quantity it cart and redirects you back to the cart if its below your threshold with a message.
Full prototype, flavor as needed:
app\etc\modules\Spirit_Cms.xml
<?xml version="1.0"?>
<config>
<modules>
<Spirit_Cms>
<active>true</active>
<codePool>local</codePool>
</Spirit_Cms>
</modules>
</config>
app\code\local\Spirit\Cms\etc\config.xml
<?xml version="1.0"?>
<config>
<modules>
<Spirit_Cms>
<version>0.0.1</version>
</Spirit_Cms>
</modules>
<frontend>
<events>
<controller_action_predispatch_checkout_onepage_index>
<observers>
<spirit_cms_restrict_checkout>
<class>Spirit_Cms_Model_Observer</class>
<method>restrictCheckout</method>
</spirit_cms_restrict_checkout>
</observers>
</controller_action_predispatch_checkout_onepage_index>
</events>
</frontend>
<global>
<models>
<spirit_cms>
<class>Spirit_Cms_Model</class>
</spirit_cms>
</models>
</global>
</config>
app\code\local\Spirit\Cms\Model\Observer.php
<?php
class Spirit_Cms_Model_Observer
{
public function restrictCheckout( $oObserver )
{
// Ensure we only observe once.
if( Mage::registry( 'restrict_checkout_flag' ) )
{
return $this;
}
else
{
$oQuote = Mage::getSingleton( 'checkout/cart' )->getQuote();
$oCartItems = $oQuote->getAllItems();
$iTotalQty = 0;
foreach( $oCartItems as $oCartItem )
{
$iTotalQty = $iTotalQty + $oCartItem->getQty();
}
if( $iTotalQty < 12 )
{
$oSession = Mage::getSingleton( 'checkout/session' );
$oSession->addError( 'Please add at least 12 items to your cart.' );
Mage::app()->getResponse()->setRedirect( Mage::getUrl( 'checkout/cart' ) );
}
Mage::register( 'restrict_checkout_flag', 1, TRUE );
}
}
}
?>

Disable 'General' group sales in Magento

I'm looking to disable sales on our Magento site for any customers in the 'General' customer group. We have a tiered customer group system setup, with different tax rules etc., but I can't figure out how to disable 'general' as a group that can purchase from the site. I don't want new customers signing up able to purchase without being assigned a group first.
Thanks.
Here is my idea: set up observer on controller_action_predispatch_checkout_onepage_index event, which will fire before checkout page is loaded. In observer we check if customer belongs to certain group and if so we redirect him to cart page and show error message.
Translated to Magento code, it would look like this:
Firs we hook on the event in our config.xml
<frontend>
<events>
<controller_action_predispatch_checkout_onepage_index>
<observers>
<your_module>
<class>your_module/observer</class>
<method>banCheckout</method>
</your_module>
</observers>
</controller_action_predispatch_checkout_onepage_index>
</events>
</frontend>
and in our observer:
public function banCheckout(Varien_Event_Observer $observer)
{
$customerSession = Mage::getSingleton('customer/session');
if (!$customerSession->isLoggedIn()) {
return $this;
}
$groupId = $customerSession->getCustomer()->getGroupId();
if ($groupId == 1) {
Mage::getSingleton('checkout/session')->addError(
Mage::helper('checkout')->__('Your error message.')
);
$action = $observer->getEvent()->getControllerAction();
$action->getResponse()->setRedirect(Mage::getUrl('checkout/cart'));
$action->setFlag('', Mage_Core_Controller_Varien_Action::FLAG_NO_DISPATCH, true);
}
return $this;
}
Keep in mind this is just an example, and a pretty simple one.

Implementing an event observer in Magento

Im starting to understand how Magento Event Observers work and hoping someone could help me figure out what Event I would need to seek out to get my potential module to work. I have certain products that have an attribute called "child skus" which is an array of SKU numbers of other products in my inventory.
Example:
Name: Totally Awesome Product
SKU: TP-1212
Child SKUS: GP-3232,FD-4332,VC-5332
Every time someone purchases TP-1212 I also need Magento to deduct that quantity from those child skus behind the scenes. Does anyone know if there is an event dispatcher for after a purchase has been completed that would handle something like that??
This is a little tricky and there are most likely some edge cases not covered in the below code - Also the answer assumes the following:
You wish to reduce the stock level of all associated products of the parent just purchased
You wish to reduce by only 1
There are no further complications or other conditions that must be met/dealt with
Code:
So, you obviously need to create a module first. The config.xml needs to declare the observer which will listen to checkout_type_onepage_save_order_after. (Note: there are other events you can listen to in order to achieve your goal).
The config.xml will contain the following code at a minimum:
<?xml version="1.0"?>
<config>
<modules>
<YourCmpany_YourModule>
<version>1.0.0</version>
</YourCmpany_YourModule>
</modules>
<frontend>
<events>
<checkout_type_onepage_save_order_after>
<observers>
<yourmodule_save_order_observer>
<class>YourCompany_YourModule_Model_Observer</class>
<method>checkout_type_onepage_save_order_after</method>
</yourmodule_save_order_observer>
</observers>
</checkout_type_onepage_save_order_after>
</events>
</frontend>
<global>
<models>
<yourmodule>
<class>YourCompany_YourModule_Model</class>
</yourmodule>
</models>
</global>
</config>
Then in your observer:
<?php
class YourCompany_YourModule_Model_Observer
{
public function checkout_type_onepage_save_order_after(Varien_Event_Observer $observer)
{
$order = $observer->getEvent()->getOrder();
/**
* Grab all product ids from the order
*/
$productIds = array();
foreach ($order->getItemsCollection() as $item) {
$productIds[] = $item->getProductId();
}
foreach ($productIds as $productId) {
$product = Mage::getModel('catalog/product')
->setStoreId(Mage::app()->getStore()->getId())
->load($productId);
if (! $product->isConfigurable()) {
continue;
}
/**
* Grab all of the associated simple products
*/
$childProducts = Mage::getModel('catalog/product_type_configurable')
->getUsedProducts(null, $product);
foreach($childProducts as $childProduct) {
/**
* in_array check below, makes sure to exclude the simple product actually
* being sold as we dont want its stock level decreased twice :)
*/
if (! in_array($childProduct->getId(), $productIds)) {
/**
* Finally, load up the stock item and decrease its qty by 1
*/
$stockItem = Mage::getModel('cataloginventory/stock_item')
->loadByProduct($childProduct)
->subtractQty(1)
->save()
;
}
}
}
}
}
There are several possibilities with simplest being just checkout_type_onepage_save_order_after
however do you also need to revert the stock if order get's cancelled or execute this only when payment is made or items are shipped?

How to send category based order emails in magento?

I have two root categories in my magento site. One is "Home Products" and the other is "Office products".
These two root categories have some sub categories also.
I want to send "Home Products" related orders to this email address "email_home#example.com",
And to send "Office Products" related orders to this email address "email_office#example.com".
How will I do this?
I suggest you to write own Observer to order.
sales_order_place_after
event suits best for your purpose.
If buyer can add to shopping cart items only from 1 cateogry.
Your module should:
Get order via observer.
Get order first item and get it's category
Choose email based on category
Send email
public function sendOrder(){
$order = $observer->getEvent()->getOrder();
...
//Implement logic here
...
$emailTemplate = Mage::getModel('core/email_template')
->loadDefault('your_template');
$emailTemplateVariables = array();
$emailTemplateVariables['order'] = $order;
$emailTemplate->setSenderName('Your shops name');
$emailTemplate->setSenderEmail('addres#from.com');
$emailTemplate->setTemplateSubject(Subject');
$emailTemplate->send('to#addres.com','Name', $emailTemplateVariables);
}
Update 1
First of all I insist that you see the link I provide in the comments area.
Then:
To create module:
Create in app/etc/modules/ Company_Module.xml file. With the content similiar to this one:
true
local
This eill tell magento, that in app/code/local/Company/Module there is something interesting to watch.
Create proper folder and file structure.
For you module I think it would be enough:
Company
-Module
--etc
---config.xml
--Model
---Observer.php
--Helper
---Data.php
Magento should know everything about your module. Moreover you should define observer for event.
Important note: we will catch Magento's event. Not ours.
config.xml:
<?xml version="1.0"?>
<config>
<modules>
<Company_Module>
<version>0.1.0</version>
</Company_Module>
</modules>
<global>
<models>
<company_module>
<class>Company_Module_Model</class>
</company_module>
</models>
<helpers>
<cmod>
<class>Company_Module_Helper</class>
</cmod>
</helpers>
<events>
<sales_order_place_after>
<observers>
<sales_order_place_after_observer>
<class>company_module/observer</class>
<method>handleOrder</method>
</sales_order_place_after_observer>
</observers>
</sales_order_place_after>
</events>
</global>
</config>
Data.php - It is empty but it should be.
class Company_Module_Helper_Data extends Mage_Core_Helper_Abstract{
}
Observer.php
class Company_Module_Model_Observer{
public function handleOrder($observer){
$order = $observer->getEvent()->getOrder();
...
//Implement logic here
...
$emailTemplate = Mage::getModel('core/email_template')
->loadDefault('your_template');
$emailTemplateVariables = array();
$emailTemplateVariables['order'] = $order;
$emailTemplate->setSenderName('Your shops name');
$emailTemplate->setSenderEmail('addres#from.com');
$emailTemplate->setTemplateSubject(Subject');
$emailTemplate->send('to#addres.com','Name', $emailTemplateVariables);
}
}
#Jevgeni (and anyone else needing the link), the Link for Magento has moved
http://www.magentocommerce.com/wiki/5_-_modules_and_development/0_-_module_development_in_magento/customizing_magento_using_event-observer_method

Resources