5

I want to have a web shop where products have tier pricing, such as:

  • order 3 save 10%
  • order 10 save 20%

This is easy by setting tier pricing in products.

However, I don't want tier pricing to apply when coupon codes are applied (discount would get too high). So I want a coupon code discount to be based on the base price (1 pcs price). Is there a way to do this?

Example: coupon code ABC123 gives 30% off. So price of items should then be 30% off max at any quantity.

Thanks!

2 Answers 2

4

There is no way to do this in standard Magento.


First off, coupons can be added after the product is added to cart (with the discount from tier pricing already in place). You will have to revert it to the original price. This will cause an issue: If the coupon discount is lower, I'll assume your customer wants to stick with tier price. Or do you want to let the customer choose?

Second, if ever the customer cancels the coupon, it should revert back to its tier price.

And what about when the coupon is added first? Should it display the tier pricing?


Overall it is a very sticky situation. I think I missed more scenarios, I'm just giving you an overview on what to think about if you really want to push it through. And it wouldn't be easy to do (at least for me)

2
  • Thanks for your answer! The desired behavior would be that buyer gets coupon OR tier discount automatically, whichever is better. Not both simultaneously. I haven't found a solution for this so far.
    – Vektori
    Commented Aug 11, 2015 at 12:32
  • @vektori can you please provide solution which worked for you? Commented Mar 2, 2016 at 5:30
0

To achieve this functionality, you can create an observer for the salesrule_validator_process event, which is triggered before applying a coupon code.

Here's how you can implement it: Create an observer class that listens to the salesrule_validator_process event.

Here's the implementation:

Create the observer declaration:

app/code/Vendor/TierpriceDisable/etc/events.xml

<?xml version="1.0" ?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
    <event name="salesrule_validator_process">
        <observer name="vendor_tierpricedisable_observer_salesrule_validatorprocess_salesrule_validator_process" instance="Vendor\TierpriceDisable\Observer\Salesrule\ValidatorProcess"/>
    </event>
</config>

Create the observer class:

app/code/Vendor/TierpriceDisable/Observer/Salesrule/ValidatorProcess.php

<?php

namespace Vendor\TierpriceDisable\Observer\Salesrule;

use Magento\Framework\Event\ObserverInterface;
use Magento\Framework\Event\Observer;
use Magento\Catalog\Model\ProductRepository;
use Psr\Log\LoggerInterface;

class ValidatorProcess implements ObserverInterface
{
    protected $productRepository;
    protected $logger;

    public function __construct(
        ProductRepository $productRepository,
        LoggerInterface $logger 
    ) {
        $this->productRepository = $productRepository;
        $this->logger = $logger;
    }

    public function execute(Observer $observer)
    {
        $item = $observer->getEvent()->getItem();
        $quote = $item->getQuote();
        $couponCode = $quote->getCouponCode();
        if ($couponCode) {
            foreach ($quote->getAllVisibleItems() as $quoteItem) {
                $product = $quoteItem->getProduct();
                $productId = $product->getId();
                try {
                    $tierPrices = $product->getTierPrices();
                    if (!empty($tierPrices)) {
                        $quoteItem->setCustomPrice($product->getFinalPrice());
                        $quoteItem->setOriginalCustomPrice($product->getFinalPrice());
                        $quoteItem->setMessage(__('Tier pricing has been disabled because a coupon has been applied..'));
                    }
                } catch (\Exception $e) {
                    $this->logger->critical($e->getMessage());
                }
            }
        }
        else{
            foreach ($quote->getAllVisibleItems() as $quoteItem) {
                $product = $quoteItem->getProduct();
                $productId = $product->getId();
               
                try {
                    $tierPrices = $product->getTierPrices();
                    if (!empty($tierPrices)) {
                        $tierPrice = $tierPrices[0]; 
                        $quoteItem->setCustomPrice($tierPrice['price']);
                        $quoteItem->setOriginalCustomPrice($tierPrice['price']);
                        $quoteItem->setMessage(__('Tier pricing has been applied.'));
                    }
                } catch (\Exception $e) {
                    $this->logger->critical($e->getMessage());
                }
            }
        }
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.