【问题标题】:Discounts the whole products belongs to category?折扣全品属于哪个品类?
【发布时间】:2020-02-12 04:31:14
【问题描述】:

我有这些表:

products
-- name
-- price
-- quantity
-- category_id

discounts
-- type('percentage','numeric')
-- value
-- expired_at

categories 
-- name

discountables
-- discount_id
-- discountable_id
-- discountable_type

discountables 之间存在多对多关系:

discounts and categories discounts and products

我已经完成了如何在discounts and products 之间打折

现在我很困惑如何对属于我添加到discountables的类别的整个产品进行折扣

关系:

类别模型

public function discounts()
{
    return $this->morphToMany('App\Models\Discount', 'discountable');
}

产品型号

public function discounts()
{
    return $this->morphToMany('App\Models\Discount', 'discountable');
}

折扣模式:

public function categories()
{
    return $this->morphedByMany('App\Models\Category', 'discountable')->withTimestamps();
}

public function products()
{
    return $this->morphedByMany('App\Models\Product', 'discountable')->withTimestamps();
}

直接打折商品的MY代码discounts and products

  /**
 * get price of product after discount
 *
 * @return void
 */
public function getDiscountProductAttribute() {
    foreach ($this->discounts as $discount) {
        if($discount->expired_at > Carbon::now()){
            if ($discount->type == 'numeric'){
                return $this->price - $discount->value;
            }else{
                return $this->price - ($this->price * ($discount->value / 100));
            }
        }
    }
}

所以我需要如何打折属于我添加到discountables 的类别的整个产品?

【问题讨论】:

  • 如果产品有多个类别,并且这些类别中至少有 (2) 个类别有折扣怎么办?在这种情况下,您如何确定分层折扣应用模式?
  • @Ohgodwhy NO 我的电子商务是每个产品只有一个类别
  • 如果产品和类别都有折扣?
  • @Ohgodwhy 这正是我的问题!我处理的产品打折,一切都很好,现在我需要一件我不知道如何处理的东西。对除已打折的产品以外的所有产品打折!
  • @Ohgodwhy +随意

标签: php laravel


【解决方案1】:

你们的关系很好。我只是重新设计了一下来解决这个问题。

class Discount extends Model
{
    /**
    * Check whether this discount is expired.
    *
    * return bool
    */
    public function expired()
    {
        // I assume that expired_at is also a Carbon instance.
        return $this->expired_at->isPast();
    }

    /**
    * Return the discount amount for each product.
    *
    * @return double
    */
    public function apply(Product $product)
    {
        if ($this->type === 'numeric') {
            $this->value;
        }

        if ($this->type === 'percentage') {
            return $product->price * ($this->value / 100);
        }

        return 0;
    }
}
class Product extends Model
{
    public function allDiscounts()
    {
        return $this->discounts
            ->merge($this->category->discounts)
            ->unique();
    }

    pubic function getTotalDiscountAttribute()
    {
        return $this->allDiscounts()
            ->reject
            ->expired()
            ->map
            ->apply($this)
            ->sum();
    }

    public function getTotalPriceAttribute()
    {
         return $this->price - $this->total_discount;
    }
}

因此,您可以通过简单地说:

$product->total_price;

我希望这会有所帮助。让我知道它是否在任何步骤中失败。

顺便说一句。你的问题让我想起了几年前的pretty good speech。它是关于使用继承和空对象模式解决一些类似的问题。它太酷了。你不需要那样重写它,但它是一个非常好的设计。

【讨论】:

  • 我能得到一些分钟吗?并继续实时聊天?
  • 我收到此错误:尝试获取非对象的属性“拒绝”
  • 我忘记在 allDiscounts() 函数中返回。我已经更新了我的答案。请再试一次。
  • 工作了,但是没有折扣就退了!
  • 请您检查我的答案,留下一点来解决问题!
猜你喜欢
  • 2017-09-27
  • 2015-01-12
  • 2017-04-13
  • 2021-09-03
  • 1970-01-01
  • 2020-08-30
  • 2021-06-10
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多