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;
    }
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;
    }
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 ; }
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
Return the cart item id which contains the free product related to a given product