【问题标题】:Add discount to single product in cart为购物车中的单个产品添加折扣
【发布时间】:2014-12-10 00:49:16
【问题描述】:

在尝试修复捆绑包价格同时保留增值税计算后,我下一次尝试解决此问题是对捆绑包应用折扣以将其减少到固定金额。

<?php

class XXX_Fixedbundlediscount_Model_Observer
{
    public function setDiscount($observer)
    {
    Mage::log('settingDiscount');
    $quote = $observer->getEvent()->getQuote();
    $quoteid = $quote->getId();
    if ($quoteid) {
        foreach ($quote->getAllItems() as $item) {
            $product = $item->getProduct();
            $fixed_price_attribute = (float)Mage::getResourceModel('catalog/product')->getAttributeRawValue($product->getId(), 'giftset_fixed_price', Mage::app()->getStore()->getStoreId());
            if ($product->getTypeId() !== Mage_Catalog_Model_Product_Type::TYPE_BUNDLE || !$fixed_price_attribute) {
                continue;
            }
        $lineTotal = (float)$item->getPriceInclTax();
        $item->setBaseDiscountAmount(0);
        $item->setDiscountAmount(0);
        if ($lineTotal > $fixed_price_attribute) {
            $item->setDiscountAmount(-($lineTotal - $fixed_price_attribute));
            $item->setDiscountDescription('Gift Set');
                    $item->setBaseDiscountAmount(-($lineTotal - $fixed_price_attribute))->save();
            Mage::log('xxxx');
        } 
        Mage::log($fixed_price_attribute);
        Mage::log($lineTotal);
        Mage::log(($fixed_price_attribute > $lineTotal) ? 'Yes' : 'No');
        }
    }
    }
}

我在捆绑产品上设置了一个自定义属性,用于指定固定成本。从上面可以看出,想法是检测到这一点,计算捆绑的成本并将属性值之间的差值作为折扣添加。

不幸的是,它没有像以往那样增加任何折扣......

任何人都可以提出任何可能有用的建议吗?

谢谢

Gav

【问题讨论】:

    标签: magento module magento-1.9


    【解决方案1】:

    我终于找到了办法:

    <?php
    
    class XXX_Fixedbundlediscount_Model_Observer
    {
        public function setDiscount($observer)
        {
            $quote = $observer->getEvent()->getQuote();
            $quoteid = $quote->getId();
            $discountAmount = 0;
            if ($quoteid) {
    
                foreach ($quote->getAllItems() as $item) {
                    $product = $item->getProduct();
                    $fixed_price_attribute = (float)Mage::getResourceModel('catalog/product')->getAttributeRawValue($product->getId(), 'giftset_fixed_price', Mage::app()->getStore()->getStoreId());
                    if ($product->getTypeId() !== Mage_Catalog_Model_Product_Type::TYPE_BUNDLE || !$fixed_price_attribute) {
                        continue;
                    }
                    if ($item->getPriceInclTax() > $fixed_price_attribute) {
                        $discountAmount += $item->getQty() * ($item->getPriceInclTax() - $fixed_price_attribute);
                    }
                }
    
                if ($discountAmount > 0) {
                    $total = $quote->getBaseSubtotal();
                    $quote->setSubtotal(0);
                    $quote->setBaseSubtotal(0);
    
                    $quote->setSubtotalWithDiscount(0);
                    $quote->setBaseSubtotalWithDiscount(0);
    
                    $quote->setGrandTotal(0);
                    $quote->setBaseGrandTotal(0);
    
                    $canAddItems = $quote->isVirtual() ? ('billing') : ('shipping');
                    foreach ($quote->getAllAddresses() as $address) {
    
                        $address->setSubtotal(0);
                        $address->setBaseSubtotal(0);
    
                        $address->setGrandTotal(0);
                        $address->setBaseGrandTotal(0);
    
                        $address->collectTotals();
    
                        $quote->setSubtotal((float)$quote->getSubtotal() + $address->getSubtotal());
                        $quote->setBaseSubtotal((float)$quote->getBaseSubtotal() + $address->getBaseSubtotal());
    
                        $quote->setSubtotalWithDiscount(
                            (float)$quote->getSubtotalWithDiscount() + $address->getSubtotalWithDiscount()
                        );
                        $quote->setBaseSubtotalWithDiscount(
                            (float)$quote->getBaseSubtotalWithDiscount() + $address->getBaseSubtotalWithDiscount()
                        );
    
                        $quote->setGrandTotal((float)$quote->getGrandTotal() + $address->getGrandTotal());
                        $quote->setBaseGrandTotal((float)$quote->getBaseGrandTotal() + $address->getBaseGrandTotal());
    
                        $quote->save();
    
                        $quote->setGrandTotal($quote->getGrandTotal() - ($discountAmount / 2))
                            ->setBaseGrandTotal($quote->getBaseGrandTotal() - $discountAmount)
                            ->setSubtotalWithDiscount($quote->getSubtotalWithDiscount() - $discountAmount)
                            ->setBaseSubtotalWithDiscount($quote->getBaseSubtotalWithDiscount() - $discountAmount)
                            ->save();
    
                        if ($address->getAddressType() == $canAddItems) {
    
                            $address->setSubtotalWithDiscount((float)$address->getSubtotalWithDiscount() - $discountAmount);
                            $address->setGrandTotal((float)$address->getGrandTotal() - $discountAmount);
                            $address->setBaseSubtotalWithDiscount((float)$address->getBaseSubtotalWithDiscount() - $discountAmount);
                            $address->setBaseGrandTotal((float)$address->getBaseGrandTotal() - $discountAmount);
    
                            if ($address->getDiscountDescription()) {
                                $address->setDiscountAmount(-($address->getDiscountAmount() - $discountAmount));
                                $address->setDiscountDescription($address->getDiscountDescription() . ', Gift Sets');
                                $address->setBaseDiscountAmount(-($address->getBaseDiscountAmount() - $discountAmount));
                            } else {
                                $address->setDiscountAmount(-($discountAmount));
                                $address->setDiscountDescription('Gift Sets');
                                $address->setBaseDiscountAmount(-($discountAmount));
                            }
                            $address->save();
                        }
                    }
    
                    foreach ($quote->getAllItems() as $item) {
                        $product = $item->getProduct();
                        $fixed_price_attribute = (float)Mage::getResourceModel('catalog/product')->getAttributeRawValue($product->getId(), 'giftset_fixed_price', Mage::app()->getStore()->getStoreId());
                        if ($product->getTypeId() !== Mage_Catalog_Model_Product_Type::TYPE_BUNDLE || !$fixed_price_attribute) {
                            continue;
                        }
    
                        $lineDiscount = 0;
                        if ($item->getPriceInclTax() > $fixed_price_attribute) {
                            $lineDiscount = $item->getQty() * ($item->getPriceInclTax() - $fixed_price_attribute);
                        }
    
                        $item->setDiscountAmount($lineDiscount);
                        $item->setBaseDiscountAmount($lineDiscount)->save();
                    }
                }
            }
        }
    }
    

    以上内容仅允许捆绑产品在具有固定价格的同时保持动态。如果动态值大于固定价格,则会添加折扣以将其降低回固定价格。

    我将很快用不包括添加到捆绑包中的额外产品的代码来更新此内容。

    【讨论】:

      猜你喜欢
      • 2020-10-24
      • 1970-01-01
      • 1970-01-01
      • 2011-04-24
      • 1970-01-01
      • 2011-12-12
      • 1970-01-01
      • 2017-09-27
      • 2019-02-06
      相关资源
      最近更新 更多