Exemplo n.º 1
0
        // When the quantity adjustment buttons are clicked they will trigger this event handler.
        private void HandleVariantLineItemQuantityAdjustment(ProductVariant variant, int quantityAdjustment)
        {
            // First we find the lineitem associated with the the variant
            var lineItem = _cart.LineItems.Get(variant);

            // We then update the current quantity with the adjustment
            _cart.LineItems.AddOrUpdate(variant, lineItem.Quantity + quantityAdjustment);

            // In addition to updating the cart itself, we also need to update the associated UI elements. We begin
            // this process by retrieving the cart panel line item in question
            var cartPanelLineItem = _idCartPanelLineItemMapping[variant.id()];


            if (lineItem.Quantity < 1)
            {
                // If the lineitem quantity has reached zero, then we need to remove its visual representation
                // which is accomplished by destroying the game object
                Destroy(cartPanelLineItem.gameObject);
                // We also take care of removing the associated mappings
                _cart.LineItems.Delete(variant);
                _lineItems.Remove(cartPanelLineItem);
                _idCartPanelLineItemMapping.Remove(variant.id());
            }
            else
            {
                // In the case where we have a new, non zero quantity, we take care of updating the UI to
                // reflect this changed quantity
                cartPanelLineItem.Quantity.text = lineItem.Quantity.ToString();
            }

            // Finally, we dispatch an event saying we've updated the cart quantity, so any secondary calculations
            // can be handled
            DispatchCartQuantityChanged();
        }
Exemplo n.º 2
0
 private void CacheProductVariantPair(ProductVariant variant, Product product)
 {
     if (!_idsToProductPairs.ContainsKey(variant.id()))
     {
         var pair = new ProductAndVariantPair(product, variant);
         _idsToProductPairs.Add(variant.id(), pair);
     }
 }
Exemplo n.º 3
0
        private void HandleVariantLineItemQuantityAdjustment(ProductVariant variant, int quantityAdjustment)
        {
            var lineItem = _cart.LineItems.Get(variant);

            _cart.LineItems.AddOrUpdate(variant, lineItem.Quantity + quantityAdjustment);
            var cartPanelLineItem = _idCartPanelLineItemMapping[variant.id()];

            if (lineItem.Quantity < 1)
            {
                Destroy(cartPanelLineItem.gameObject);
                _cart.LineItems.Delete(variant);
                _lineItems.Remove(cartPanelLineItem);
                _idCartPanelLineItemMapping.Remove(variant.id());
            }
            else
            {
                cartPanelLineItem.Quantity.text = lineItem.Quantity.ToString();
            }
            DispatchCartQuantityChanged();
        }
Exemplo n.º 4
0
        /// <summary>
        /// Used internally by the SDK to construct a CartLineItem.
        /// </summary>
        /// <param name="variant">The variant this CartLineItem will be associated to</param>
        /// <param name="onChange">This function will be called whenever the CartLineItem is modified</param>
        /// <param name="quantity">The count of items to be ordered</param>
        /// <param name="customAttributes">Custom attributes for this line item used to customize and add meta data</param>
        public CartLineItem(ProductVariant variant, LineItemChangeHandler onChange, long quantity = 1, IDictionary <string, string> customAttributes = null)
        {
            _VariantId = variant.id();
            _Quantity  = quantity;
            _Price     = variant.priceV2().amount();
            OnChange   = onChange;

            if (customAttributes != null)
            {
                CustomAttributes = new ObservableDictionary <string, string>(customAttributes, () => { OnChange(CartLineItems.LineItemChangeType.Update, this); });
            }
        }
Exemplo n.º 5
0
        /// <summary>
        /// Sets the variant to the specified quantity in the cart.
        /// </summary>
        /// <param name="variant">The variant to modify</param>
        /// <param name="product">The product the variant belongs to</param>
        /// <param name="quantity">The desired quantity</param>
        public void UpdateVariant(ProductVariant variant, Product product, long quantity)
        {
            if (quantity <= 0)
            {
                Cart.LineItems.Delete(variant);
                RemoveProductVariantPairByIDFromCache(variant.id());
            }
            else
            {
                Cart.LineItems.AddOrUpdate(variant, quantity);
                CacheProductVariantPair(variant, product);
            }

            OnQuantityChange.Invoke(TotalItemsInCart());
            OnCartItemsChange.Invoke(CartItems);
        }
Exemplo n.º 6
0
        public void AddToCart(Product product, ProductVariant variant)
        {
            if (_cart == null)
            {
                _cart = ShopifyHelper.CreateCart();
            }

            // Handle adding a particular variant to the cart
            // For more information on adding variants to the cart visit
            // https://help.shopify.com/api/sdks/custom-storefront/unity-buy-sdk/getting-started#create-cart-line-items-based-on-selected-options

            var existingLineItem = _cart.LineItems.Get(variant);

            if (existingLineItem == null)
            {
                _cart.LineItems.AddOrUpdate(variant);

                var instance = Instantiate(CartPanelLineItemTemplate);
                instance.transform.SetParent(Content, false);
                instance.SetCurrentProduct(product, variant, 1);

                instance.OnVariantLineItemQuantityAdjustment.AddListener(HandleVariantLineItemQuantityAdjustment);

                _idCartPanelLineItemMapping.Add(variant.id(), instance);
                _lineItems.Add(instance);
            }
            else
            {
                _cart.LineItems.AddOrUpdate(variant, existingLineItem.Quantity + 1);

                var cartPanelLineItem = _idCartPanelLineItemMapping[variant.id()];
                cartPanelLineItem.Quantity.text = existingLineItem.Quantity.ToString();
            }

            if (!_idVariantMapping.ContainsKey(variant.id()))
            {
                _idVariantMapping.Add(variant.id(), variant);
            }

            if (!_idProductMapping.ContainsKey(variant.id()))
            {
                _idProductMapping.Add(variant.id(), product);
            }

            DispatchCartQuantityChanged();
            UpdateSeparatorVisibility();
        }
Exemplo n.º 7
0
        /// <summary>
        /// Adds or updates a line item using a <see ref="ProductVariant">ProductVariant </see>.
        /// </summary>
        /// <param name="variant"><see ref="ProductVariant">ProductVariant </see> whose id will be used to create or update a line item</param>
        /// <param name="quantity">the number of items you'd like to order for variantId</param>
        /// <param name="customAttributes">can be used to define extra information for this line item</param>
        /// \code
        /// // Example that updates the quantity of items to be purchased to 3.
        /// // If no line item exists for `variantId`, then a new line item is created
        /// Cart cart = ShopifyBuy.Client().Cart();
        ///
        /// cart.LineItems.AddOrUpdate(variant, 3);
        /// \endcode
        public void AddOrUpdate(ProductVariant variant, long?quantity = null, Dictionary <string, string> customAttributes = null)
        {
            CartLineItem lineItem = Get(variant.id());

            if (lineItem != null)
            {
                if (quantity != null && lineItem.Quantity != (long)quantity)
                {
                    lineItem.Quantity = (long)quantity;
                }

                if (customAttributes != null)
                {
                    lineItem.CustomAttributes = customAttributes;
                }
            }
            else
            {
                if (quantity == null)
                {
                    quantity = 1;
                }

                lineItem = new CartLineItem(
                    variant: variant,
                    onChange: OnLineItemChange,
                    quantity: (long)quantity,
                    customAttributes: customAttributes
                    );

                LineItems.Add(lineItem);

                if (OnChange != null)
                {
                    OnChange(LineItemChangeType.Add, lineItem);
                }
            }
        }
Exemplo n.º 8
0
        /// <summary>
        /// Removes a variant from the cart.
        /// </summary>
        /// <param name="variant">The variant to remove from the cart</param>
        /// <param name="product">The product the variant belongs to</param>
        public void RemoveVariant(ProductVariant variant, Product product)
        {
            var existingItem = Cart.LineItems.Get(variant);

            if (existingItem == null)
            {
                return;
            }
            var newQuantity = existingItem.Quantity - 1;

            if (newQuantity == 0)
            {
                Cart.LineItems.Delete(variant);
                RemoveProductVariantPairByIDFromCache(variant.id());
            }
            else
            {
                Cart.LineItems.AddOrUpdate(variant, newQuantity);
                CacheProductVariantPair(variant, product);
            }

            OnQuantityChange.Invoke(TotalItemsInCart());
            OnCartItemsChange.Invoke(CartItems);
        }
Exemplo n.º 9
0
        public void AddToCart(Product product, ProductVariant variant)
        {
            if (_cart == null)
            {
                _cart = ShopifyHelper.CreateCart();
            }

            var existingLineItem = _cart.LineItems.Get(variant);

            if (existingLineItem == null)
            {
                _cart.LineItems.AddOrUpdate(variant);
                var instance = Instantiate(HatCartPanelItem);
                instance.transform.SetParent(Content, false);
                instance.SetCurrentProduct(product, variant, 1);
                instance.OnVariantLineItemQuantityAdjustment.AddListener(HandleVariantLineItemQuantityAdjustment);
                _idCartPanelLineItemMapping.Add(variant.id(), instance);
                _lineItems.Add(instance);
            }
            else
            {
                _cart.LineItems.AddOrUpdate(variant, existingLineItem.Quantity + 1);

                var cartPanelLineItem = _idCartPanelLineItemMapping[variant.id()];
                cartPanelLineItem.Quantity.text = existingLineItem.Quantity.ToString();
            }
            if (!_idVariantMapping.ContainsKey(variant.id()))
            {
                _idVariantMapping.Add(variant.id(), variant);
            }

            if (!_idProductMapping.ContainsKey(variant.id()))
            {
                _idProductMapping.Add(variant.id(), product);
            }

            DispatchCartQuantityChanged();
            // UpdateSeparatorVisibility();
        }
Exemplo n.º 10
0
 /// <summary>
 /// Deletes one <see ref="CartLineItem">Line Item </see> based on a <see ref="ProductVariant">ProductVariant </see>. If a line
 /// item was deleted, <c>true</c> will be returned. If no line items were deleted, <c>false</c> will be returned.
 /// </summary>
 /// <param name="variant"><see ref="ProductVariant">variant </see> to provide the ID to delete a line item</param>
 /// \code
 /// // Example that deletes a line item based on a product variant
 /// Cart cart = ShopifyBuy.Client().Cart();
 ///
 /// Debug.Log("Did delete? " + cart.LineItems.Delete(variant));
 /// \endcode
 public bool Delete(ProductVariant variant)
 {
     return(Delete(variant.id()));
 }
Exemplo n.º 11
0
 /// <summary>
 /// Returns one <see ref="CartLineItem">Line Item </see> based on a <see ref="ProductVariant">ProductVariant </see>. If no line item
 /// exists for the variant, <c>null</c> will be returned.
 /// </summary>
 /// <param name="variant">variant whose variant id used to create a line item</param>
 /// \code
 /// // Example that checks the quantity of a line item based on a variant
 /// Cart cart = ShopifyBuy.Client().Cart();
 ///
 /// Debug.Log(cart.LineItems.Get(variant).quantity);
 /// \endcode
 public CartLineItem Get(ProductVariant variant)
 {
     return(Get(variant.id()));
 }