PHP code example of davidiwezulu / ecommerce

1. Go to this page and download the library: Download davidiwezulu/ecommerce 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/ */

    

davidiwezulu / ecommerce example snippets


use Davidiwezulu\Ecommerce\Facades\Cart;

Cart::addOrUpdate($productId, $quantity);

namespace App\Models;

use Davidiwezulu\Ecommerce\Models\Product as BaseProduct;

class Product extends BaseProduct
{
    // Add your customizations here

    /**
     * Example of adding a new relationship.
     */
    public function categories()
    {
        return $this->belongsToMany(Category::class);
    }

    /**
     * Example of adding a custom method.
     */
    public function calculateDiscountedPrice()
    {
        // Implement your discount logic here
    }
}
bash
php artisan vendor:publish --provider="Davidiwezulu\Ecommerce\EcommerceServiceProvider"
bash
php artisan migrate

$items = Cart::items();

foreach ($items as $item) {
    echo 'Product: ' . $item->product->name . PHP_EOL;
    echo 'Price: ' . $item->price . PHP_EOL;
    echo 'Tax Amount: ' . $item->tax_amount . PHP_EOL;
    echo 'Quantity: ' . $item->quantity . PHP_EOL;
    echo 'Total Price: ' . $item->total_price . PHP_EOL; // Uses getTotalPriceAttribute()
    echo '---' . PHP_EOL;
}

 
use Davidiwezulu\Ecommerce\Facades\Cart;

// Add product to cart
Cart::addOrUpdate($productId, $quantity);

// Retrieve cart items with tax details
$items = Cart::items();

foreach ($items as $item) {
    echo 'Product: ' . $item->product->name . PHP_EOL;
    echo 'Price: ' . $item->price . PHP_EOL;
    echo 'Tax Amount: ' . $item->tax_amount . PHP_EOL;
    echo 'Total Price (incl. Tax): ' . $item->total_price . PHP_EOL;
}
 
namespace App\Payments;

use Davidiwezulu\Ecommerce\Payments\PaymentGatewayInterface;

class CustomGateway implements PaymentGatewayInterface
{
    public function charge($amount, $paymentDetails)
    {
        // Implement charge logic
    }

    public function execute($paymentId, $payerId)
    {
        // Implement execute logic (if needed)
    }

    public function refund($transactionId)
    {
        // Implement refund logic
    }
}