Installation
composer require ederribeiro/wooapubundle "~1.*"
Add the bundle to AppKernel.php:
new Woo\ApuBundle\WooApuBundle(),
Configure credentials in parameters.yml.dist:
woo_apu:
consumer_key: "your_woocommerce_consumer_key"
consumer_secret: "your_woocommerce_consumer_secret"
shop: "https://your-store.com"
First Use Case Inject the client service into a controller/service:
use Symfony\Component\DependencyInjection\ContainerInterface;
class ProductController
{
public function __construct(ContainerInterface $container)
{
$this->client = $container->get("wooapu.client");
}
public function fetchProducts()
{
$response = $this->client->get('/products');
return json_decode($response->getBody(), true);
}
}
REST API Calls Use the client for standard WooCommerce API operations:
// Fetch products
$products = $this->client->get('/products')->getBody();
// Create an order
$orderData = ['payment_method' => 'bacs', 'billing' => [...]];
$order = $this->client->post('/orders', $orderData)->getBody();
Dependency Injection Prefer constructor injection for testability:
class OrderService
{
public function __construct(private WooApuClient $client) {}
}
Error Handling Validate responses with HTTP status checks:
$response = $this->client->get('/products');
if ($response->getStatusCode() !== 200) {
throw new \RuntimeException('Failed to fetch products');
}
Pagination Handle paginated endpoints:
$page = 1;
$perPage = 10;
$response = $this->client->get("/products?page=$page&per_page=$perPage");
order_created) via the API.Authentication Failures
consumer_key/consumer_secret throws 401 Unauthorized.parameters.yml and regenerate keys in WooCommerce → Settings → Advanced → REST API.Rate Limiting
Legacy Symfony Kernel
AppKernel). For Symfony 4/5, use config/bundles.php instead of AppKernel.php.Deprecated Methods
Psr\Http\Client\ClientInterface).debug: true to parameters.yml for verbose API logs.$headers = $response->getHeaders();
Custom Endpoints Extend the client to support non-standard routes:
$this->client->setBaseUri('https://custom-endpoint.com');
Middleware Add request/response middleware:
$client->getEmitter()->addSubscriber(new CustomMiddleware());
Testing Mock the client in tests:
$mockClient = $this->createMock(WooApuClient::class);
$mockClient->method('get')->willReturn(new Response(200, [], json_encode([])));
$this->container->set('wooapu.client', $mockClient);
shop URL uses https://; WooCommerce REST API rejects HTTP.woo_apu:
consumer_key: "%env(WOOCOMMERCE_KEY)%"
How can I help you explore Laravel packages today?