Sharing specific post types between sites


I’m working with woocommerce and we would like to be able to share coupons between sites on a multisite network.

So for example you have a coupon that gives you a $5 off that’s valid at both Shop A and Shop B but can only be used once, when you redeem it at Shop A, it shouldn’t be allowed to be used at Shop B.

Woocommerce already provides all of the functionality for this in the form of “usage limits”, so now I just need to figure out how to connect the sites together.

On woocommerce, coupons are a custom post type so they’re stored in the posts table.

There are three solutions that I’m currently thinking of

  1. I can set up some sort of data syncing where changes made to Shop A would be pushed to Shop B and vice-versa. So whenever a post is changed, sync would occur. But I don’t like data duplication so this is a bad idea to me.

  2. WordPress provides switch_to_blog, so in theory before any queries are made, I could switch to the main site and then switch back to the current site. We’d basically be switching which database it’s read from.

  3. WordPress provides a REST API, and woocommerce provides endpoints for coupon related functionality. I could change coupon handling to point to a specific site on our network. CRUD operations would need to be changed to properly point to the other wordpress site but once that’s done, it should work. This approach also has the benefit that I could check for the existence of a coupon on one site, and then check for existence of a coupon on a parent site if needed, so there’s some flexibility in terms of functionality.

How do these solutions look in the context of wordpress coding: do they make sense?

Are there other solutions that I can consider when it comes to “synchronizing” posts between sites?

vancoder 4 months 2021-06-21T14:35:54-05:00 0 Answers 0 views 0

Leave an answer