Implement different types of discounts to Products - PHP

1

how are you? I'm doing a PHP shop with CodeIgniter and I have a problem with the following: I want my products to receive 2 types of discounts: a discount% of the total price or a fixed amount. To achieve this, I thought of something like this:

abstract class Descuento
{
    /**
     * @var float valor de descuento
     */
    protected $descuento;

    public function __construct($descuento)
    {
        parent::__construct();
        $this->descuento = $descuento;
    }

    public function getDescuento()
    {
        return $this->descuento;
    }

    abstract function aplicarA(Item $item);
}

class DescuentoImporte extends Descuento
{
    function aplicarA(Item $item)
    {
        return $item->getPrecioLista() - $this->descuento;
    }
}

class DescuentoPorcentaje extends Descuento_Model
{
    function aplicarA(Item $item)
    {
        $precioLista = $item->getPrecioLista();
        return $this->descuento > 0? $precioLista - $precioLista * $this->descuento : $precioLista;
    }
}

Next, my Product class would have an attribute of the Discount type where I would store some of these discounts if they had them or could be injected using the setter. The problem I find with this idea is how to persist the discounts so that when you bring them from the database you know if the discount is% or fixed.

Probably I am complicating it, can you think of a simpler or more practical way to implement this requirement? Thank you very much

    
asked by Federico Antonucci 09.08.2016 в 20:39
source

0 answers