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



我正在做一个项目,其中整个商店产品的特殊价格与原始价格不同(降低)。

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

现在,如果我应用购物车的"产品价格折扣百分比"规则,它将折扣应用于特价而不是原始价格。

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

我最近不得不解决同样的问题。这两个博客指南对我制定最终解决方案非常有帮助。

我在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()

最新更新