How can I change the order life cycle in Magento? - magento

I’m new to magento and as far as I can see, the order states are labeled as "pending", "processing" and "completed". When customer checks out, then the state becomes "pending", and either invoicing or shipping makes the state "processing", and when both are done, state becomes "completed". Please correct me if I am wrong.
So, I’d like to introduce new states but not statuses, I did manage to introduce new statuses when it is in one of the states above, or I could change the labels of the existing states but I want to learn how magento manages the states and how to modify it/add new states. I can modify the core code if required.
So, my question is how do I introduce new states (not statuses) or how do I change/modify the order life-cycle?
I can investigate the source if you tell me which parts of the code is managing the order life-cycle. Any help or clue is appreciated.
PS: I'm using v1.5.1.0 right now.

So in your config.xml you would have something like ...
<config>
<global>
<sales>
<order>
<states>
<my_state translate="label">
<label>My State</label>
<statuses>
<pending default="1"/>
</statuses>
<visible_on_front/>
</my_state>
</states>
</order>
</sales>
</global>
</config>
Then whenever you want the state to change you could override the core or add an observer to change the state (please don't edit the core directly!) with something like (assuming $order is a valid order already loaded and ready to go) ....
$order->setState("my_state");
$order->save();
which will do the default status for the state. if you want to set a status put that in the second parameter so ..
$order->setState("my_state","complete");
$order->save();
HTH

Related

Magmi url_key slug - how to include sku?

I'm trying to figure out the best course of action to change URL keys in the store to be name-sku.html
Currently MAGMI seems to be dictating the url key, which is fine. Some of our products are not imported through MAGMI, so I think I'd probably have to also use the updateurl event to change the url on save -- I found this here for that -- https://magento.stackexchange.com/questions/24869/manufacturer-in-product-url-key
The question is, how is it best to do this within MAGMI? I'm importing about 300,000 products currently. The URL also needs to change, as it currently just uses the default settings.
I've looked into the wiki here -- http://wiki.magmi.org/index.php?title=Value_Replacer -- I guess the following code could work for the way it is currently working -- but what do I actually type into the value replacer box?
{{ Slugger::slug({item.name}) }}
Does anyone know how to add the sku to this as well? Would I just do something like....
{{ Slugger::slug({item.name} ."-". {item.sku}) }}
Also, does anyone know if this automatically adds the dashes and the .html?
Any guidance would be extremely appreciated.
EDIT:
I have successfully tested this as working in my test environment, but it is not working as expected in my production environment. I'll go over what happens, and what I did.
1) I purged the core_url_rewrite table
2) I deleted all cache
3) I set up MAGMI (updated it to the most current version as well, and triple checked all my settings)
4) MAGMI went through fine, and the url key on about 5 products that I checked for were how I would expect
5) During the catalog url rewrites reindex, all url keys disappeared.
6) After the reindex was done, they were now in the default magento format (using just the name, and not the sku)
Any ideas?
The only difference between production and development that I can think of at the moment that may have anything to do with this is the fact that I did create a module based on the linked manufacturer-in-product-url-key question on production. Now that I think of it, maybe that's the main difference that I need to address..... I didn't think reindexing would have anything to do with that, but maybe I'm wrong.
Anyway, if anyone has any insight, I would still appreciate it. I have a feeling maybe MAGMI should also be rewriting the url_path?? I think I read something about that somewhere.
Ok, I figured this out. I think this is the best approach.
I was mistaken in my question. I'm actually looking to add the mpn, not the sku. Either way, that doesn't change the process.
So, the best thing to do is to use the Value Replacer in MAGMI, and to create a custom module that updates the url on the Magento catalog_product_save_before event.
Here's exactly what I did.....
MAGMI settings (Value Replacer)
Replaced attributes: url_key
New value for url_key: {{ Slugger::slug({item.name}.' '.{item.mpn}) }}
URLKeyChange (Magento Module)
app/code/local/My/URLKeyChange/etc/config.xml
<config>
<global>
<models>
<LeathornURLRewrite>
<class>Leathorn_URLKeyChange_Model</class>
</LeathornURLRewrite>
</models>
<events>
<catalog_product_save_before>
<observers>
<LeathornURLRewrite>
<type>singleton</type>
<class>LeathornURLRewrite/observer</class>
<method>updateurl</method>
</LeathornURLRewrite>
</observers>
</catalog_product_save_before>
</events>
</global>
</config>
app/code/local/My/URLKeyChange/Model/Observer.php
class My_URLKeyChange_Model_Observer {
public function updateurl($observer){
Mage::log('My log entry', null, 'mylogfile.log');
if($observer->getEvent()->getProduct()){
$Product=$observer->getEvent()->getProduct();
$Url='';
if(!is_null($Product->getData('name'))):
$Url=$Url.$Product->getData('name');
endif;
if(!is_null($Product->getData('mpn'))):
$Url=$Url.$Product->getData('mpn').'-';
endif;
//Mage::log('My log entry'.$Url, null, 'mylogfile.log');
$Product->setData('url_key',$Url);
}
}
}
app/etc/modules/My_URLKeyChange.xml
<config>
<modules>
<My_URLKeyChange>
<!-- Whether our module is active: true or false -->
<active>true</active>
<!-- Which code pool to use: core, community or local -->
<codePool>local</codePool>
</My_URLKeyChange>
</modules>
</config>
URL key will always be just dash separated strings. Don't worry, magento will add .html in it dynamically. You can see in magnento backend in any product configuration page, url key is without html.
Also magento will add dash in url key, Replace your "-" by space " ". Otherwise it will take last word of name and SKU as one word. :)

Orders with paypal not listing on user dashboard

I have a magento website and I have a problem.
In the user dashboard the orders via Paypal are not listing, it just show a message:"you don't have any order yet", but the orders via cc or other one works perfectly.
I've been exploring sales.xml but I haven't been able to find what can be the cause or how to fix it.
Thank you for your kind help.
It took me a while to figure that out. The problem is not with Paypal but with the order status "Pending Payment".
In the file ../Mage/Sales/etc/config.xml find the block that describes the Pending Payment order status and add this line:
<visible_on_front>1</visible_on_front>
If you are not sure where to put it just look the rest of the order statuses and see that all of them have the visible_on_front attribute set to 1.
Your code block should look like this:
<pending_payment translate="label">
<label>Pending Payment</label>
<statuses>
<pending_payment default="1"/>
</statuses>
<visible_on_front>1</visible_on_front>
</pending_payment>

How to sell coupoun code in magento?

How to sell a coupon code in magento.
The coupon code that can be gift to a friend, so that he can buy products with that code?
Log in to Magento Admin.
Click on Promotions –> Shopping Cart Price Rule.
On Right side click on Add new rule.
Fill in the fields be sure to give the rule a Name and Description and decide whether or not you are choosing ALL Customer groups or not.
In the General Information page in the coupon menu, select the Specific Coupon option.
Enter a code in the coupon code field (can be letters or numbers).
In the Uses Per Coupon field, specify the number of times a customer can use this coupon code if you would like to provide a limit. If not, leave blank.
In the Uses Per Customer field, specify the number of times a customer can use this promotion if you would like to provide a limit. If not, leave blank.
In the From/To Date menu, select a time frame for your coupon if you would like to provide one. If not, leave blank.
IDEA:
create coupon rule with auto generated coupons
create specific controller which will generate coupon and display it to customer
create downloadable product with link to our controller
NOTE
THIS IS MY FIRST INITIAL IDEA AND I'M NOT SURE IF IT IS PROPER SOLUTION.
I've never dealt with downloadable products before.
FIRST STEP CREATE COUPONS
When adding new rule to Shopping Cart Price Rules fill all required fields, remember to select appropriate customer groups (include NOT LOGGED IN if coupon should be available for guest customers).
Then, in Coupon select Specific Coupon, and select Use Auto Generation in checkbox below. Uses per Coupon set to 1. You can also set time limit if you want to.
Then save.
After saving new tab will appear on the left: Manage Coupon Codes.
There you can generate many coupon codes by setting qty (amount of coupons to generate), code length, code format (letters and digits, letters, only digits), code prefix and/or suffix, and if to include dash every x characters.
Remember code length applies only to generated part without dashes, prefix or suffix.
You don't need to generate anything yet, I'm just explaining some properties which I will refer to later on.
SECOND STEP PREPARE MODULE
Create path app/code/local/Example/Coupon, and two directories there etc and controllers.
In etc create config.xml
<?xml version="1.0"?>
<config>
<modules>
<Example_Coupon>
<version>0.1</version>
</Example_Coupon>
</modules>
<global>
</global>
<frontend>
<routers>
<example_coupon>
<use>standard</use>
<args>
<module>Example_Coupon</module>
<frontName>mycoupon</frontName>
</args>
</example_coupon>
</routers>
</frontend>
</config>
Then IndexController.php in controllers:
<?php
class Example_Coupon_IndexController extends Mage_Core_Controller_Front_Action {
const RULE_ID = 4;
public function indexAction() {
$rule = Mage::getModel('salesrule/rule')->load(self::RULE_ID);
$generator = Mage::getModel('salesrule/coupon_massgenerator');
$generator->setFormat(Mage_SalesRule_Helper_Coupon::COUPON_FORMAT_ALPHANUMERIC);
$generator->setDash(false);
$generator->setLength(6);
$generator->setPrefix('EX');
$rule->setCouponCodeGenerator($generator);
$rule->setCouponType(Mage_SalesRule_Model_Rule::COUPON_TYPE_AUTO);
$coupon = $rule->acquireCoupon();
$code = $coupon->getCode();
$coupon->setType(Mage_SalesRule_Helper_Coupon::COUPON_TYPE_SPECIFIC_AUTOGENERATED)->save();
die($code);
}
}
Here actual coupon is being generated and saved (upon requesting our custom url). Note that here all properties of code are defined (described above). You should edit this to suit your needs.
You can access this by visiting http://www.yourstore.com/mycoupon/index
Finally you need to enable module in etc/app/modules
create Example_Coupon.xml
<?xml version="1.0"?>
<config>
<modules>
<Example_Coupon>
<active>true</active>
<codePool>local</codePool>
</Example_Coupon>
</modules>
</config>
THIRD STEP ADD DOWNLOADABLE PRODUCT
I'm not going to cover all the details here, there is a good tutorial: https://www.hostknox.com/tutorials/magento/downloadable-products
What you finally need to do is to provide previously generated url as an downloadable link: http://www.yourstore.com/mycoupon/index
ISSUES
It works, but:
There is no validation whatsoever if customer have all rights to download this coupon or not. I'm not sure, maybe magento covers this or maybe not. There is an option to disable sharing in magento, so I would expect magento to hide this final url from customer.
You would probably need to expand my example module, because there should be downloadable file at the end - most likely some kind of PDF. Not simply echoing new generated coupon.
This file should be stored somewhere on disk, and served again upon request from same customer and same order.

Magento backend_model - do I need to specify for each config field?

If I want to do something extra when a particular configuration field for my custom module is saved (over and above saving to the Magento core config table), I can just specify a backend_model for that field in my system.xml, and have that backend model class extend Mage_Core_Model_Config_Data, override _afterSave, and put my extra stuff in that method.
But what if I have several fields I want to do this for. I don't want the behaviour to be to save field1 and call my afterSave for that field, save field2 and call my afterSave for that field, etc. I'd rather that all the fields were saved to the Magento core config table, and then I do my extra stuff.
Is that possible? I thought I might be able to achieve that using event/observer. So in my config.xml, <adminhtml> section, I added an observer as follows:
<events>
<admin_system_config_changed_mysection>
<observers>
<mypfx_admin_system_config_changed_mysection>
<class>mymodule/adminhtml_system_config_backend_configSaveObserver</class>
<method>myConfigSaved</method
</mypfx_admin_system_config_changed_mysection>
</observers>
</admin_system_config_changed_mysection>
</events>
but my observer method is not called when the config is saved. Maybe I have the wrong event name? The "mysection" bit on the end of the event name I was guessing had to match the section from system.xml:
<sections>
<mysection translate="label" module="mymodule">
...
<groups>
...
</groups>
</mysection>
</sections>
Thanks.
The event you're trying to listen for doesn't exist. Here's what you want to do, and some tips for picking the right event in the future.
First, every event is fired in Magento by the Mage::dispatchEvent method. Search the core code for these calls and you'll always know the name of the event you want to listen for.
$ ack 'admin_system_config_changed_'
Adminhtml/controllers/System/ConfigController.php
136: Mage::dispatchEvent("admin_system_config_changed_section_{$section}",
From the above, you can see the name of the event vs. what you thought it was
admin_system_config_changed_section_{$section}
admin_system_config_changed_mysection
So, it looks like you're missing the section before your own section name.
Second, while working on a development box, the best way to find the event you're looking for is to log things at the source. Temporarily add some debugging code to the dispatchEvent function.
#File: app/Mage.php
public static function dispatchEvent($name, array $data = array())
{
//either one of the lines below should do it. One uses Magento's
//built in logging, the other uses something more crude
#Mage::Log($name);
#file_put_contents('/tmp/test.log',"$name\n",FILE_APPEND);
Varien_Profiler::start('DISPATCH EVENT:'.$name);
$result = self::app()->dispatchEvent($name, $data);
#$result = self::registry('events')->dispatch($name, $data);
Varien_Profiler::stop('DISPATCH EVENT:'.$name);
return $result;
}
This will dump a huge list of event names out to your log. I typically use OS X's Console.app to view the log file during the request, copy the lines out, sort and remove duplicates, and then end up with a list like this
admin_system_config_changed_section_commercebug
admin_user_load_after
admin_user_load_before
adminhtml_block_html_before
adminhtml_controller_action_predispatch_start
application_clean_cache
controller_action_layout_generate_blocks_after
controller_action_layout_generate_blocks_before
controller_action_layout_generate_xml_before
controller_action_layout_load_before
controller_action_layout_render_before
controller_action_layout_render_before_adminhtml_system_config_edit
controller_action_postdispatch
controller_action_postdispatch_adminhtml
controller_action_postdispatch_adminhtml_system_config_edit
controller_action_postdispatch_adminhtml_system_config_save
controller_action_predispatch
controller_action_predispatch_adminhtml
controller_action_predispatch_adminhtml_system_config_edit
controller_action_predispatch_adminhtml_system_config_save
controller_front_init_before
controller_front_init_routers
controller_front_send_response_after
controller_front_send_response_before
core_abstract_load_after
core_abstract_load_before
core_block_abstract_prepare_layout_after
core_block_abstract_prepare_layout_before
core_block_abstract_to_html_after
core_block_abstract_to_html_before
core_collection_abstract_load_after
core_collection_abstract_load_before
core_config_data_load_after
core_config_data_save_after
core_config_data_save_before
core_config_data_save_commit_after
core_layout_block_create_after
core_locale_set_locale
core_session_abstract_add_message
core_session_abstract_clear_messages
http_response_send_before
model_load_after
model_load_before
model_save_after
model_save_before
model_save_commit_after
resource_get_tablename
store_load_after
store_load_before
You still need to use some intelligence guessing to figure out which event you want, but they're named intuitively enough that you can usually find what you're looking for.
You need to tie your observer method to a specific Magento event (you can add your own, but need to find when you want it to be fired and add your own dispatchEvent call). If Magento has an event built in, use that event name in the config.
There's a pdf of the built in event lists on the web - google for it & you'll find it.

remove particular order status from list in magento

I am using extension
http://www.magentocommerce.com/magento-connect/eitai2001/extension/1468/order-status/reviews#reviews
of magento which provide a lot of order status..but my need is limited ..so i want to remove some status from my order-status list... how can i do this..please help
Thanks!
Edited
I made change in config.xml
If I comment any order-status ... like this
<!--<processing_cc_settled translate="label"><label>Processing - Credit Card has been Settled</label></processing_cc_settled>-->
but in combobox(where status shows in admin site )still processing_cc_settled appears at the same place where Processing - Credit Card has been Settled showing up before commenting
If this is the same extension that I have seen installed one client's site then it should have a config.xml file included in its /etc subdirectory (NOTE: not the global /app/etc but somwhere in the /app/code/community/ folder). In this file you will find definitions for all the additional statuses. Just comment out the ones you don't want. You can also change the ones that you leave so that they are better suited to your needs.
CLARIFICATION:
Here's a piece of my config.xml:
<config>
<modules>
<Mage_Sales_Community>
<version>0.1.2</version>
</Mage_Sales_Community>
</modules>
<global>
<sales>
<order>
<statuses>
<!--Complete Status Descriptions-->
<complete translate="label"><label>Dispatched</label></complete>
<!--Cancelled Status Descriptions-->
<canceled translate="label"><label>Suspended</label></canceled>
<!--<canceled_discontinued translate="label"><label>Suspended - No stock</label></canceled_discontinued> -->
<!-- /* Additional custom statuses will go here */ -->
<!-- Format of a status description is <name_of_status translate="label"><label>Name of Status</label?</name_of_status> -->
</statuses>
</order>
</sales>
</global>
</config>
Note that complete and canceled have a different label defined. There is also a canceled_discontinued status, but it is not used as the whole entry is commented out. If the entry stays on the order comments dropdown after you have commented it make sure that you have refreshed the cache...
I add/remove magento statuses in install scripts, using something like below:
<?php
$installer = $this;
/*
Possible states:
Mage_Sales_Model_Order::STATE_CANCELED
Mage_Sales_Model_Order::STATE_CLOSED
Mage_Sales_Model_Order::STATE_COMPLETE
Mage_Sales_Model_Order::STATE_HOLDED
Mage_Sales_Model_Order::STATE_NEW
Mage_Sales_Model_Order::STATE_PAYMENT_REVIEW
Mage_Sales_Model_Order::STATE_PENDING_PAYMENT
Mage_Sales_Model_Order::STATE_PROCESSING
*/
$installer->startSetup();
// Get status model
$status = Mage::getModel('sales/order_status');
// Delete some statuses
$status->setStatus('processing_cc_settled')->delete();
$status->setStatus('another_status_code_to_delete')->delete();
//Add a new status
$status->setStatus('holded_cc_error')
->setLabel('On Hold: CC Error')
->assignState(Mage_Sales_Model_Order::STATE_HOLDED)
//for example, use any available existing state from above
->save();
//To set an order to this status:
//$order->setData('state', "holded");
//$order->setStatus("holded_cc_error");
//$order->save();
$installer->endSetup();

Resources