HomeWiki
HomeWiki
  1. Collections
  • Back to home
  • 1. Themes
  • Layout
    • New Layout
    • Legacy Layout
  • Components
    • Announcement
    • Banner Carousel
    • Banner With Products Carousel
    • Blog Category List
    • Blog List
    • Brand List
    • Brands Carousel
    • Breadcrumb
    • Call To Action
    • Cart
    • Categories List
    • Change Password
    • Checkout
    • Cookie Manager
    • Filter list
    • Footer
    • Forgot Password
    • Form
    • Hero Carousel
    • Icon Block
    • Invitation
    • Last Visited Products
    • Layout
    • Login
    • Map
    • Nav Bar
    • Offer
    • Product Attachments
    • Product Attributes
    • Product Documentation
    • Product Expected
    • Product Modal
    • Products Block
    • Products Carousel
    • Product Single
    • Profile
    • Quote
    • Register
    • Related Products
    • Search
    • Stores
    • Subscribe Newsletter
    • Text with Image
    • Top Bar
    • Video
  • Reusables
    • Getting Started
  • Assets
    • Getting Started
  • SDK
    • Products
      • _findProductsByCategory
      • _findProductsByIds
      • _findProductsByTitle
      • _findProductsByFilter
      • _findProductsByCriteria
      • _findProductsAndCalculate
      • _findProductsThenCalculate
      • _getProductAttributeSet
      • _setLastVisited
    • Categories
      • _findCategoryTreeById
      • _findCategoriesByIds
      • _findCategoryByAlias
      • _findCategoryTreeByAlias
      • _getCategoryContent
    • Collections
      • _getCollectionContent
      • _findCollectionsByIds
      • _findCollectionsByIdsThenCalculate
    • Brands
      • _getBrandContent
      • _findBrandsByIds
    • Cart
      • _addToCartMulti
      • _addToCart
      • _setCart
      • _clearCart
      • _setCartListener
      • _removeFromCart
      • _calculateCart
    • Checkout
      • _startCheckout
      • _updateCheckout
      • _completeCheckout
    • Shopping Lists
      • _getShoppingLists
      • _updateShoppingList
      • _createShoppingList
      • _deleteShoppingList
      • _getShoppingListByAlias
    • Navigation
      • _getFooterMenu
      • _getHeaderMenu
    • Users
      • _getUserById
    • Utils
      • _calculateCurrency
      • _getCurrencySymbol
      • _getCulture
      • _subscribeToNewsletter
      • _findUnitsByIds
  • Vs Code
    • Getting Started
  1. Collections

_findCollectionsByIdsThenCalculate

Example Usage of _findCollectionsByIdsThenCalculate Method#

The _findCollectionsByIdsThenCalculate method retrieves collections based on their IDs and optionally includes associated products. After fetching the collections, it calculates the pricing for the products within those collections.

Method Signature#

Parameters#

collectionIds (Array): An array of collection IDs to retrieve.
showProducts (Boolean): Indicates whether to include associated products in the response.
callback (Function): A function to handle the retrieved collections.
pricingCallback (Function, optional): A function to handle the collections with updated pricing. This is invoked if provided.

How It Works#

1.
Check Collection IDs: If collectionIds is null, the method exits early without making an API request.
2.
Build Query String: Constructs a query string for the API request:
Includes a calculate=false parameter to indicate that pricing calculation is handled separately.
Includes collectionIds joined by &collectionIds= and the expanded flag to include associated products if showProducts is true.
3.
Make API Request: Sends a GET request to the /api/collection endpoint with the constructed query string. The request includes collectionIds and showProducts parameters.
4.
Handle Response:
On successful response, the collections data is passed to the callback function.
If a pricingCallback function is provided, the method proceeds to calculate pricing for the products in each collection:
Iterates over the collections and calculates pricing for each product using _calculatePricing.
Updates the products in the collections with the calculated pricing data.
Once all collections have been processed, the pricingCallback function is called with the updated collections.

Example Usage#

Here's how to use the _findCollectionsByIdsThenCalculate method:
In this example:
collectionIds specifies the IDs of the collections to fetch.
showProducts indicates whether to include associated products.
handleCollections processes and logs the retrieved collections.
handlePricingUpdatedCollections processes and logs the collections with updated pricing.

Detailed Breakdown#

1.
Fetch Collections:
The method constructs the request URL and fetches collections from the /api/collection endpoint.
The retrieved collections are passed to the callback function.
2.
Calculate Pricing:
If pricingCallback is provided, the method calculates pricing for the products in each collection.
_calculatePricing is called for each collection’s products, updating their prices.
Once all products are processed, pricingCallback is invoked with the updated collections.
3.
Error Handling:
Ensure error handling is implemented in _calculatePricing for robust performance.
Consider adding error handling within the callback and pricingCallback functions.

Notes#

API Endpoint: Ensure that /api/collection and _calculatePricing are correctly implemented and that they handle the parameters and data as expected.
Performance: Be aware of potential performance implications when dealing with a large number of collections or products. Adjust request handling as needed.
Data Consistency: Ensure that the response data is correctly processed and that the updated collections reflect accurate pricing.
By following this guide, you can effectively use the _findCollectionsByIdsThenCalculate method to integrate collection retrieval with dynamic pricing calculations.
Previous
_findCollectionsByIds
Next
_getBrandContent
Built with