Magento - 修改购物车规则功能以按原始价格百分比而非产品价格折扣应用折扣

时间:2014-12-25 23:48:36

标签: magento shopping-cart

我正在开展一个项目,整个商店的产品价格与原价不同(降低)。

我希望能够按原始价格的百分比应用购物车折扣,就像目录折扣价格规则一样。

目前,如果我使用购物车“产品价格折扣百分比”规则,则会将折扣应用于特价而非原价。

购物车规则的功能在哪里?任何有关修改它以应用原价折扣的细节将不胜感激。

1 个答案:

答案 0 :(得分:0)

我最近不得不解决同样的问题。 These two博客指南非常有助于制定我的最终解决方案。

我在salesrule_validator_process事件上的自定义观察者看起来像这样:

class My_SalesRule_Model_Observer
{
    // New SalesRule type
    const TO_ORIGINAL_PRICE = 'to_original_price';

    /**
     * Add the new SalesRule type to the admin form.
     * @param Varien_Event_Observer $obs
     */
    public function adminhtmlBlockSalesruleActionsPrepareform($obs)
    {
        $field = $obs->getForm()->getElement('simple_action');
        $options = $field->getValues();
        $options[] = array(
            'value' => self::TO_ORIGINAL_PRICE,
            'label' => Mage::helper('salesrule')->__('Percent of original product price discount')
        );
        $field->setValues($options);
    }

    /**
     * Apply the new SalesRule type to eligible cart items.
     * @param Varien_Event_Observer $obs
     */
    public function salesruleValidatorProcess($obs)
    {
        /* @var Mage_SalesRule_Model_Rule $rule */
        $rule = $obs->getRule();
        if ($rule->getSimpleAction() == self::TO_ORIGINAL_PRICE) {
            /* @var Mage_Sales_Model_Quote_Item $item */
            $item = $obs->getItem();
            // Apply rule qty cap if it exists.
            $qty  = $rule->getDiscountQty()? min($obs->getQty(), $rule->getDiscountQty()) : $obs->getQty();
            // Apply rule stepping if specified.
            $step = $rule->getDiscountStep();
            if ($step) {
                $qty = floor($qty / $step) * $step;
            }

            // Rule discount amount (assumes %).
            $ruleDiscountPercent = $rule->getDiscountAmount();
            /* @see My_Catalog_Model_Product::getDiscountPercent */
            $productDiscountPercent = $item->getProduct()->getDiscountPercent();

            // Ensure that the rule does not clobber a larger discount already present on the $cartItem.
            // Do not apply the rule if the discount would be less than the price they were already quoted
            // from the catalog (i.e. special_price or Catalog Price Rules).
            if ($ruleDiscountPercent > $productDiscountPercent) {
                // Reduce $ruleDiscountPercent to just the gap required to reach target pct of original price.
                // In this way we add the coupon discount to the existing catalog discount (if any).
                $ruleDiscountPercent -= $productDiscountPercent;

                $pct = $ruleDiscountPercent / 100;
                // Apply the discount to the product original price, not the current quote item price.
                $discountAmount = ($item->getProduct()->getPrice() * $pct) * $qty;

                $item->setDiscountPercent($ruleDiscountPercent);
                $obs->getResult()
                    ->setDiscountAmount($discountAmount)
                    ->setBaseDiscountAmount($discountAmount);
            }
        }
    }
}

如果您已正确设置扩展程序,则应看到新规则类型显示在:

促销 - >购物车价格规则 - >编辑新规则' - >行动 - >使用以下信息更新价格:应用[原始产品价格折扣百分比]

注意事项:您会注意到我实施此操作以处理百分比,包括在产品模型上创建方法以计算目录折扣价格(即特殊价格和目录级别适用的其他折扣)。如果您希望这样做,或者需要更新此逻辑以适合您的场景,您可能需要实现它,而不是仅仅引用$item->getProduct()->getPrice()