Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to use discount? #33

Open
mohammad1381 opened this issue Feb 16, 2017 · 2 comments
Open

How to use discount? #33

mohammad1381 opened this issue Feb 16, 2017 · 2 comments

Comments

@mohammad1381
Copy link

make an example for using discount please.

@alexantr
Copy link

alexantr commented Jun 8, 2017

Example with personal discount for logged users:

  1. create behavior:
<?php

namespace app\behaviors;

use Yii;
use yz\shoppingcart\DiscountBehavior;

class PersonalDiscount extends DiscountBehavior
{
    /**
     * @param \yz\shoppingcart\CostCalculationEvent $event
     */
    public function onCostCalculation($event)
    {
        // saved in percentages
        $discount = Yii::$app->user->identity ? Yii::$app->user->identity->discount : 0;
        $event->discountValue = $event->baseCost * ($discount / 100);
    }
}
  1. attach behavior to cart:
Yii::$app->cart->attachBehavior('personalDiscount', ['class' => 'app\behaviors\PersonalDiscount']);
  1. result:
<p>Total cost: <?= Yii::$app->cart->getCost() ?></p>
<p>Total cost with discount: <?= Yii::$app->cart->getCost(true) ?></p>

@alexantr
Copy link

alexantr commented Jun 8, 2017

Instead step 1 & 2 can use this variant:

Yii::$app->cart->on(ShoppingCart::EVENT_COST_CALCULATION, function ($event) {
    $discount = Yii::$app->user->identity ? Yii::$app->user->identity->discount : 0;
    $event->discountValue = $event->baseCost * ($discount / 100);
});

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants