PHP code example of shopper / pricelist

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

    

shopper / pricelist example snippets


use Shopper\Facades\Shopper;
use Shopper\PriceList\PriceListAddon;

public function register(): void
{
    Shopper::addons([
        new PriceListAddon,
    ]);
}

Shopper::addons([
    new CustomerGroupsAddon,
    new PriceListAddon,
]);

namespace App\Pricing;

use App\Models\User;
use Shopper\Core\Pricing\PricingContext;
use Shopper\PriceList\Contracts\ApplicabilityRule;

final class B2BCompanyRule implements ApplicabilityRule
{
    /** @var array<int, int|null> */
    private array $companies = [];

    public function key(): string
    {
        return 'b2b_company';
    }

    public function label(): string
    {
        return __('Companies');
    }

    public function passes(array $values, PricingContext $context): bool
    {
        if ($context->customerId === null) {
            return false;
        }

        $company = $this->companies[$context->customerId] ??= User::query()
            ->whereKey($context->customerId)
            ->value('company_id');

        return in_array($company, array_map(intval(...), $values), true);
    }
}

Shopper::addons([
    (new PriceListAddon)->rules([B2BCompanyRule::class]),
]);

  $this->app->scoped(B2BCompanyRule::class);
  

[
    'public' => ['price_list' => ['id' => '01JC4M6Q2T8XK3V5N7R9W1Y0ZB', 'type' => 'sale']],
    'price_list' => ['id' => '01JC4M6Q2T8XK3V5N7R9W1Y0ZB', 'name' => 'Summer sale', 'type' => 'sale'],
    'source' => 'fixed',
    'level' => 'variant',
    'tier' => [10, null],
]

use Shopper\Cart\CartManager;

app(CartManager::class)->setLinePrice($cart, $line->id, 4500);
app(CartManager::class)->clearLinePrice($cart, $line->id);

[
    'priceable_type' => 'variant',
    'priceable_id' => 42,
    'currency' => 'USD',
    'before' => [['min_quantity' => 1, 'max_quantity' => null, 'amount' => 1100, 'compare_amount' => null]],
    'after' => [['min_quantity' => 1, 'max_quantity' => null, 'amount' => 950, 'compare_amount' => null]],
]

use Illuminate\Support\Facades\Event;
use Shopper\PriceList\Events\PriceListPricesSaved;

Event::listen(function (PriceListPricesSaved $event): void {
    foreach ($event->changes as $change) {
        logger()->info('List prices changed', [
            'price_list' => $event->priceList->public_id,
            'actor' => $event->actorId,
            ...$change,
        ]);
    }
});

Shopper::addons([
    (new PriceListAddon)->usingLivewireComponents([
        'pricelist.index' => App\Livewire\CustomPriceListIndex::class,
    ]),
]);
bash
php artisan shopper:pricelist:rebuild-index
bash
php artisan model:prune --model="Shopper\PriceList\Models\PriceHistory"