Skip to content

Instantly share code, notes, and snippets.

function theme_scripts() {
wp_enqueue_script( 'buy-button', 'http://sdks.shopifycdn.com/buy-button/0.1.34/buybutton.min.js', array( ), '', true );
wp_enqueue_script( 'shopify-product', '/assets/js/custom.js', array('buy-button' ), '', true );
}
add_action( 'wp_enqueue_scripts', 'theme_scripts' );
var client = ShopifyBuy.buildClient({
apiKey: 'your-api-key',
domain: 'my-shop.myshopify.com',
appId: '6'
});
var ui = ShopifyBuy.UI.init(client);
var productNodes = document.querySelectorAll('[data-shopify-product]');
Array.prototype.slice.call(productNodes).forEach(function (node) {
var productId = node.dataset.shopifyProduct;
ui.createComponent('product', {
id: productId,
node: node,
});
});
<div data-shopify-product="1234567"></div>
ui.createComponent('product', {
id: productId,
options: {
product: {
iframe: false
},
cart: {
iframe: false
}
}
wp_enqueue_style( 'buy-button','http://sdks.shopifycdn.com/buy-button/0.1.34/buybutton.css' );
function shopify_shortcode ($atts) {
$a = shortcode_atts(array(
'id' => ''
), $atts);
return '<div data-shopify-product="' . $a['id'] . '"></div>';
}
add_shortcode( 'shopify_product', 'shopify_shortcode' );
function enqueue_shopify_buy() {
wp_enqueue_script('shopify-buy', 'http://sdks.shopifycdn.com/js-buy-sdk/v0/latest/shopify-buy.umd.polyfilled.min.js');
}
add_action( 'admin_enqueue_scripts', 'enqueue_shopify_buy' );
<ul id="products"></ul>
<input id="product_id" name="product_id" type="text" />