Answer the question
In order to leave comments, you need to log in
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
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question