2

is it possible with Magento (1.9) to create a custom, 10-digit, voucher code for a user/order after the order was placed and paid?
I also need to output it in the PDF or in the user backend.

I need those codes to identify the user at certain events.

Is this possible?

Thank you!

4
+25

Yes it is quite possible to create custom voucher codes in magento through programming.

You have to create an observer to listen to the order success event and create the coupon based on your needs. Here is a sample code to help you out

    $model = Mage::getModel('salesrule/rule');
    $model->load(Mage::getStoreConfig('newsletter/coupon/roleid'));
    $massGenerator = $model->getCouponMassGenerator();
    $massGenerator->setData(array(
        'rule_id' => <YOUR_ROLE_ID>,
        'qty' => 1,
        'length' => 10,
        'format' => <VOUCHER_FORMAT>,
        'prefix' => <PREFIX>, //if any
        'suffix' => <SUFIX>, //if any
        'dash' => <DASHES>,
        'uses_per_coupon' => 1,
        'uses_per_customer' => 1
    ));
    $massGenerator->generatePool();
    $generated = $massGenerator->getGeneratedCount();
    $latestCuopon = max($model->getCoupons());
    $couponData = $latestCuopon->getData();
    $coupon_code = $couponData['code'];

You can add any conditionals you want to check for specific customers or orders. Hope this helps.

NOTE: You need to have promotion enabled for this to work.

EDIT

If you want to add extra data with orders it is advisable to create a custom module.

In your config.xml add observer

<events>
     <sales_quote_save_before> <!-- Event Called Before Quote Object is saved -->
         <observers>
                   <save_before>
                    <type>singleton</type>
                    <class>TM_Custom_Model_Observer</class> <!-- Over Model Class -->
                    <method>saveQuoteBefore</method> <!-- name of function -->
                  </save_before>
        </observers>
      </sales_quote_save_before>
      <sales_quote_save_after> <!-- Event called After Quote Object is saved -->
         <observers>
                   <save_after>
                        <type>singleton</type>
                        <class>TM_Custom_Model_Observer</class> <!-- Over Model Class -->
                        <method>saveQuoteAfter</method> <!-- name of function -->
                  </save_after>
        </observers>
      </sales_quote_save_after>
      <sales_quote_load_after> <!-- Event called when Quote Object is loaded -->
            <observers>
                <load_after>
                        <type>singleton</type>
                        <class>TM_Custom_Model_Observer</class> <!-- Over Model Class -->
                        <method>loadQuoteAfter</method> <!-- name of function -->
                 </load_after>
             </observers>
      </sales_quote_load_after>
      <sales_model_service_quote_submit_after> <!-- Event called after order placed -->
            <observers>
                <sales_model_service_quote_submit_after>
                        <type>singleton</type>
                        <class>TM_Custom_Model_Observer</class> <!-- Over Model Class -->
                        <method>saveOrderAfter</method> <!-- name of function -->
                 </sales_model_service_quote_submit_after>
             </observers>
      </sales_model_service_quote_submit_after>
      <sales_order_load_after> <!-- Event called after Order Object is loaded -->
            <observers>
                <sales_order_load_after>
                        <type>singleton</type>
                        <class>TM_Custom_Model_Observer</class> <!-- Over Model Class -->
                        <method>loadOrderAfter</method> <!-- name of function -->
                 </sales_order_load_after>
             </observers>
      </sales_order_load_after>
</events>


<models>
 ...
 <sales>
            <rewrite>
                <order>TM_Custom_Model_Sales_Order</order>
            </rewrite>
        </sales>
        <custom>
            <class>TM_Custom_Model</class>
            <resourceModel>custom_mysql4</resourceModel>
        </custom>
        <custom_mysql4>
            <class>Excellence_Custom_Model_Mysql4</class>
            <entities>
                <custom_quote>
                    <table>sales_quote_custom</table>
                </custom_quote>
                <custom_order>
                    <table>sales_order_custom</table>
                </custom_order>
            </entities>
        </custom_mysql4>
    </models>

Model/Observer.php

 class TM_Custom_Model_Observer{

public function saveQuoteBefore($evt){

     $quote = $evt->getQuote();
     $post = Mage::app()->getFrontController()->getRequest()->getPost();
     $quote->setVoucher($var);


}

public function saveQuoteAfter($evt){
    $quote = $evt->getQuote();
    if($quote->getVoucher()){
        $var = $quote->getVoucher();
        if(!empty($var)){
            $model = Mage::getModel('custom/custom_quote');
            $model->deteleByQuote($quote->getId(),'voucher');
            $model->setQuoteId($quote->getId());
            $model->setKey('voucher');
            $model->setValue($var);
            $model->save();
        }
    }


}


public function loadQuoteAfter($evt){
    $quote = $evt->getQuote();
    $model = Mage::getModel('custom/custom_quote');
    $data = $model->getByQuote($quote->getId());
    foreach($data as $key => $value){
        $quote->setData($key,$value);
    }
}


public function saveOrderAfter($evt){
    $order = $evt->getOrder();
    $quote = $evt->getQuote();
    if($quote->getVoucher()){
        $var = $quote->getVoucher();
        if(!empty($var)){
            $model = Mage::getModel('custom/custom_order');
            $model->deleteByOrder($order->getId(),'voucher');
            $model->setOrderId($order->getId());
            $model->setKey('voucher');
            $model->setValue($var);
            $order->setVoucher($var);
            $model->save();
        }
    }

}


public function loadOrderAfter($evt){
    $order = $evt->getOrder();
    $model = Mage::getModel('custom/custom_order');
    $data = $model->getByOrder($order->getId());
    foreach($data as $key => $value){
        $order->setData($key,$value);
    }
}

}

Model/sales/Order.php

class TM_Custom_Model_Sales_Order extends Mage_Sales_Model_Order{
public function hasCustomFields(){
    $var = $this->getVoucher();
    if($var && !empty($var)){
        return true;
    }else{
        return false;
    }
}
public function getFieldHtml(){
    $var = $this->getVoucher();
    $html = '<b>Voucher:</b>'.$var.'<br/>';
    return $html;
}
}
| improve this answer | |
  • But I dont need a coupon code. I kinda need a custom field within the order filled with an 10 digit code that I need at certain real life events outside if magento when I export the orders. – Marek123 Nov 1 '14 at 11:25
  • I edited the answer. Using the code above you can assign any custom value to an order and use the same in Invoice/Shipment etc. You will have to edit/override the invoice class accordingly to get the values in invoice. – Tarun Nov 1 '14 at 12:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.