当前位置:首页 > PHP

php实现折扣商品

2026-02-16 16:30:03PHP

实现折扣商品功能

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

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

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);
    }
}

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

php实现折扣商品

<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接口 为移动应用提供折扣商品接口:

$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中实现商品排序功能可以通过计算属性或方法对数据进行处理。以下是一个完整的实现方案,包含价格、销量和综合排序三种常见方式。 数据准备 假设商品数据存储在goodsList数…

Vue实现商品详情

Vue实现商品详情

Vue实现商品详情页的方法 数据获取与渲染 使用Vue的data属性存储商品详情数据,通过v-for指令循环渲染商品属性列表。结合v-if或v-show控制图片预览、库存状态等动态显示。 data(…

vue实现商品展示

vue实现商品展示

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

vue实现商品卡片

vue实现商品卡片

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

vue实现商品分页

vue实现商品分页

Vue 实现商品分页的方法 基础分页实现 安装必要的依赖(如axios用于请求数据) npm install axios 创建分页组件模板 <template> <div…

vue element 实现商品sku

vue element 实现商品sku

商品SKU实现方法 在Vue和Element UI中实现商品SKU功能,可以通过以下步骤完成: 数据结构设计 商品SKU通常需要处理多个属性组合,每个属性有不同的选项。数据结构可以设计为: d…