/** * Create a new product sale element, with or without combination * * @param ProductSaleElementCreateEvent $event * @throws \Exception */ public function create(ProductSaleElementCreateEvent $event) { $con = Propel::getWriteConnection(ProductSaleElementsTableMap::DATABASE_NAME); $con->beginTransaction(); try { // Check if we have a PSE without combination, this is the "default" PSE. Attach the combination to this PSE $salesElement = ProductSaleElementsQuery::create()->filterByProductId($event->getProduct()->getId())->joinAttributeCombination(null, Criteria::LEFT_JOIN)->add(AttributeCombinationTableMap::PRODUCT_SALE_ELEMENTS_ID, null, Criteria::ISNULL)->findOne($con); if ($salesElement == null) { // Create a new default product sale element $salesElement = $event->getProduct()->createProductSaleElement($con, 0, 0, 0, $event->getCurrencyId(), false); } else { // This (new) one is the default $salesElement->setIsDefault(true)->save($con); } // Attach combination, if defined. $combinationAttributes = $event->getAttributeAvList(); if (count($combinationAttributes) > 0) { foreach ($combinationAttributes as $attributeAvId) { $attributeAv = AttributeAvQuery::create()->findPk($attributeAvId); if ($attributeAv !== null) { $attributeCombination = new AttributeCombination(); $attributeCombination->setAttributeAvId($attributeAvId)->setAttribute($attributeAv->getAttribute())->setProductSaleElements($salesElement)->save($con); } } } $event->setProductSaleElement($salesElement); // Store all the stuff ! $con->commit(); } catch (\Exception $ex) { $con->rollback(); throw $ex; } }