Q
Q
Qixing2015-02-05 03:45:31
symfony
Qixing, 2015-02-05 03:45:31

How to override the value of an entity property using a service in Symfony2?

Good afternoon.
I need to display a different final price for a product depending on the conditions (let's say user groups).
If I already have it written everywhere in the templates. {{ item.price }}
How can I implement a service that intercepts $item->getPrice and returns the desired value.
Links to examples are
welcome

<?php
namespace Bundle\Service;

class Price
{
    public function __construct($ratio , $minCommission) 
    {
        $this->ratio = $ratio;
        $this->minCommission = $minCommission; 
    }

    public function actualPrice($price)
    {
        return $price > 20000 ? $price * $this->ratio : $price + $this->minCommission;
    }
}

crbr.price:
        class: CrbrBundle\Service\Price
        arguments:
            - %price_ratio%
            - %price_minCommission

parameters:
     price_ratio : 1.1
     price_minCommission: 1500

Answer the question

In order to leave comments, you need to log in

1 answer(s)
Y
Yuri, 2015-02-05
@Qixing

In your case, I think you can do otherwise - implement a twig filter, and in the template itself already do this {{ item.price| actualPrice }} .

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question