├── README.md └── shopify ├── ShopifyPlugin.php ├── fieldtypes └── Shopify_ProductFieldType.php ├── resources └── icon.svg ├── services └── ShopifyService.php ├── templates ├── _select.html └── _settings.html └── variables └── ShopifyVariable.php /README.md: -------------------------------------------------------------------------------- 1 | # Craft Shopify Plugin 2 | 3 | Craft plugin to integrate with the Shopify API 4 | 5 | ## Installation 6 | 7 | 1. Move the ```shopify``` folder into your ```craft/plugins``` directory 8 | 2. Install the plugin in the Control Panel 9 | 3. Enter your Shopify Credentials in the Plugin settings 10 | 11 | ## Usage 12 | 13 | ### craft.shopify.getProducts 14 | 15 | Retrieve all products from Shopify. You can pass in any parameters that are noted in the [products endpoint](http://docs.shopify.com/api/product#index). Example: 16 | 17 | ``` 18 | {% for product in craft.shopify.getProducts({ fields: 'title,variants', limit: 5 }) %} 19 |
20 |

{{ product.title }}

21 | 26 |
27 | {% endfor %} 28 | ``` 29 | 30 | ### craft.shopify.getProductsVariants 31 | 32 | This is just a slightly modified version of the getProducts method where the array keys are the product ID. So if you have a field named ```shopifyProduct``` which stores the Shopify product ID, you can access all the products' variants with a single API call. 33 | 34 | ``` 35 | {% set shopifyProducts = craft.shopify.getProductsVariants() %} 36 | 37 | {% for entry in craft.entries({ section: 'product' }) %} 38 |
39 |

{{ entry.title }}

40 | 41 | {% if entry.shopifyProduct and shopifyProducts[entry.shopifyProduct] %} 42 | 47 | {% endif %} 48 |
49 | {% endfor %} 50 | ``` 51 | 52 | ### craft.shopify.getProductById 53 | 54 | Useful on product show pages to access Shopify product information. Useful to get a product price or create an [add to cart form](http://docs.shopify.com/manual/configuration/store-customization/page-specific/cart-page/adding-to-the-cart-from-a-remote-website#html). This hits the [single product endpoint](http://docs.shopify.com/api/product#show). 55 | 56 | ``` 57 | {% set shopify = craft.shopify.getProductById({ id: entry.shopifyProduct, fields: 'variants' }) %} 58 | 59 |
60 | 65 | 66 | 67 |
68 | ``` -------------------------------------------------------------------------------- /shopify/ShopifyPlugin.php: -------------------------------------------------------------------------------- 1 | array(AttributeType::String, 'required' => true, 'label' => 'API Key'), 45 | 'password' => array(AttributeType::String, 'required' => true, 'label' => 'Password'), 46 | 'secret' => array(AttributeType::String, 'required' => true, 'label' => 'Secret'), 47 | 'hostname' => array(AttributeType::String, 'required' => true, 'label' => 'Hostname') 48 | ); 49 | } 50 | 51 | public function getSettingsHtml() 52 | { 53 | return craft()->templates->render('shopify/_settings', array( 54 | 'settings' => $this->getSettings() 55 | )); 56 | } 57 | } -------------------------------------------------------------------------------- /shopify/fieldtypes/Shopify_ProductFieldType.php: -------------------------------------------------------------------------------- 1 | 250); 20 | $products = craft()->shopify->getProducts($productOptions); 21 | 22 | $options = array(); 23 | foreach ($products as $product) { 24 | $options[] = array( 25 | 'label' => $product['title'], 26 | 'value' => $product['id'] 27 | ); 28 | } 29 | 30 | return craft()->templates->render('shopify/_select', array( 31 | 'name' => $name, 32 | 'value' => $value, 33 | 'options' => $options 34 | )); 35 | } 36 | } -------------------------------------------------------------------------------- /shopify/resources/icon.svg: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /shopify/services/ShopifyService.php: -------------------------------------------------------------------------------- 1 | plugins->getPlugin('shopify')->getSettings(); 18 | 19 | $this->apiKey = $settings->apiKey; 20 | $this->password = $settings->password; 21 | $this->secret = $settings->secret; 22 | $this->hostname = $settings->hostname; 23 | } 24 | 25 | /** 26 | * Get products from Shopify 27 | * 28 | * @return array Array of Shopify Products 29 | */ 30 | public function getProducts($options = array()) 31 | { 32 | $query = http_build_query($options); 33 | $url = $this->_getShopifyUrl('admin/products.json?' . $query); 34 | 35 | try { 36 | $client = new \Guzzle\Http\Client(); 37 | $request = $client->get($url); 38 | $response = $request->send(); 39 | 40 | if (!$response->isSuccessful()) { 41 | return; 42 | } 43 | 44 | $items = $response->json(); 45 | 46 | return $items['products']; 47 | } catch(\Exception $e) { 48 | return; 49 | } 50 | } 51 | 52 | /** 53 | * Get specific product from Shopify 54 | * 55 | * @param array $options Array of options: id, fields 56 | * @return array Shopify Product 57 | */ 58 | public function getProductById($options = array()) 59 | { 60 | $id = $options['id']; 61 | $fields = isset($options['fields']) ? '?fields=' . $options['fields'] : ''; 62 | $url = $this->_getShopifyUrl('admin/products/' . $id . '.json' . $fields); 63 | 64 | try { 65 | $client = new \Guzzle\Http\Client(); 66 | $request = $client->get($url); 67 | $response = $request->send(); 68 | 69 | if (!$response->isSuccessful()) { 70 | return; 71 | } 72 | 73 | $items = $response->json(); 74 | 75 | return $items['product']; 76 | } catch(\Exception $e) { 77 | return; 78 | } 79 | } 80 | 81 | /** 82 | * Get specific product from Shopify 83 | * 84 | * @param string $endpoint API endpoint 85 | * @return string Full URL to make Shopify Request 86 | */ 87 | private function _getShopifyUrl($endpoint) 88 | { 89 | return 'https://' . $this->apiKey . ':' . $this->password . '@' . $this->hostname . '/' . $endpoint; 90 | } 91 | } -------------------------------------------------------------------------------- /shopify/templates/_select.html: -------------------------------------------------------------------------------- 1 |
2 | 8 |
-------------------------------------------------------------------------------- /shopify/templates/_settings.html: -------------------------------------------------------------------------------- 1 | {% import '_includes/forms' as forms %} 2 | 3 |

In order to use this plugin, you must provide your Shopify API credentials. You can find these credentials in the Private Apps section in your Shopify Control Panel.

4 | 5 | {{ forms.textField({ 6 | label: 'API Key'|t, 7 | required: true, 8 | name: 'apiKey', 9 | value: settings.apiKey, 10 | errors: settings.getErrors('apiKey') 11 | }) }} 12 | 13 | {{ forms.textField({ 14 | label: 'Password'|t, 15 | required: true, 16 | name: 'password', 17 | value: settings.password, 18 | errors: settings.getErrors('password') 19 | }) }} 20 | 21 | {{ forms.textField({ 22 | label: 'Secret'|t, 23 | required: true, 24 | name: 'secret', 25 | value: settings.secret, 26 | errors: settings.getErrors('secret') 27 | }) }} 28 | 29 | {{ forms.textField({ 30 | label: 'Hostname'|t, 31 | required: true, 32 | name: 'hostname', 33 | value: settings.hostname, 34 | placeholder: "hostname.myshopify.com", 35 | errors: settings.getErrors('hostname') 36 | }) }} -------------------------------------------------------------------------------- /shopify/variables/ShopifyVariable.php: -------------------------------------------------------------------------------- 1 | shopify->getProducts($options); 10 | } 11 | 12 | public function getProductById($options = array()) 13 | { 14 | return craft()->shopify->getProductById($options); 15 | } 16 | 17 | public function getProductsVariants($options = array()) 18 | { 19 | $products = craft()->shopify->getProducts($options); 20 | 21 | foreach ($products as $product) { 22 | $id = $product['id']; 23 | $variants = $product['variants']; 24 | $product_prices[$id] = $variants; 25 | } 26 | 27 | return $product_prices; 28 | } 29 | } --------------------------------------------------------------------------------