Esempio n. 1
0
 /**
  * Create a basic product sale element attached to this product.
  */
 public function createProductSaleElement(ConnectionInterface $con, $weight, $basePrice, $salePrice, $currencyId, $isDefault, $isPromo = false, $isNew = false, $quantity = 0, $eanCode = '', $ref = false)
 {
     // Create an empty product sale element
     $saleElements = new ProductSaleElements();
     $saleElements->setProduct($this)->setRef($ref == false ? $this->getRef() : $ref)->setPromo($isPromo)->setNewness($isNew)->setWeight($weight)->setIsDefault($isDefault)->setEanCode($eanCode)->setQuantity($quantity)->save($con);
     // Create an empty product price in the provided currency
     $productPrice = new ProductPrice();
     $productPrice->setProductSaleElements($saleElements)->setPromoPrice($salePrice)->setPrice($basePrice)->setCurrencyId($currencyId)->setFromDefaultCurrency(false)->save($con);
     return $saleElements;
 }
 /**
  * Update an existing product sale element
  *
  * @param  ProductSaleElementUpdateEvent $event
  * @throws \Exception
  */
 public function update(ProductSaleElementUpdateEvent $event)
 {
     $salesElement = ProductSaleElementsQuery::create()->findPk($event->getProductSaleElementId());
     $con = Propel::getWriteConnection(ProductSaleElementsTableMap::DATABASE_NAME);
     $con->beginTransaction();
     try {
         // Update the product's tax rule
         $event->getProduct()->setTaxRuleId($event->getTaxRuleId())->save($con);
         // If product sale element is not defined, create it.
         if ($salesElement == null) {
             $salesElement = new ProductSaleElements();
             $salesElement->setProduct($event->getProduct());
         }
         // If this PSE is the default one, be sure to have *only one* default for this product
         if ($event->getIsDefault()) {
             ProductSaleElementsQuery::create()->filterByProduct($event->getProduct())->filterByIsDefault(true)->filterById($event->getProductSaleElementId(), Criteria::NOT_EQUAL)->update(['IsDefault' => false], $con);
         }
         // Update sale element
         $salesElement->setRef($event->getReference())->setQuantity($event->getQuantity())->setPromo($event->getOnsale())->setNewness($event->getIsnew())->setWeight($event->getWeight())->setIsDefault($event->getIsDefault())->setEanCode($event->getEanCode())->save();
         // Update/create price for current currency
         $productPrice = ProductPriceQuery::create()->filterByCurrencyId($event->getCurrencyId())->filterByProductSaleElementsId($salesElement->getId())->findOne($con);
         // If price is not defined, create it.
         if ($productPrice == null) {
             $productPrice = new ProductPrice();
             $productPrice->setProductSaleElements($salesElement)->setCurrencyId($event->getCurrencyId());
         }
         // Check if we have to store the price
         $productPrice->setFromDefaultCurrency($event->getFromDefaultCurrency());
         if ($event->getFromDefaultCurrency() == 0) {
             // Store the price
             $productPrice->setPromoPrice($event->getSalePrice())->setPrice($event->getPrice());
         } else {
             // Do not store the price.
             $productPrice->setPromoPrice(0)->setPrice(0);
         }
         $productPrice->save($con);
         // Store all the stuff !
         $con->commit();
     } catch (\Exception $ex) {
         $con->rollback();
         throw $ex;
     }
 }