コード例 #1
0
        private void OnLineItemChange(LineItemChangeType type, CartLineItem item)
        {
            _IsSaved = false;

            if (OnChange != null)
            {
                OnChange(type, item);
            }
        }
コード例 #2
0
        /// <summary>
        /// Deletes one <see ref="CartLineItem">Line Item </see> based on a variant id. 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="variantId">variant id used to delete a line item</param>
        /// \code
        /// // Example that deletes a line item based on variantId
        /// Cart cart = ShopifyBuy.Client().Cart();
        ///
        /// Debug.Log("Did delete? " + cart.LineItems.Delete(variantId));
        /// \endcode
        public bool Delete(string variantId)
        {
            int idxToDelete = LineItems.FindIndex(lineItem => lineItem.VariantId == variantId);

            if (idxToDelete == -1)
            {
                return(false);
            }
            else
            {
                CartLineItem lineItemRemoved = LineItems[idxToDelete];
                LineItems.RemoveAt(idxToDelete);

                if (OnChange != null)
                {
                    OnChange(LineItemChangeType.Remove, lineItemRemoved);
                }

                return(true);
            }
        }
コード例 #3
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);
                }
            }
        }