In order to do this, you need to make a controller and method that will receive an AJAX request and make a call to the Shopify API.
Create click event on the product subscription button.
Click event will look like this:
let url = 'https://your_host.com/your_contoller/your_method?shop=your_shop_domain';
let postData = { product_id: "your_product_id" };
$.ajax({
type: 'post',
url: url,
data: postData,
dataType: 'json',
complete: (res) => {
console.log(res); // response from your_method
}
});
In the controller method, you must use the Shopify client as phpish/shopify or any other.
With the client using api_key, access_token of your app or private app, you can make this kind of request:
POST /admin/api/2020-10/products/PRODUCT_ID/metafields.json
{
"metafield": {
"namespace": "inventory",
"key": "warehouse",
"value": 25,
"value_type": "integer"
}
}
After you should get back the result.
But if you want to create metafields on request from your customer, it's better to wrap controller method in a queue and use the queues, because if you don't use the queues there's a chance to fall within the limit of Shopify REST API.
After create a metafield, you can get value from liquid with code from Richard.