PHP code example of albertvds / laravel-wp-sync

1. Go to this page and download the library: Download albertvds/laravel-wp-sync 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/ */

    

albertvds / laravel-wp-sync example snippets


use Albertvds\WpSync\WpFacade as Wp;

// Fetch published posts
Wp::posts()->where('status', 'publish')->get();

// Fetch posts in a category, paginated
Wp::posts()->where('status', 'publish')->category('news')->paginate(10);

// Fetch a single post by slug
Wp::posts()->slug('my-post-slug')->first();

// Search posts
Wp::posts()->search('laravel')->get();

// Fetch pages
Wp::pages()->where('status', 'publish')->get();

// Fetch categories
Wp::categories()->get();

// Fetch media
Wp::media()->get();

use Albertvds\WpSync\WooFacade as Woo;

// Fetch all processing orders
Woo::orders()->where('status', 'processing')->get();

// Fetch orders placed after a date, paginated
Woo::orders()->where('status', 'processing')->after('2024-01-01')->paginate(20);

// Fetch a single order by ID
Woo::orders()->find(42);

// Fetch in-stock products
Woo::products()->inStock()->get();

// Search products
Woo::products()->search('t-shirt')->paginate(15);

// Fetch customers
Woo::customers()->get();

// Fetch coupons
Woo::coupons()->get();

// Fetch product categories
Woo::categories()->get();

use Albertvds\WpSync\Events\OrderCreated;
use Albertvds\WpSync\Events\OrderUpdated;
use Albertvds\WpSync\Events\ProductUpdated;
use Albertvds\WpSync\Events\CustomerCreated;

// In EventServiceProvider or using #[AsEventListener]
Event::listen(OrderCreated::class, function (OrderCreated $event) {
    // $event->order is a typed WooOrder DTO
    Mail::to($event->order->billingEmail)
         ->send(new OrderConfirmationMail($event->order));
});

Event::listen(ProductUpdated::class, function (ProductUpdated $event) {
    // $event->product is a typed WooProduct DTO
    Cache::forget('product-'.$event->product->id);
});

use Albertvds\WpSync\Events\WooWebhookReceived;

Event::listen(WooWebhookReceived::class, function (WooWebhookReceived $event) {
    Log::info('Webhook received', [
        'topic'   => $event->topic,
        'payload' => $event->payload,
    ]);
});

Schedule::command('woo:sync products')->hourly();
Schedule::command('wp:sync posts')->daily();

'connections' => [
    'store-nl' => [
        'url'    => env('WOO_NL_URL'),
        'key'    => env('WOO_NL_KEY'),
        'secret' => env('WOO_NL_SECRET'),
    ],
    'store-de' => [
        'url'    => env('WOO_DE_URL'),
        'key'    => env('WOO_DE_KEY'),
        'secret' => env('WOO_DE_SECRET'),
    ],
],

Woo::connection('store-de')->orders()->where('status', 'processing')->get();

Wp::connection('blog-nl')->posts()->where('status', 'publish')->paginate(10);

'cache_ttl' => [
    'posts'    => 300,
    'products' => 60,
    'orders'   => 0,   // 0 disables caching for this resource
],

return [
    'wp' => [
        'url'          => env('WP_URL'),
        'user'         => env('WP_USER'),
        'app_password' => env('WP_APP_PASSWORD'),
    ],

    'woo' => [
        'url'    => env('WOO_URL'),
        'key'    => env('WOO_KEY'),
        'secret' => env('WOO_SECRET'),
    ],

    'webhook_path' => env('WOO_WEBHOOK_PATH', 'woo/webhook'),

    'cache_ttl' => env('WP_SYNC_CACHE_TTL', 300),

    'connections' => [],
];
bash
php artisan vendor:publish --tag=wp-sync-config