İçeriğe geç
Tüm kodlar

ER3NSHOP

Kurulum sihirbazlı online mağaza sistemi

90 satır · 3,1 KBHamİndir
<?php

// Ürünün varyantlarını id => varyant şeklinde döner.
function product_variants($product_id)
{
    $rows = db_all('SELECT * FROM product_variants WHERE product_id = ? ORDER BY sort, id', [$product_id]);
    return array_column($rows, null, 'id');
}

function product_variant_label($item)
{
    return ($item['variant_name'] ? $item['variant_name'] . ': ' : '') . $item['variant'];
}

function product_gallery($product)
{
    $images = array_column(db_all('SELECT file FROM product_images WHERE product_id = ? ORDER BY sort, id', [$product['id']]), 'file');
    if ($product['image']) {
        array_unshift($images, $product['image']);
    }
    return array_values(array_unique($images));
}

function product_price_range($product, $variants)
{
    $prices = array_filter(array_column($variants, 'price'), fn($price) => $price !== null);
    if (!$prices) {
        return null;
    }
    $prices[] = $product['price'];
    return [min($prices), max($prices)];
}

// Formdan gelen varyant satırlarını kaydeder, ürünün toplam stoğunu günceller.
function product_save_variants($product_id, $rows)
{
    $keep = [];
    $sort = 0;
    foreach ($rows as $row) {
        $name = trim($row['name'] ?? '');
        if ($name === '') {
            continue;
        }
        $data = [
            'product_id' => $product_id,
            'name'       => $name,
            'sku'        => trim($row['sku'] ?? '') ?: null,
            'price'      => to_decimal($row['price'] ?? ''),
            'stock'      => max(0, (int) ($row['stock'] ?? 0)),
            'sort'       => $sort++,
        ];
        $id = (int) ($row['id'] ?? 0);
        if ($id && db_value('SELECT COUNT(*) FROM product_variants WHERE id = ? AND product_id = ?', [$id, $product_id])) {
            db_update('product_variants', $data, $id);
        } else {
            $id = db_insert('product_variants', $data);
        }
        $keep[] = $id;
    }

    $sql = 'DELETE FROM product_variants WHERE product_id = ?';
    $params = [$product_id];
    if ($keep) {
        $sql .= ' AND id NOT IN (' . db_in($keep) . ')';
        $params = array_merge($params, $keep);
    }
    db_query($sql, $params);

    if ($keep) {
        db_query('UPDATE products SET stock = (SELECT COALESCE(SUM(stock), 0) FROM product_variants WHERE product_id = ?) WHERE id = ?', [$product_id, $product_id]);
    }
}

function product_delete($product)
{
    foreach (db_all('SELECT file FROM product_images WHERE product_id = ?', [$product['id']]) as $image) {
        upload_delete($image['file']);
    }
    upload_delete($product['image']);
    db_query('DELETE FROM product_images WHERE product_id = ?', [$product['id']]);
    db_query('DELETE FROM product_variants WHERE product_id = ?', [$product['id']]);
    db_query('DELETE FROM reviews WHERE product_id = ?', [$product['id']]);
    db_query('DELETE FROM favorites WHERE product_id = ?', [$product['id']]);
    db_delete('products', $product['id']);
}

function bestsellers($limit = 8)
{
    return db_all('SELECT * FROM products WHERE status = 1 AND sold > 0 ORDER BY sold DESC LIMIT ' . (int) $limit);
}

Lisans bilgisi projenin LICENSE dosyasında.