我一直在一个项目,该项目需要客户根据那里的物品获得应用于他们的购物篮的自动折扣代码。

我认为这与下面的代码逻辑完美。我看到代码生成(在后端,并在购物车上显示折扣)。但是今天,它已经提出了客户信贷不能完全用于支付订单。

我已成功将其缩小到该模块,当禁用时,问题不存在。然而,我相信,因为我们在飞行中生成代码并让Magento处理实际折扣应该正常工作。调试表明,在检查免费结账时,宏伟的总数仍然是原始价格。

是否有一些额外的逻辑,我需要输入它以兼容它?

请注意,由于客户机密性原因,我有屏蔽的命名空间/模块名称稍微略微略有下面,所以您可能会发现以下一些错误,但请忽略代码确实执行并运行我的期望。

<强>期望:

如果客户符合折扣标准,则生成折扣代码,他们对购物车获得折扣。

如果客户有足够的商店信用以完全支付,他们应该能够用商店信用支付。

实际

生成折扣代码(适用时)

客户无法使用商店信用以满足他们的购买。

观察者:checkout_cart_product_add_after

public function checkout_cart_product_add_after($observer)
{
    if (!Mage::helper('core')->isModuleOutputEnabled('Namespace_WholesaleDiscount')) {
        return;
    }

    /*
     * When emptying the cart this observer is updated
     * due to the setting of it to 0 for deletion.
     * This prevents running the observer and thus resetting items in quote.
     */
    $request = Mage::app()->getRequest()->getParam('update_cart_action');
    if ($request == 'empty_cart') {
        return;
    }



    try {
        $model = Mage::getModel('salesrule/rule')
            ->getCollection()
            ->addFieldToSelect('*')
            ->addFieldToFilter('name', array('eq' => sprintf('WHOLESALE AUTO_%s', Mage::getSingleton('customer/session')->getSessionId())))
            ->load();

        if ($model->count() < 1) {
            // No coupon yet.
            $helper = Mage::helper('wholesalediscount');
            $helper->createDiscountCode($helper->getDiscountAmount(), Mage::getSingleton('customer/session')->getSessionId());

            $model = Mage::getModel('salesrule/rule')
                ->getCollection()
                ->addFieldToFilter('name', array('eq' => sprintf('WHOLESALE AUTO_%s', Mage::getSingleton('customer/session')->getSessionId())))
                ->getFirstItem();

        } else {
            $helper = Mage::helper('wholesalediscount');

            $model = $model->getFirstItem();


            $model->setDiscountAmount($helper->getDiscountAmount());
            $code = Mage::helper('core')->getRandomString(16);

            // Magento initializes the coupon_code, but on model load it is code.
            $model->setCouponCode($code);
            $model->setCode($code);

            $model->save();
        }

        $couponCode = $model->getCode();
        /* END This part is my extra, just to load our coupon for this specific customer */

        Mage::getSingleton('checkout/cart')
            ->getQuote()
            ->getShippingAddress()
            ->setCollectShippingRates(true);

        Mage::getSingleton('checkout/cart')
            ->getQuote()
            ->setCouponCode(strlen($couponCode) ? $couponCode : '')
            ->collectTotals()
            ->save();

    } catch (Exception $e) {
        Mage::logException($e);
    }

    return $this;
}
.

帮助逻辑: 再次如上所述,我掩盖了任何我认为可能揭示业务逻辑或违反任何客户机密性的事情。 如前所述,这里的两种方法只是生成折扣代码优惠券(我可以在管理员中看到工作并确实应用折扣)并计算折扣(这完全是业务逻辑)

public function createDiscountCode($discount, $reference)
    {
        $now = new Zend_Date();
        $now->setTimezone('UTC');
        $data = array(
            'product_ids' => null,
            'name' => sprintf('WHOLESALE AUTO_%s', $reference),
            'description' => null,
            'is_active' => 1,
            'website_ids' => array(Mage::app()->getWebsite()->getId()),
            'customer_group_ids' => array(Mage::getSingleton('customer/session')->getCustomerGroupId()),
            'coupon_type' => 2,
            'coupon_code' => Mage::helper('core')->getRandomString(16),
            'uses_per_coupon' => 1,
            'uses_per_customer' => 1,
            'from_date' => $now->getFullYear() . "-". $now->getMonth() . "-" . $now->getDay(),
            'to_date' => null,
            'sort_order' => null,
            'is_rss' => 1,
            'rule' => array(
                'conditions' => array(
                    array(
                        'type' => 'salesrule/rule_condition_combine',
                        'aggregator' => 'all',
                        'value' => 1,
                        'new_child' => null
                    )
                )
            ),
            'simple_action' => 'cart_fixed',
            'discount_amount' => $this->getDiscountAmount(),
            'discount_qty' => 0,
            'discount_step' => null,
            'apply_to_shipping' => 0,
            'simple_free_shipping' => 0,
            'stop_rules_processing' => 0,
            'rule' => array(
                'actions' => array(
                    array(
                        'type' => 'salesrule/rule_condition_product_combine',
                        'aggregator' => 'all',
                        'value' => 1,
                        'new_child' => null
                    )
                )
            ),
            'store_labels' => array('E-Liquids With Kits')
        );

        $model = Mage::getModel('salesrule/rule');

        $validateResult = $model->validateData(new Varien_Object($data));

        if ($validateResult == true) {

            if (isset($data['simple_action']) && $data['simple_action'] == 'by_percent'
                && isset($data['discount_amount'])) {
                $data['discount_amount'] = min(100, $data['discount_amount']);
            }

            if (isset($data['rule']['conditions'])) {
                $data['conditions'] = $data['rule']['conditions'];
            }

            if (isset($data['rule']['actions'])) {
                $data['actions'] = $data['rule']['actions'];
            }

            unset($data['rule']);

            $model->setData($data);

            $model->save();
        }
    }

    /*
     * Use this to calculate how much money off is allowed based on whats in the cart.
     */
    public function getDiscountAmount()
    {
        $customer   = Mage::getSingleton('customer/session')->getCustomer();
        $cart       = Mage::getModel('checkout/cart');
        $discountItems = array();
        /* Exclude some Business logic here */
        foreach ($cart->getQuote()->getAllVisibleItems() as $_item) {
            $product = Mage::getModel('catalog/product')->load($_item->getProduct()->getId());

            /* Business Logic to decide if this item is relevent */
            $discountItems[] = $_item;

        }

        // Calculate Discount
        $appliedDiscounts = 0;
        $discount = 0;
        foreach ($discountitem as $_item) {
            if ($appliedDiscounts < $allowed) {
                $i = 0;
                while ($appliedDiscounts < $allowed && $i < $_item->getQty()) {

                    $appliedDiscounts++;
                    $discount += $_item->getProduct()->getFinalPrice();

                    $i++;

                }
            }
        }
        return $discount;
    }
.

有帮助吗?

解决方案

所以如果有人在将来遇到类似的问题,我已经设法解决了我问题的原因。收集总计设置了一个标志,导致它在折扣被折扣后不计算在折扣之后。

Mage::getSingleton('checkout/cart')
        ->getQuote()
        ->setCouponCode(strlen($couponCode) ? $couponCode : '')
        ->collectTotals()
        ->save();
.

用:

替换上面的内容
Mage::getSingleton('checkout/cart')
            ->getQuote()
            ->setCouponCode(strlen($couponCode) ? $couponCode : '')
            ->save();
.

这意味着在尝试应用客户信用时,它会计算总计,并有资格获得零支付结账。

许可以下: CC-BY-SA归因
scroll top