
If you follow the “roadmap” of ImageCMS releases, then, most likely, you have already noticed that more and more of the functionality becomes independent. This vector has become even clearer with the release of ImageCMS 4.5.2 release. Here was presented a new approach to the functionality of the basket module.
What was the work done for the basket functionality?
This module is one of the main in the store, so he paid special attention. The idea of distributing system capabilities by dividing into functional modules allows you to create flexible solutions and provide more independent APIs.
The latter should be especially interesting for integrators and developers on ImageCMS. Since previously there was no clear understanding of the available methods for working with the basket, the solution of this issue became a priority task for us. We encapsulated internal methods, and all important methods, on the contrary, left it on the surface. Moreover, they were combined into an API. All of them exchange information in JSON-format and are described in the documentation.
')
At the design stage, we are faced with the issue of backward compatibility. Since the new approach had a completely new implementation, we had to make an explicit pointer to the methods used. To use the old approach, insert in config.php the attribute
$config['use_deprecated_cart_methods'] = TRUE;
for the new approach, the value is
FALSE
, or remove the directive from the file.
Where to see the API, as well as its documentation and implementation
All available methods for working with the basket are described in the specially created section of the documentation:
http://docs.imagecms.net/api-docs Classes Cart\Api

Here you can find a brief description of the method, as well as the necessary input data. This is the first such kind of documentation in ImageCMS, so the plans are to add more description and continue the development of this section.
Example
Add item to cart
The following example adds a product to the cart. For this we use the
addProductByVariantId
method.

It can also be used to add product to the cart. The easiest way to do this is to use the AJAX request, but the variations can be different, for every taste.
<a class=”buy” data-variantId=”198” href=”#”></a> $('.buy').on('click', function() { var variantId = $(this).attr('data-variantId'); $.ajax({ url: '/shop/cart/api/addProductByVariantId/'+ variantId, dataType: 'json', success: function() { } }); })
This simple example demonstrates the uncomplicated ability to add a product to the cart. Summing up this example, we add that it all came down to sending a request:
/shop/cart/api/addProductByVariantId/198
to the server. After that, the product with
ID 19
8, (or rather the product version) will be added to the cart for the current user.
Number of items in the cart
The space method is responsible for the number of products added by the user to the cart:
Cart/Api getTotalItemsCount
. The request to the server is as follows:
/shop/cart/api/getTotalItemsCount
. But, imagine that this information needs to be displayed in a template or for the purposes of your module.
The method is accessed via the namespace path:
\Cart\Api
. The first method in the “
create()
” chain which will prohibit the creation of several similar API instances is required.
To business:
\Cart\Api::create()->getTotalItemsCount()
We get:
{"success":true,"errors":false,"data":1}
Using the function for working with the JSON json_decode format, we can present the obtained data as an object:
$result = json_decode(\Cart\Api::create()->getTotalItemsCount()); $count = $result->data;
What do we plan next?
This approach has already proved itself to be a good one, and we do not plan to stop there. Soon we are waiting for additions to the already published
Cart::API
, as well as an API for products, categories and discounts. All developed solutions will be presented in the appropriate section of the documentation.
In addition, you can save the local version of this documentation by downloading the developer version of
http://www.imagecms.net/developers . Documentation will be available at
site_name.loc / docsFollow our updates; in case of questions - ask them in the comments.