How to sell coupoun code in magento? - 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.

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. :)

How can I sort product listing by "best value", that is, an attribute calculated from (price divided by size)?

I have several homogeneous products of varying sizes, in grams. I would like to provide a value by which the customer could sort the product listing by best value, as calculated from price divided by mass/weight/size. I already have one attribute which is the size in grams and is defined by a number followed by a 'g' (eg, 100g, 500g), so to make it easier, I suppose I would create another attribute that reflects the weight without the 'g' appended to the value.
However, I don't know if it's possible to automatically calculate value with attributes (ie, create an attribute that automatically calculates the price/weight value without any more work on my part). Or, do I have to do some custom code, and where would that be? Any suggestions appreciated :)
You can achieve this by creating an custom module.
In this module you have to create an observer on "catalog_product_save_after" event.
As i don't know which Magento version are you using, i will just give you some snippets that may or may not work in your current version, but will definitely help you understand.
This is how your custom module 'config.xml' should look like:
<global>
<events>
<catalog_product_save_after>
<observers>
<calculateBestValue>
<type>singleton</type>
<class>Foo_Bar_Model_Observer</class>
<method>calcBestValue</method>
</calculateBestValue>
</observers>
<catalog_product_save_after>
</events>
</global>
And this is your 'Observer.php' model.
public function calcBestValue(Varien_Event_Observer $observer){
$_product = $observer->getEvent()->getProduct();
// Here you can access your productData and do your logic
// Ex: $_product->getData('weight');
// Ex: $_product->getData('price');
// Ex: $_product->getData('mass');
// Ex: $_product->getData('size');
/* Your best value logic here */
// After that you just need to set the bestValue attribute in the product
$_product->setBestValue($bestValue);
}
You will need to create an catalog attribute for "Best Value" and associate it in an attribute set.
After the "best value logic" is ready you just need to enable your custom attribute for sorting in product list. See https://magento.stackexchange.com/questions/9928/adding-custom-attributes-to-product-list

Shopping cart Rules doing the opposite

We have added a "yes/no" product attribute labelled: "Allow this product to have coupons applied?" with a default value of "yes".
The reason for this is, that we never want the sales team to give discounts on certain core products out of the thousands available.
We could add these products as SKU's to every one of the thousand coupons created, but that can then be changed by accident, or improperly listed by the sales team creating the coupons.
We therefore have asked them to add this rule to their new coupons, to exclude these products. We also updated all the other coupons via code, to include this rule.
When I test the rule on Magento 1.5, Community edition, it does exactly the opposite of what the condition says.
When I test the same rule on our other Enterprise solution (magento 1.9), it actually does what it is supposed to.
When I have one product in the shopping cart, being a product that has this attribute set to "No", and I apply a coupon code, it accepts the coupon code. On the other system (Enterprise) it rejects it, as it should.
Has anyone else came across this?
UPDATE:
Ok, this just got more interesting.
When I stepped through the Rule classes, the product was passed, but the product attribute did not come through. The scope is set to website, and I did check that is it saved under that scope in the back end.
This is really weird...
On the enterprise system, the product attribute, with the same configuration and data is coming through.
I even tried to not have double negatives, with another new attribute, to make sure.
Running against the following rule, the coupon gets refused: "Coupon code is not valid."
This is correct, as the product is set as "Can use coupon? = No".
But the problem is that I do not want to do a check against all the other products, but rather against, the few that has been marked as "No".
This is not going to work for us anyway, as we don't want to apply a coupon if there is any product in the cart that has a value of "No" for the attribute "Can use coupon?".
This is a double negative rule, no matter which way you look at it.
I stepped through /app/code/core/Mage/SalesRule/Model/Rule/Condition/Product.php through the validate() function, when adding the product a second time, and the data shows up in the attributes, but when I go and re-apply the coupon, and step through this function again, those values are gone. this is weird.
When adding new product to shopping cart:
When Re-applying Coupon Code:
Where the heck did description and some of the other attributes disappear to?
UPDATE NOTE I am getting a bit closer.
When I change the following validate() function in "/app/code/core/Mage/SalesRule/Model/Rule/Condition/Product.php" from the following:
public function validate(Varien_Object $object)
{
$product = false;
if ($object->getProduct() instanceof Mage_Catalog_Model_Product) {
$product = $object->getProduct();
} else {
$product = Mage::getModel('catalog/product')
->load($object->getProductId());
}
$product
->setQuoteItemQty($object->getQty())
->setQuoteItemPrice($object->getPrice())
->setQuoteItemRowTotal($object->getBaseRowTotal());
return parent::validate($product);
}
To This:
public function validate(Varien_Object $object)
{
$product = false;
$product = Mage::getModel('catalog/product')
->load($object->getProductId());
$product
->setQuoteItemQty($object->getQty())
->setQuoteItemPrice($object->getPrice())
->setQuoteItemRowTotal($object->getBaseRowTotal());
return parent::validate($product);
}
It then works fine!
Found out the actual problem, and here is what I did for the solution:
The following bit gets the product data for the Shopping Cart Price Rules:
if ($object->getProduct() instanceof Mage_Catalog_Model_Product) {
$product = $object->getProduct();
} else {
$product = Mage::getModel('catalog/product')
->load($object->getProductId());
}
The culprit is this line: $product = $object->getProduct();
That calls a line from the "Quote/Item/Abstract.php" being: $product = $this->_getData('product');
It will simply get the cached data from the system, and that consists of a selection like this:
$productCollection = Mage::getModel('catalog/product')->getCollection()
->setStoreId($this->getStoreId())
->addIdFilter($this->_productIds)
->addAttributeToSelect(Mage::getSingleton('sales/quote_config')->getProductAttributes())
->addOptionsToResult()
->addStoreFilter()
->addUrlRewrite()
->addTierPriceData();
The problem with this is, the following line:
->addAttributeToSelect(Mage::getSingleton('sales/quote_config')->getProductAttributes())
That will get only the attributes defined in the Config.xml for quote items being:
<quote>
<item>
<product_attributes>
<sku/>
<type_id/>
<name/>
<status/>
<visibility/>
<price/>
<weight/>
<url_path/>
<url_key/>
<thumbnail/>
<small_image/>
<tax_class_id/>
<special_from_date/>
<special_to_date/>
<special_price/>
<cost/>
<is_recurring/><!-- for totals calculation, placing and processing order -->
<recurring_profile/><!-- for placing order -->
<gift_message_available/>
</product_attributes>
</item>
</quote>
This will not get the new custom attribute that I want, and the rule will never be able to compare the data with the expected value properly.
Adding the following xml to my custom sales module config.xml, which have some observers in it for other stuff, fixes the problem.
<sales>
<quote>
<item>
<product_attributes>
<exclude_from_coupon/>
</product_attributes>
</item>
</quote>
</sales>
Then it all works!
Why did it work in the enterprise version of magento
Don't really know. I think Enterprise is maybe looking for settings on the actual Attribute definition, and includes it to the list of Attributes to collect, and then return the data for it, my guess. I will research that, and update it here to the post.
At least I now know what the real issue is, and why the Shopping Cart Rule Condition (coupon) failed.
Thanks to all those who helped me with some good questions and suggestions.
Another day wiser with magento...
I tested this with Magento CE 1.5.1.0 and cannot reproduce your problem. For me, it works.
Please recheck the following for your CE installation:
You set the default attribute value to "yes"
You did reindex the indexes as required.
You edited one test product and explicitly set the attribute value to "no".
(Just to be sure) You updated the caches.
You set the shopping cart price rule exactly as described above (apply if conditions are FALSE and allow is set to NO).
If you don't have any custom code interfering, this should work. It does in the default installation.
If it still doesn't work:
What happens if you rephrase the condition to only apply if all conditions are TRUE and allow is set to TRUE (does remove the double negative which is more complicated)?
Please post screenshots of the backend forms for shopping cart price rule, product attribute and the product.

How can I change the order life cycle in 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

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