TransWikia.com

Dynamic subdomains for woocommerce affiliate sales but NO multisite. Is that possible?

WordPress Development Asked on January 2, 2022

I’ve got a woocommerce installation that sells things. We want to offer affiliates to sell our products with their own url, ie mysite.thebasesite.com. Basically, the way that it works is that a visitor would hit the above url and I have written a plugin that extracts the "mysite" segment, does an API call to an API to get the affiliate information and then uses that information to customize some things in the header, set a cookie/session object later used to store affiliate information in the woocommerce order. All the above works when I hardcode the url segment into the plugins.

My problem is getting wordpress to maintain the subdomain in the url throughout the session. First, if I visit mysite.thebasesite.com, it immediately forwards me to thebasesite.com. Not sure why. Second, all the urls in wordpress are absolute, which I think I can get around, but still annoying. Third, all the emails from the site (particularly orders and whatnot) will have links from thebasesite.com and not mysite.thebbasesite.com. I can likely get around this too, but I’m wondering if I’m barking up the wrong tree here.

I have no aversion to using multisite but I don’t think that will work because all the content (products, themes, plugins, content) is all the same for each site AND there could be thousands of affiliates and therefor thousands of subdomains. I’m starting to think that this is just something that is going to be impossible with woocommerce and wordpress.

One Answer

It's as possible/impossible as you have time/budget for. It seems achievable to me, given that I'm assuming you're a for-profit organisation and this is generating you revenue somehow, but you might end up hacking some Wordpress internals at some point.

It's a broad and subjective question, which is not ideally suited to this stackexchange, but a couple of ideas below which are not 'solutions' just things worth considering.

1 - Use URL rewriting to make the subdomain a parameter passed to a single domain

Using e.g. the below mod_rewrite rules, taken from [here][1], you could perhaps solve some of your problem by getting the subdomain into a parameter you can easily work with in code.

The below htaccess (or something like it) would assume that you actually only have a single website at e.g. domain.com and a visit to affiliate123.example.com/foo/bar.php would actually appear to the server as example.com/foo/bar.php?sub=affiliate123.

If you make sure that all the URLs on your site are rendered relative to the domain (i.e. your URLs are '/foo/bar/baz.jpg' rather than 'https://www.example.com/foo/bar/baz.jpg') then the browser will consider them to be URLs in the current domain and you have a lot less stuff to worry about.

You'd then need to do some coding in order to be able to map this parameter in your URL that gives you the subdomain to lookup theme components, etc.

This probably has various issues that you'd have to figure out, like what happens if a plugin messes with your sub URL parameter or some other URL construction corner cases.

RewriteEngine on
RewriteCond %{QUERY_STRING} !^sub=
RewriteCond %{HTTP_HOST} ^(?!www.)(.+).domain.com$
RewriteRule ^(/?)$ $1?sub=%1

2 - What do you do when the affiliate wants to put some content on their page

Occurs to me that it's quite possible that an affiliate or someone in your organisation might have the requirement where they want some content specific to one of the affiliates to be posted only on that affiliate's page. I'd suggest you either allow for that now or make it clear that it's going to be impossible. If this is a possible future requirement, it makes Multisite a potentially much better option because of the ability to allow each affiliate to manage their own content.

3 Management tools

Seems like you definitely need to look into management tools that are going to let you manage a large number of similar sites from e.g. a centralised 'control panel' or set of scripts that you write. There are tools with plugins out there that let you manage many sites from a single dashboard. This could meet a bunch of your requirements. (This also mentioned by @TonyDjukic while I was drafting this answer)

Hope some of that helps you figure out which way to go. [1]: https://stackoverflow.com/questions/42263265/how-to-rewrite-subdomain-and-subsubdomain-into-a-parameter

Answered by mozboz on January 2, 2022

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP