/** * @inheritdoc * * Find a possible existing cart item by also filtering on our cart item attribute combinations. */ protected function findItem($cartId, $productId, $productSaleElementsId) { // no legacy attributes, let the parent handle it if (empty($this->legacyProductAttributes)) { return parent::findItem($cartId, $productId, $productSaleElementsId); } $query = CartItemQuery::create(); $query->filterByCartId($cartId)->filterByProductId($productId)->filterByProductSaleElementsId($productSaleElementsId); /** @var CartItem $cartItem */ foreach ($query->find() as $cartItem) { $legacyCartItemAttributeCombinations = LegacyCartItemAttributeCombinationQuery::create()->findByCartItemId($cartItem->getId()); $cartItemLegacyProductAttributes = []; /** @var LegacyCartItemAttributeCombination $legacyCartItemAttributeCombination */ foreach ($legacyCartItemAttributeCombinations as $legacyCartItemAttributeCombination) { $cartItemLegacyProductAttributes[$legacyCartItemAttributeCombination->getAttributeId()] = $legacyCartItemAttributeCombination->getAttributeAvId(); } if ($cartItemLegacyProductAttributes == $this->legacyProductAttributes) { return $cartItem; } } return null; }
/** * Return the cart item id which contains the free product related to a given product * * @param Product $product the product in the cart which triggered the discount * * @return bool|int|CartItem the cart item which contains the free product, or false if the product is no longer in the cart, or ADD_TO_CART_IN_PROCESS if the adding process is not finished */ protected function getRelatedCartItem($product) { $cartItemIdList = $this->facade->getRequest()->getSession()->get($this->getSessionVarName(), array()); if (isset($cartItemIdList[$product->getId()])) { $cartItemId = $cartItemIdList[$product->getId()]; if ($cartItemId == self::ADD_TO_CART_IN_PROCESS) { return self::ADD_TO_CART_IN_PROCESS; } elseif (null !== ($cartItem = CartItemQuery::create()->findPk($cartItemId))) { return $cartItem; } } else { // Maybe the product we're offering is already in the cart ? Search it. $cartItems = $this->facade->getCart()->getCartItems(); /** @var CartItem $cartItem */ foreach ($cartItems as $cartItem) { if ($cartItem->getProduct()->getId() == $this->offeredProductId) { // We found the product. Store its cart item as the free product container. $this->setRelatedCartItem($product, $cartItem->getId()); return $cartItem; } } } return false; }
/** * Find a specific record in CartItem table using the current CartEvent * * @param CartEvent $event the cart event */ public function findCartItem(CartEvent $event) { if (null !== ($foundItem = CartItemQuery::create()->filterByCartId($event->getCart()->getId())->filterByProductId($event->getProduct())->filterByProductSaleElementsId($event->getProductSaleElementsId())->findOne())) { $event->setCartItem($foundItem); } }
/** * If this collection has already been initialized with * an identical criteria, it returns the collection. * Otherwise if this ProductSaleElements is new, it will return * an empty collection; or if this ProductSaleElements has previously * been saved, it will retrieve related CartItems from storage. * * This method is protected by default in order to keep the public * api reasonable. You can provide public methods for those you * actually need in ProductSaleElements. * * @param Criteria $criteria optional Criteria object to narrow the query * @param ConnectionInterface $con optional connection object * @param string $joinBehavior optional join type to use (defaults to Criteria::LEFT_JOIN) * @return Collection|ChildCartItem[] List of ChildCartItem objects */ public function getCartItemsJoinProduct($criteria = null, $con = null, $joinBehavior = Criteria::LEFT_JOIN) { $query = ChildCartItemQuery::create(null, $criteria); $query->joinWith('Product', $joinBehavior); return $this->getCartItems($query, $con); }
/** * Find a specific record in CartItem table using the current CartEvent * * @param CartEvent $event the cart event */ public function findCartItem(CartEvent $event) { // Do not try to find a cartItem if one exists in the event, as previous event handlers // mays have put it in th event. if (null === $event->getCartItem() && null !== ($foundItem = CartItemQuery::create()->filterByCartId($event->getCart()->getId())->filterByProductId($event->getProduct())->filterByProductSaleElementsId($event->getProductSaleElementsId())->findOne())) { $event->setCartItem($foundItem); } }
/** * Performs an INSERT on the database, given a CartItem or Criteria object. * * @param mixed $criteria Criteria or CartItem object containing data that is used to create the INSERT statement. * @param ConnectionInterface $con the ConnectionInterface connection to use * @return mixed The new primary key. * @throws PropelException Any exceptions caught during processing will be * rethrown wrapped into a PropelException. */ public static function doInsert($criteria, ConnectionInterface $con = null) { if (null === $con) { $con = Propel::getServiceContainer()->getWriteConnection(CartItemTableMap::DATABASE_NAME); } if ($criteria instanceof Criteria) { $criteria = clone $criteria; // rename for clarity } else { $criteria = $criteria->buildCriteria(); // build Criteria from CartItem object } if ($criteria->containsKey(CartItemTableMap::ID) && $criteria->keyContainsValue(CartItemTableMap::ID)) { throw new PropelException('Cannot insert a value for auto-increment primary key (' . CartItemTableMap::ID . ')'); } // Set the correct dbName $query = CartItemQuery::create()->mergeWith($criteria); try { // use transaction because $criteria could contain info // for more than one table (I guess, conceivably) $con->beginTransaction(); $pk = $query->doInsert($con); $con->commit(); } catch (PropelException $e) { $con->rollBack(); throw $e; } return $pk; }
/** * find a specific record in CartItem table using the Cart id, the product id * and the product_sale_elements id * * @param int $cartId * @param int $productId * @param int $productSaleElementsId * @return ChildCartItem */ protected function findItem($cartId, $productId, $productSaleElementsId) { return CartItemQuery::create()->filterByCartId($cartId)->filterByProductId($productId)->filterByProductSaleElementsId($productSaleElementsId)->findOne(); }
/** * Removes this object from datastore and sets delete attribute. * * @param ConnectionInterface $con * @return void * @throws PropelException * @see CartItem::setDeleted() * @see CartItem::isDeleted() */ public function delete(ConnectionInterface $con = null) { if ($this->isDeleted()) { throw new PropelException("This object has already been deleted."); } if ($con === null) { $con = Propel::getServiceContainer()->getWriteConnection(CartItemTableMap::DATABASE_NAME); } $con->beginTransaction(); try { $deleteQuery = ChildCartItemQuery::create()->filterByPrimaryKey($this->getPrimaryKey()); $ret = $this->preDelete($con); if ($ret) { $deleteQuery->delete($con); $this->postDelete($con); $con->commit(); $this->setDeleted(true); } else { $con->commit(); } } catch (Exception $e) { $con->rollBack(); throw $e; } }
/** * Get the associated ChildCartItem object * * @param ConnectionInterface $con Optional Connection object. * @return ChildCartItem The associated ChildCartItem object. * @throws PropelException */ public function getCartItem(ConnectionInterface $con = null) { if ($this->aCartItem === null && $this->cart_item_id !== null) { $this->aCartItem = CartItemQuery::create()->findPk($this->cart_item_id, $con); /* The following can be used additionally to guarantee the related object contains a reference to this object. This level of coupling may, however, be undesirable since it could result in an only partially populated collection in the referenced object. $this->aCartItem->addLegacyCartItemAttributeCombinations($this); */ } return $this->aCartItem; }