Ejemplo n.º 1
0
 /**
  * @depends testRemoveAccessory
  */
 public function testSetProductTemplate(ProductModel $product)
 {
     $templateId = TemplateQuery::create()->select('id')->addAscendingOrderByColumn('RAND()')->findOne();
     $currencyId = CurrencyQuery::create()->select('id')->addAscendingOrderByColumn('RAND()')->findOne();
     $oldProductSaleElements = $product->getDefaultSaleElements();
     $this->assertEquals("Thelia\\Model\\ProductSaleElements", get_class($oldProductSaleElements), "There is no default pse for this product");
     $event = new ProductSetTemplateEvent($product, $templateId, $currencyId);
     $event->setDispatcher($this->getDispatcher());
     $action = new Product();
     $action->setProductTemplate($event);
     $updatedProduct = $event->getProduct();
     $this->assertEquals($templateId, $updatedProduct->getTemplateId());
     $productSaleElements = $updatedProduct->getProductSaleElementss();
     $this->assertEquals(1, count($productSaleElements), "after setting a new template, only 1 product_sale_elements must be present");
     /** @var \Thelia\Model\ProductSaleElements $newProductSaleElements */
     $newProductSaleElements = $productSaleElements->getFirst();
     $this->assertEquals($updatedProduct->getRef(), $newProductSaleElements->getRef(), sprintf("PSE ref must be %s", $updatedProduct->getRef()));
     $this->assertTrue($newProductSaleElements->getIsDefault(), 'new PSE must be the default one for this product');
     $productPrice = $newProductSaleElements->getProductPrices()->getFirst();
     $oldProductPrice = $oldProductSaleElements->getProductPrices()->getFirst();
     $this->assertEquals($oldProductSaleElements->getWeight(), $newProductSaleElements->getWeight(), sprintf("->testSetProductTemplate new PSE weight must be %s", $oldProductSaleElements->getWeight()));
     $this->assertEquals($oldProductPrice->getPrice(), $productPrice->getPrice(), sprintf("->testSetProductTemplate price must be %s", $oldProductPrice->getPrice()));
     $this->assertEquals($oldProductPrice->getPromoPrice(), $productPrice->getPromoPrice(), sprintf("->testSetProductTemplate promo price must be %s", $oldProductPrice->getPromoPrice()));
     $this->assertEquals($oldProductPrice->getCurrencyId(), $productPrice->getCurrencyId(), sprintf("->testSetProductTemplate currency_id must be %s", $oldProductPrice->getCurrencyId()));
     return $updatedProduct;
 }
Ejemplo n.º 2
0
 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;
     }
 }