PHP code example of ttpryg / catalog-engine

1. Go to this page and download the library: Download ttpryg/catalog-engine library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

ttpryg / catalog-engine example snippets


use PDO;
use Ttpryg\CatalogEngine\Repositories\PdoProductRepository;
use Ttpryg\CatalogEngine\Services\ProductService;
use Ttpryg\CatalogEngine\ValueObjects\Price;

// 1. Initialize PDO
$pdo = new PDO("mysql:host=localhost;dbname=my_db", "root", "secret");

// 2. Setup Service
$productRepo = new PdoProductRepository($pdo);
$productService = new ProductService($productRepo);

// 3. Create a Product
$product = $productService->createProduct(
    name: 'Running Shoes Nike Air',
    price: 1500000.0,
    salePrice: 1200000.0,
    sku: 'NIKE-AIR-001',
    stock: 25,
    weightGrams: 450,
    status: 'active'
);

// 4. Calculate Price & Discount
$priceVo = new Price($product->getPrice(), $product->getSalePrice());
echo $priceVo->format(); // Rp 1.200.000
echo $priceVo->getDiscountPercentage(); // 20.0%

// 5. Deduct Stock on Order
$productService->updateStock($product->getId(), -2);