在购物车中为单个商品添加折扣

时间:2014-12-10 00:49:17

标签: magento module magento-1.9

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

<?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');
        }
    }
    }
}

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

不幸的是,它没有添加任何折扣......

任何人都可以建议任何可能有用的东西吗?

由于

GAV株系

1 个答案:

答案 0 :(得分:0)

我最终找到了办法:

<?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();
                }
            }
        }
    }
}

以上简单地允许捆绑产品保持动态,同时具有固定价格。如果动态值超过固定价格,它将添加折扣以将其降低回固定价格。

我很快会使用不包含添加到捆绑包中的额外产品的代码对此进行更新。