当前位置:首页 > 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中实现添加商品功能,通常需要使用v-model进行表单数据双向绑定。创建一个包含商品名称、价格、库存等字段的表单结构,通过data()初始化商品对象: <templ…

vue实现商品卡片列表

vue实现商品卡片列表

Vue实现商品卡片列表 使用Vue实现商品卡片列表需要结合组件化开发思想,通过数据驱动视图的方式动态渲染商品信息。以下是具体实现方法: 数据准备 在Vue组件中定义商品数据数组,每个商品对象包含必要…

vue实现选择商品规格

vue实现选择商品规格

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

js商品规格实现

js商品规格实现

商品规格实现方案 在JavaScript中实现商品规格功能通常涉及前端交互逻辑和后端数据结构的配合。以下是几种常见的实现方式: 基于对象或数组的规格组合 使用对象或数组存储规格信息,并通过遍历生成可…

uniapp商品列表

uniapp商品列表

商品列表的实现方法 在UniApp中实现商品列表通常需要结合前端页面布局、数据请求和渲染逻辑。以下是具体实现步骤: 页面布局设计 使用<scroll-view>组件实现可滚动列表,搭配&…

vue实现商品布局

vue实现商品布局

Vue实现商品布局的方法 使用Grid布局 通过CSS Grid可以快速实现商品网格布局。在Vue组件中,结合v-for循环渲染商品列表,利用Grid控制行列间距。 <template>…