以编程方式创建Magento优惠券,有效期为30天

时间:2013-10-14 06:44:32

标签: magento magento-1.7

我需要在Magento的帐户创建行动中创建优惠券。 我已经创建了在帐户Create上正确执行的观察者,但我正在努力创建优惠券。我已根据适用的购物车小计固定了固定金额的优惠券名称。

我找到了很多,但他们都没有关于优惠券到期的任何内容

1 个答案:

答案 0 :(得分:0)

还没有回复:(

虽然我找到了解决方案。

    $customer = $observer->getCustomer()->getData();
    $email = $customer['email'];
    $email100 = $email.'-100-1';
    $datei=date('d/m/Y');
    $datee = date('d/m/Y', strtotime('1 day'));
    $firstname=$customer['firstname'];
    $couponcode1=$email.'100-1';

    $data = array(
        'product_ids' => null,
        'name' => sprintf('coupon-100-1', Mage::getSingleton('customer/session')->getCustomerId()),
        'description' => null,
        'is_active' => 1,
        'website_ids' => array(1),
        'customer_group_ids' => array(1),
        'coupon_type' => 2,
        'coupon_code' => $email100,
        'uses_per_coupon' => 1,
        'uses_per_customer' => 1,
        'from_date' => $datei,
        'to_date' => $datee,
        'sort_order' => null,
        'is_rss' => 1,
        'test' => 'test',
        'rule' => array(
            'conditions' => array(
                            '1' =>array(
                                'type' => 'salesrule/rule_condition_combine',
                                'aggregator' => 'all',
                                'value' => 1,
                            ),

                            '1--1' => array(
                                'type' => 'salesrule/rule_condition_combine',
                                'aggregator' => 'all',
                                'value' => 1,
                            ),

                            '1--1--1' =>array(
                                'type' => 'salesrule/rule_condition_address',
                                'attribute' => 'base_subtotal',
                                'operator' => '>=',
                                'value' => '400'
                            ),

                            '1--1--2' =>array(
                                'type' => 'salesrule/rule_condition_combine',
                                'aggregator' => 'all',
                                'value' => 0
                            ),

                            '1--1--2--1' =>array(
                                'type' => 'salesrule/rule_condition_product_found',
                                'value' => 1,
                                'aggregator' => 'all'
                            ),

                            '1--1--2--1--1' => array(
                                'type' => 'salesrule/rule_condition_product',
                                'attribute' => 'special_price',
                                'operator' => '>=',
                                'value' => 0
                            )
                        ),
            'actions' => array(
                        '1' =>array(
                            'type' => 'salesrule/rule_condition_combine',
                            'aggregator' => 'all',
                            'value' => 1,
                            'new_child' => null
                        ),  
                    )
                ),
        'simple_action' => 'cart_fixed',
        'discount_amount' => 100,
        'discount_qty' => 0,
        'discount_step' => null,
        'apply_to_shipping' => 0,
        'simple_free_shipping' => 0,
        'stop_rules_processing' => 0,
        'store_labels' => array()
    );
    $model = Mage::getModel('salesrule/rule');
    $validateResult = $model->validateData(new Varien_Object($data));
    if ($validateResult !== true) {
            foreach($validateResult as $errorMessage) {
                // print_r($errorMessage);
                $session->addError($errorMessage);
            }
        }
        if (isset($data['rule']['conditions'])) {
            $data['conditions'] = $data['rule']['conditions'];
        }
        if (isset($data['rule']['actions'])) {
            $data['actions'] = $data['rule']['actions'];
        }
        unset($data['rule']);
        //print_r($data);            

        $model->loadPost($data);
        $model->save();

此处优惠券代码的名称是电子邮件ID,后跟-100-1。 $ datei和$ datee是初始日期和到期日期。我已经自定义了优惠券代码,以便在购物车包含特价商品时不使用优惠券代码。 固定金额折扣适用于完整购物车小计。

如果是查询,请回复。

相关问题