当前位置:首页 > PHP

php实现折扣商品

2026-02-16 16:30:03PHP

实现折扣商品功能

在PHP中实现折扣商品功能,通常涉及数据库设计、价格计算逻辑和前端展示。以下是具体实现方法:

数据库设计 创建商品表时需包含折扣相关字段:

CREATE TABLE products (
    id INT PRIMARY KEY AUTO_INCREMENT,
    name VARCHAR(255) NOT NULL,
    original_price DECIMAL(10,2) NOT NULL,
    discount_price DECIMAL(10,2),
    discount_percentage INT,
    is_discounted BOOLEAN DEFAULT 0,
    discount_start DATE,
    discount_end DATE
);

价格计算逻辑 在商品模型中添加折扣计算逻辑:

class Product {
    public function getCurrentPrice() {
        if ($this->isDiscounted() && $this->isDiscountActive()) {
            return $this->discount_price ?? 
                   $this->original_price * (1 - $this->discount_percentage/100);
        }
        return $this->original_price;
    }

    private function isDiscountActive() {
        $now = date('Y-m-d');
        return (!$this->discount_start || $this->discount_start <= $now) &&
               (!$this->discount_end || $this->discount_end >= $now);
    }
}

前端展示 在商品展示页面突出显示折扣信息:

<div class="product">
    <h3><?= htmlspecialchars($product->name) ?></h3>
    <?php if ($product->isDiscounted() && $product->isDiscountActive()): ?>
        <span class="original-price"><?= number_format($product->original_price, 2) ?></span>
        <span class="discount-price"><?= number_format($product->getCurrentPrice(), 2) ?></span>
        <?php if ($product->discount_percentage): ?>
            <span class="discount-badge">-<?= $product->discount_percentage ?>%</span>
        <?php endif; ?>
    <?php else: ?>
        <span class="price"><?= number_format($product->original_price, 2) ?></span>
    <?php endif; ?>
</div>

定时任务处理 使用cron job自动更新折扣状态:

// 在cron job中执行的脚本
$expiredProducts = Product::where('discount_end', '<', date('Y-m-d'))
                         ->where('is_discounted', 1)
                         ->get();
foreach ($expiredProducts as $product) {
    $product->update([
        'is_discounted' => 0,
        'discount_price' => null,
        'discount_percentage' => null
    ]);
}

API接口 为移动应用提供折扣商品接口:

php实现折扣商品

$router->get('/api/discounted-products', function() {
    $products = Product::where('is_discounted', 1)
                      ->where(function($query) {
                          $query->whereNull('discount_start')
                                ->orWhere('discount_start', '<=', date('Y-m-d'));
                      })
                      ->where(function($query) {
                          $query->whereNull('discount_end')
                                ->orWhere('discount_end', '>=', date('Y-m-d'));
                      })
                      ->get();
    return response()->json($products);
});

标签: 折扣商品
分享给朋友:

相关文章

vue实现商品全选

vue实现商品全选

实现全选功能的基本思路 在Vue中实现商品全选功能,通常需要维护一个商品列表数据和一个选中状态数组。通过计算属性判断是否全选,通过方法控制全选或取消全选。 定义数据模型 data() { ret…

vue实现商品展示

vue实现商品展示

使用 Vue 实现商品展示 商品数据准备 商品数据可以存储在 Vue 组件的 data 中,或者通过 API 从后端获取。以下是一个简单的商品数据示例: data() { return {…

vue实现商品卡片

vue实现商品卡片

Vue 实现商品卡片 在 Vue 中实现商品卡片可以通过组件化的方式完成,以下是一个完整的实现方案: 商品卡片组件 <template> <div class="product…

vue实现商品橱窗

vue实现商品橱窗

实现商品橱窗的基本结构 使用Vue.js创建商品橱窗需要先搭建基本结构。商品橱窗通常包含商品列表、商品卡片、筛选和排序功能。在Vue项目中,可以通过组件化方式实现这些功能。 创建商品列表组件,用于展…

vue实现选择商品规格

vue实现选择商品规格

实现商品规格选择功能 在Vue中实现商品规格选择功能通常需要结合组件化开发和数据绑定。以下是一种常见的实现方式: 数据结构设计 商品规格数据通常采用嵌套结构,例如: data() { retu…

vue实现商品规格分类

vue实现商品规格分类

Vue 实现商品规格分类的方法 数据结构设计 商品规格通常采用树形结构或嵌套对象表示。例如,颜色、尺寸等作为一级规格,具体选项作为子规格。数据结构示例如下: specs: [ { nam…