Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zakhardage/83f73f89e48ffb6d4be6225961d55c2e to your computer and use it in GitHub Desktop.
Save zakhardage/83f73f89e48ffb6d4be6225961d55c2e to your computer and use it in GitHub Desktop.
<!doctype html>
<html>
<head>
<meta charset="utf-8" />
<title>Buy button / cart permalink</title>
<style>
img {max-width:200px;}
</style>
</head>
<body>
<?php
// Get helper functions
require_once("functions.php");
// Set variables for our request
require_once("credentials.php");
$query = array("Content-type" => "application/json"); // Tell Shopify that we're expecting a response in JSON format
$productID = '6614972399814';
$endpoint = 'products/' . $productID . '.json';
$get_product = shopify_call($token, $shop, $endpoint, $query, 'GET');
$get_product_response = $get_product["response"];
$get_product_response_json = json_decode($get_product["response"], TRUE);
$product = $get_product_response_json["product"];
// Images
$images = $product["images"];
$featured_image = $images[0]["src"];
// Variants
$variants = $product["variants"];
$number_variants = sizeof($variants);
echo '<h2>' . $product["title"] . '</h2>';
echo '<img src="' . $featured_image . '" alt="" />';
echo '<h4>Options</h4>';
for ($x = 0; $x < $number_variants; $x++) {
if($x==0) {
echo '<input type="radio" name="option" value="' . $variants[$x]["id"] . '" checked /> ' . $variants[$x]["title"] . '<br />';
} else {
echo '<input type="radio" name="option" value="' . $variants[$x]["id"] . '" /> ' . $variants[$x]["title"] . '<br />';
}
}
echo '<p><a href="https://store-name.myshopify.com/cart/' . $variants[0]["id"] . ':1" target="_blank">Buy this item</a></p>';
?>
</body>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<script>
$('input').click(function() {
var variantID = $(this).val();
console.log(variantID);
var permalink = 'https://store-name.myshopify.com/cart/' + variantID + ':1';
$('a').attr('href',permalink);
});
</script>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment