How ALEF Bookstores Scaled For 34,000+ Products

Galal Aly fromย Robustastudioย explains how WooCommerce enabled ALEF Bookstores, one of Egyptโ€™s fastest growing bookstores, to scale in order to solve tracking challenges and take their blooming business to even greater heights.


ALEF Bookstoresย hadย embracedย eCommerceย forย expansion but faced problems with scalabilityย that threatened to slow them down. Theyย needed to be able to track the many thousands ofย books within their inventory โ€“ whether available in their main warehouse or at branches โ€“ but due to many of the books having the sameย identifiers, theyย were oftenย untraceable.

Screenshot of ALEF Bookstores homepage.

Some background

โ†‘ Revenir en haut

ALEF Bookstores were using an ERP system,ย hosting over 34,000 products, built on top of the famous Opentaps (an open source ERP system).ย The main requirement was to use the ERP system as the singleย interface forย entering products and the commerce website therefore had to synchronize both data.

The synchronization model at first was designed to synchronize all data four times a day soย that the productsโ€™ data appear the same on the website as on the system. The ERP was configured to export all of the updated productโ€™s data since last export in anย XML file. Initially, this contained all products.

WordPress and WooCommerce

โ†‘ Revenir en haut

The website was created using WordPress and WooCommerce. The synchronization partย was initially created as a WordPress plugin that parses the XML and uses the WordPressย functions to insert the product and update its data; wp_insert_post and update_post_meta. Products inserted had categories, tags, prices, descriptions, photos, weights andย dimensions, and other custom attributes like Author of the book and so on.

For WooCommerce functionality, we used the get_product function to be able to apply productโ€™s functions on the inserted product (e.g. setting stock). The synchronization plugin was written at first to handle all 34,000+ย products which took 18 hours at first.

Improving durationย and solving memory problems

โ†‘ Revenir en haut

This duration was reduced to one third by turning off theย term counting. This was done using the wp_defer_term_counting function before starting toย insert the products in the database (for new products) and turning it on afterwards. Afterย thatโ€™s done, the 34,000+ all details synchronization took six hours.

A problem was raised. The server sometimes ran out of memory when we were processingย 34000 products at once. Therefore, the XML was divided to smaller XMLs (100 productsย each). This solved the memory problem a little.

However, six hours was not an acceptable rate. We had to optimize the logic of theย synchronization itself. To identify the products, we had to use a unique identifier that wasย common on the ERP system and the website. We used the ID of the ERP system and savedย it as a meta value in our database.

This ID was used to detect whether we already had theย product in our database or not. Forย a new product for the website, we inserted it and got theย post ID to use it afterwards. For an existing product, we got its post ID directly. This wasย typically executed using the query:

$args = array(
โ€˜posts_per_pageโ€™ => 1,
โ€˜post_typeโ€™ => โ€˜productโ€™,
โ€˜post_statusโ€™ => array( โ€˜publishโ€™, โ€˜trashโ€™, โ€˜draftโ€™, โ€˜pendingโ€™ ),
โ€˜meta_queryโ€™ => array(
array(
โ€˜keyโ€™ => โ€˜_spin_idโ€™,
โ€˜valueโ€™ => (string) trim($p->SpinId),
) )
);
$posts = get_posts( $args );
view raw alef1 hosted with โค by GitHub

This is a very heavy query to execute out of the box with no modifications to the databaseย itself. We had to index the meta_value column reduce time taken and. in addition, all the insertions and updates were done in batchesย (beforeย using any of the insertions and updates we turned the AUTOCOMMIT off, and committedย every 10,000 products, and when done, turned the AUTOCOMMIT on again),

By doing so, the synchronization was halved to take onlyย three hours โ€“ an acceptable duration for us that we havenโ€™t optimized furtherย since we need toย keep the WordPress and WooCommerce usage in our code.

The 34,000+ products synchronization case had to be handled forย bulk changes made on the ERP system for all of the products (even if rare). This scenario is run only once per day so as to avoid overloading the serverย and because theย productsโ€™ data (other than quantities) are not changed frequentlyย andย can wait till the next day to be applied without any problems to the eCommerce process.

Stock quantities

โ†‘ Revenir en haut

The one attribute that is being constantly updated is the stock quantity. The ERP systemย is being used in all ALEF Bookstores branches and when an offline sale happens, the stockย changes in the ERP. This neededย be reflected on the website to avoid overselling of anyย of the products.

Unfortunately, we couldnโ€™t get an exported XML from the ERP system with the updatedย attributes only. We get all of the productโ€™s details if the product had been updated since lastย synchronisation. So, another synchronization scenario had to be created to update theย quantities.

First, we exported XMLs from the ERP system, and for each product, set its stockย using WooCommerce functions. This took approximately the same time (three hours) asย the all information synchronization described above andย wasnโ€™tย acceptable.

We had to further optimize the synchronization script. The ERP system was configured toย export the quantities of the products along with the unique identifier to a MySQL table in the database. When done, a simple join MySQL query gotย the product IDs of theย changed quantities and the new quantities values. This join was saved as a view andย contained the website quantity, ERP quantity, and product ID.

Aย simple loop to initialize a WooCommerce product from the IDs and set the stock toย the new value completed the task butย the time taken when this synchronization was run was still a full hour.ย Better but stillย not acceptable because the intention was to run the quantities update hourly.

The WooCommerce set stock function for a product made unnecessary checks (for ourย case): the back-ordering and that the stock is being managed on the website. Therefore, aย copy of this function was created that omits these checks, and turned off theย AUTOCOMMIT while updating. These last changes reduced the time required to update theย quantities for the 34,000+ products to under 2 minutes.


Moral of the story? What takes 18 hours in a first attempt can be sliced down to just two minutes. Maximum respect to the Robustastudio team and to ALEF Bookstores for their journey so far and where theyโ€™re headed.

Visit the Woo Marketplace for all the tools you need.
Marina Pape Avatar

About

On this page

2 comments

  1. Rinaldo Pavanello
    juillet 17, 2014

    I would love to know what the web server infrastructure that this store uses?
    Do you have this information?

    Thanks

  2. Robin
    juillet 19, 2014

    Great case study.
    Thanks for sharing.

Never miss a beat โ€” join our mailing list

View our privacy policy. You can unsubscribe anytime.

Subscribing...

There was an error subscribing; please try again later.

Thanks for subscribing!
Emails will be sent to

You're already subscribed!
Emails are sent to

  • Assistance des รฉquipes partout dans le monde