sylius/core
Sylius Core integrates all Sylius components into a flexible PHP eCommerce framework, powering storefront and admin features with a decoupled architecture and strong API support. Documentation, contribution guides, and issue tracking available.
Installation:
composer require sylius/core
Ensure you have sylius/resource-bundle and sylius/doctrine-bundle installed as dependencies, as they are core to Sylius's functionality.
Configuration:
Add the bundles to your config/bundles.php:
return [
// ...
Sylius\Bundle\CoreBundle\SyliusCoreBundle::class => ['all' => true],
Sylius\Bundle\ResourceBundle\SyliusResourceBundle::class => ['all' => true],
Sylius\Bundle\DoctrineBundle\SyliusDoctrineBundle::class => ['all' => true],
];
Database Setup: Run migrations to create the required tables:
php bin/console doctrine:migrations:migrate
First Use Case: Create a product programmatically:
use Sylius\Component\Core\Model\Product;
$product = new Product();
$product->setName('Test Product');
$product->setSlug('test-product');
$product->setEnabled(true);
$productManager = $this->container->get('sylius.manager.product_manager');
$productManager->save($product);
Sylius\Component\Core\Model for core entities like Product, Order, Customer, etc.Sylius\Bundle\CoreBundle\Resources/config/services.xml for key services like product_manager, order_manager, and cart_manager.product_manager service to create, update, or delete products.
$product = $productManager->find(1); // Find by ID
$product->setPrice('19.99');
$productManager->save($product);
ProductVariant and ProductOption:
$variant = new ProductVariant();
$variant->setProduct($product);
$variant->setOnHand(10);
$variant->setEnabled(true);
$productVariantManager->save($variant);
cart_resolver and cart_manager to handle carts:
$cart = $cartManager->create();
$cartItem = new CartItem();
$cartItem->setProduct($product);
$cartItem->setQuantity(2);
$cartItem->setUnitPrice($product->getPrice());
$cart->add($cartItem);
$cartManager->persist($cart);
$order = $orderManager->createFromCart($cart);
$order->setState(OrderInterface::STATE_COMPLETED);
$orderManager->save($order);
Customer entity:
$customer = new Customer();
$customer->setEmail('user@example.com');
$customer->setPlainPassword('securepassword');
$customerManager->save($customer);
$address = new Address();
$address->setFirstName('John');
$address->setStreet('123 Main St');
$customer->addAddress($address);
$customerManager->save($customer);
products, orders, and customers:
# config/packages/api_platform.yaml
api_platform:
formats:
jsonld:
mime_types: ['application/ld+json']
patch_formats:
json: true
sylius/graphql bundle for GraphQL queries/mutations:
query {
products {
edges {
node {
name
slug
price
}
}
}
}
sylius.order.completed) to trigger custom logic:
use Sylius\Component\Core\Event\OrderCompletedEvent;
$eventDispatcher->addListener(OrderCompletedEvent::NAME, function (OrderCompletedEvent $event) {
// Send email, update inventory, etc.
});
sylius.order.placedsylius.product.createdsylius.customer.registeredExtend Sylius entities using Doctrine inheritance:
use Sylius\Component\Core\Model\Product as BaseProduct;
class CustomProduct extends BaseProduct
{
private $customField;
// Add getters/setters for customField
}
Register the custom entity in config/packages/sylius_core.yaml:
sylius_core:
resources:
product:
classes:
model: App\Entity\CustomProduct
Override Twig templates in your theme bundle:
templates/
SyliusUi/
Store/
Product/
show.html.twig
Use Sylius's Attribute system to add custom fields to entities:
$attribute = new Attribute();
$attribute->setType('pim_core_attribute_type_string');
$attribute->setCode('custom_field');
$attribute->setName('Custom Field');
$attributeManager->save($attribute);
Assign attributes to products:
$productAttributeValue = new ProductAttributeValue();
$productAttributeValue->setAttribute($attribute);
$productAttributeValue->setValue('Custom Value');
$product->addAttributeValue($productAttributeValue);
Use Sylius's test utilities for functional/integration tests:
use Sylius\Bundle\CoreBundle\Tests\Functional\WebTestCase;
class MyTest extends WebTestCase
{
public function testSomething()
{
$client = static::createClient();
$client->request('GET', '/products');
// Assertions...
}
}
EntityManager before saving:
$productManager->save($product); // Correct
$em->persist($product); // May fail if not managed by Sylius's manager
Product → ProductVariant). Be mindful of unintended saves/deletes.cart → checkout → completed). Transition states explicitly:
$order->setState(OrderInterface::STATE_CHECKOUT); // Not $order->setState('checkout');
#[Groups] attributes to control API output:
use ApiPlatform\Core\Annotation\Groups;
class Product
{
#[Groups(['product:read'])]
private $name;
}
knplabs/knp-paginator-bundle by default. Configure pagination in api_platform.yaml:
api_platform:
pagination_enabled: true
prePersist). Overriding these requires caution to avoid conflicts.sylius/translation for translations. Ensure fallback locales are configured in config/packages/sylius_core.yaml:
sylius_core:
locales: [en_US, fr_FR]
fallback_locale: en_US
Enable Sylius logging in config/packages/dev/monolog.yaml:
monolog:
handlers:
sylius:
type: stream
path
How can I help you explore Laravel packages today?