public function checkStock(ProductSaleElementUpdateEvent $productSaleElementUpdateEvent) { if ($productSaleElementUpdateEvent->getQuantity() > 0) { // add extra checking $pse = ProductSaleElementsQuery::create()->findPk($productSaleElementUpdateEvent->getProductSaleElementId()); $availabilityEvent = new ProductSaleElementAvailabilityEvent($pse); $productSaleElementUpdateEvent->getDispatcher()->dispatch(StockAlertEvents::STOCK_ALERT_CHECK_AVAILABILITY, $availabilityEvent); if ($availabilityEvent->isAvailable()) { $subscribers = RestockingAlertQuery::create()->filterByProductSaleElementsId($productSaleElementUpdateEvent->getProductSaleElementId())->find(); if (null !== $subscribers) { foreach ($subscribers as $subscriber) { try { $this->sendEmail($subscriber); $subscriber->delete(); } catch (\Exception $ex) { } } } } } }
/** * 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; } }