Hey there, fellow developer! Ready to dive into the world of Ecwid API integration? You're in for a treat. We'll be using the awesome dspacelabs/ecwid-client package to make our lives easier. Ecwid's API is a powerhouse, letting you tap into store data, manage products, and handle orders like a boss. Let's get cracking!
Before we jump in, make sure you've got:
Got all that? Great! Let's move on.
First things first, let's get that dspacelabs/ecwid-client package installed. Fire up your terminal and run:
composer require dspacelabs/ecwid-client
Easy peasy, right?
Now, let's initialize our Ecwid client. It's as simple as:
use Dspacelabs\Ecwid\Client; $client = new Client('YOUR_STORE_ID', 'YOUR_ACCESS_TOKEN');
Replace those placeholders with your actual credentials, and you're good to go!
Let's get our hands dirty with some basic operations:
$storeInfo = $client->getStoreProfile(); echo "Welcome to " . $storeInfo['name'];
$products = $client->getProducts(); foreach ($products as $product) { echo $product['name'] . " - $" . $product['price']; }
$orders = $client->getOrders(); foreach ($orders as $order) { echo "Order #" . $order['orderNumber'] . " - $" . $order['total']; }
The Ecwid API uses pagination for large datasets. Here's how to handle it:
$offset = 0; $limit = 100; do { $products = $client->getProducts(['offset' => $offset, 'limit' => $limit]); // Process products $offset += $limit; } while (count($products) == $limit);
Always wrap your API calls in try-catch blocks:
try { $product = $client->getProductById($productId); } catch (\Exception $e) { echo "Oops! " . $e->getMessage(); }
Webhooks are your friends for real-time updates. Set up an endpoint in your app:
$payload = file_get_contents('php://input'); $event = json_decode($payload, true); switch ($event['eventType']) { case 'order.created': // Handle new order break; // Add more cases as needed }
Use Ecwid's sandbox environment for testing. Just change your API endpoint:
$client->setApiUrl('https://app.ecwid.com/api/v3-sandbox');
And there you have it! You're now equipped to build some seriously cool Ecwid integrations. Remember, the API is your playground - don't be afraid to experiment and push the boundaries. Happy coding, and may your integrations be ever awesome!