public function setProductTemplate(ProductSetTemplateEvent $event) { $con = Propel::getWriteConnection(ProductTableMap::DATABASE_NAME); $con->beginTransaction(); try { $product = $event->getProduct(); // Delete all product feature relations if (null !== ($featureProducts = FeatureProductQuery::create()->findByProductId($product->getId()))) { /** @var \Thelia\Model\FeatureProduct $featureProduct */ foreach ($featureProducts as $featureProduct) { $eventDelete = new FeatureProductDeleteEvent($product->getId(), $featureProduct->getFeatureId()); $event->getDispatcher()->dispatch(TheliaEvents::PRODUCT_FEATURE_DELETE_VALUE, $eventDelete); } } // Delete all product attributes sale elements AttributeCombinationQuery::create()->filterByProductSaleElements($product->getProductSaleElementss())->delete($con); //Delete all productSaleElements except the default one (to keep price, weight, ean, etc...) ProductSaleElementsQuery::create()->filterByProduct($product)->filterByIsDefault(1, Criteria::NOT_EQUAL)->delete($con); // Update the product template $template_id = $event->getTemplateId(); // Set it to null if it's zero. if ($template_id <= 0) { $template_id = null; } $product->setTemplateId($template_id)->save($con); //Be sure that the product has a default productSaleElements /** @var \Thelia\Model\ProductSaleElements $defaultPse */ if (null == ($defaultPse = ProductSaleElementsQuery::create()->filterByProduct($product)->filterByIsDefault(1)->findOne())) { // Create a new default product sale element $product->createProductSaleElement($con, 0, 0, 0, $event->getCurrencyId(), true); } $product->clearProductSaleElementss(); $event->setProduct($product); // Store all the stuff ! $con->commit(); } catch (\Exception $ex) { $con->rollback(); throw $ex; } }