akeneo-labs/classification-rule-bundle
Installation:
composer require akeneo-labs/classification-rule-bundle:1.2.*
Enable the bundle in AppKernel.php:
new PimEnterprise\Bundle\ClassificationRuleBundle\PimEnterpriseClassificationRuleBundle(),
Clear cache and reinstall assets:
php app/console cache:clear --env=prod
php app/console pim:installer:assets --env=prod
First Use Case:
Define a rule to unclassify products from a category tree. Example rule (resources/config/rules.yml):
classification_rule_unclassify_example:
conditions:
- condition: "product.attribute.text == 'example'"
actions:
- action: "unclassify"
category_tree: "ecommerce"
category_code: "electronics"
Trigger the Rule: Use Akeneo’s rule engine to apply the rule to products:
php app/console pim:rule:execute classification_rule_unclassify_example
Rule-Based Declassification:
unclassify action to remove products from categories based on conditions (e.g., attribute values, product families).is_discontinued = true from the "promotions" category tree.Integration with Akeneo’s Rule Engine:
product.attribute, product.family).classify, set_attribute) for complex workflows.Batch Processing:
php app/console pim:rule:execute rule_name --products=1,2,3
Dynamic Category Trees:
category_tree in actions to target specific category hierarchies (e.g., "apparel", "electronics").Extend Existing Rules:
Modify existing CatalogRuleBundle rules to include unclassify actions. Example:
conditions:
- condition: "product.family == 'apparel'"
actions:
- action: "unclassify"
category_tree: "ecommerce"
- action: "classify"
category_code: "sale_items"
Event Listeners: Trigger rules on product updates via Akeneo’s event system:
// src/Akeneo/YourBundle/EventListener/RuleListener.php
public function onProductUpdate(ProductUpdateEvent $event) {
$this->ruleEngine->execute('classification_rule_unclassify_example', [$event->getProduct()]);
}
API Integration: Use Akeneo’s REST API to fetch products and apply rules programmatically:
$client = new Client();
$response = $client->get('/api/rest/v1/products');
$products = json_decode($response->getBody(), true);
$this->ruleEngine->execute('rule_name', $products);
Deprecated Interfaces:
Akeneo\Component\Classification\Repository\CategoryRepositoryInterface). Ensure your codebase aligns with Akeneo PIM Enterprise’s latest dependencies.config.yml to use the new interfaces.Rule Engine Compatibility:
pim:rule:debug to validate rule syntax:
php app/console pim:rule:debug classification_rule_unclassify_example
Category Tree Scope:
unclassify action only affects categories within the specified category_tree. Products may remain classified in other trees.category_code: null to declassify from all categories in the tree.Performance:
php app/console pim:rule:execute rule_name --limit=100
Logs:
Enable debug mode (APP_DEBUG=true) and check var/log/dev.log for rule execution errors.
Example log entry:
[2023-01-01 12:00:00] pim.rule.ERROR: Failed to unclassify product SKU-123: Category "electronics" not found in tree "ecommerce" []
Dry Runs:
Test rules without saving changes by mocking the ProductUpdater service in tests.
Custom Actions:
Extend the rule engine to add new actions (e.g., reclassify):
// src/Akeneo/YourBundle/Rule/Action/ReclassifyAction.php
class ReclassifyAction implements ActionInterface {
public function execute(ProductInterface $product, array $parameters) {
// Logic to reclassify product to a new category
}
}
Condition Providers:
Add custom conditions (e.g., product.price > 100):
// src/Akeneo/YourBundle/Rule/Condition/PriceCondition.php
class PriceCondition implements ConditionInterface {
public function isValid(ProductInterface $product, array $parameters) {
return $product->getPrice() > $parameters['threshold'];
}
}
Configuration:
Override default rule configurations in config/packages/pim_enterprise_classification_rule.yaml:
pim_enterprise_classification_rule:
default_category_tree: "custom_tree"
Environment-Specific Rules:
Use environment variables to dynamically set category_tree or category_code:
actions:
- action: "unclassify"
category_tree: "%env(CLASSIFICATION_TREE)%"
Rule Inheritance:
Rules defined in child bundles (e.g., YourBundle) override those in parent bundles (e.g., ClassificationRuleBundle). Use explicit bundle ordering in AppKernel.php.
How can I help you explore Laravel packages today?