Loading shopify.routing.yml +7 −0 Changes for shopify.routing.yml: 7 added lines, 0 removed lines. Original line number Diff line number Diff line Loading @@ -13,6 +13,13 @@ shopify.added-to-cart: requirements: _permission: 'access content' shopify.redirect: path: '/shopify/redirect' defaults: _controller: '\Drupal\shopify\Controller\ShopifyRedirect::handleRedirect' requirements: _permission: 'access content' shopify.admin: path: '/admin/config/system/shopify' defaults: Loading src/Controller/ShopifyRedirect.php 0 → 100644 +52 −0 Changes for src/Controller/ShopifyRedirect.php: 52 added lines, 0 removed lines. Original line number Diff line number Diff line <?php /** * @file * Contains controller for redirecting to specific products/variants/collections/tags. */ namespace Drupal\shopify\Controller; use Drupal\Core\Controller\ControllerBase; use Drupal\shopify\Entity\ShopifyProduct; use Drupal\shopify\Entity\ShopifyProductVariant; use Drupal\taxonomy\Entity\Term; use Symfony\Component\HttpFoundation\RedirectResponse; use Symfony\Component\HttpFoundation\Response; /** * Class ShopifyRedirect * * Provides a route to redirect user to a specific product/variant/collection/tag. */ class ShopifyRedirect extends ControllerBase { public function handleRedirect() { $request = \Drupal::request(); if ($request->get('variant_id')) { // We are redirecting to a specific variant page. $variant = ShopifyProductVariant::loadByVariantId($request->get('variant_id')); if ($variant instanceof ShopifyProductVariant) { return new RedirectResponse($variant->url()); } } if ($request->get('product_id')) { // We are redirecting to a product page (no variant selected). $product = ShopifyProduct::loadByProductId($request->get('product_id')); if ($product instanceof ShopifyProduct) { return new RedirectResponse($product->url()); } } if ($request->get('collection_id')) { // We are redirecting to a collection page. $collection = shopify_collection_load($request->get('collection_id')); if ($collection instanceof Term) { return new RedirectResponse($collection->url()); } } return new Response('', Response::HTTP_NOT_FOUND); } } src/Entity/ShopifyProduct.php +2 −0 Changes for src/Entity/ShopifyProduct.php: 2 added lines, 0 removed lines. Original line number Diff line number Diff line Loading @@ -226,9 +226,11 @@ class ShopifyProduct extends ContentEntityBase implements ShopifyProductInterfac */ public static function loadByVariantId($variant_id) { $variant = ShopifyProductVariant::loadByVariantId($variant_id); if ($variant instanceof ShopifyProductVariant) { $products = (array) self::loadByProperties(['variants' => $variant->id()]); return reset($products); } } /** * Load products by their properties. Loading src/Entity/ShopifyProductVariant.php +26 −0 Changes for src/Entity/ShopifyProductVariant.php: 26 added lines, 0 removed lines. Original line number Diff line number Diff line Loading @@ -155,6 +155,32 @@ class ShopifyProductVariant extends ContentEntityBase implements ShopifyProductV ->loadByProperties($props); } /** * Returns the associated parent product. * * @return \Drupal\shopify\Entity\ShopifyProduct */ public function getProduct() { return ShopifyProduct::loadByVariantId($this->variant_id->value); } /** * {@inheritdoc} */ public function url($rel = 'canonical', $options = array()) { // While self::toUrl() will throw an exception if the entity has no id, // the expected result for a URL is always a string. if ($this->id() === NULL || !$this->hasLinkTemplate($rel)) { return ''; } // URL should point to the product page with a variant_id param set. $product = $this->getProduct(); $options['query'] = ['variant_id' => $this->variant_id->value]; $uri = $product->toUrl($rel); $uri->setOptions($options); return $uri->toString(); } /** * {@inheritdoc} */ Loading src/Form/ShopifyVariantOptionsForm.php +15 −0 Changes for src/Form/ShopifyVariantOptionsForm.php: 15 added lines, 0 removed lines. Original line number Diff line number Diff line Loading @@ -35,6 +35,21 @@ class ShopifyVariantOptionsForm extends FormBase { $variant_id = \Drupal::request()->get('variant_id', FALSE); $default_options = \Drupal::request()->get('options', []); if (empty($default_options) && $variant_id) { // No options set from the query. // Set default options from the active variant. $variant = ShopifyProductVariant::loadByVariantId($variant_id); $variant_options = $variant->getFormattedOptions(); foreach ($options as $option) { foreach ($option->values as $option_value) { if (in_array($option_value, $variant_options)) { $default_options[$option->id] = $option_value; } } } } $form['options']['#tree'] = TRUE; foreach ($options as $option) { Loading Loading
shopify.routing.yml +7 −0 Changes for shopify.routing.yml: 7 added lines, 0 removed lines. Original line number Diff line number Diff line Loading @@ -13,6 +13,13 @@ shopify.added-to-cart: requirements: _permission: 'access content' shopify.redirect: path: '/shopify/redirect' defaults: _controller: '\Drupal\shopify\Controller\ShopifyRedirect::handleRedirect' requirements: _permission: 'access content' shopify.admin: path: '/admin/config/system/shopify' defaults: Loading
src/Controller/ShopifyRedirect.php 0 → 100644 +52 −0 Changes for src/Controller/ShopifyRedirect.php: 52 added lines, 0 removed lines. Original line number Diff line number Diff line <?php /** * @file * Contains controller for redirecting to specific products/variants/collections/tags. */ namespace Drupal\shopify\Controller; use Drupal\Core\Controller\ControllerBase; use Drupal\shopify\Entity\ShopifyProduct; use Drupal\shopify\Entity\ShopifyProductVariant; use Drupal\taxonomy\Entity\Term; use Symfony\Component\HttpFoundation\RedirectResponse; use Symfony\Component\HttpFoundation\Response; /** * Class ShopifyRedirect * * Provides a route to redirect user to a specific product/variant/collection/tag. */ class ShopifyRedirect extends ControllerBase { public function handleRedirect() { $request = \Drupal::request(); if ($request->get('variant_id')) { // We are redirecting to a specific variant page. $variant = ShopifyProductVariant::loadByVariantId($request->get('variant_id')); if ($variant instanceof ShopifyProductVariant) { return new RedirectResponse($variant->url()); } } if ($request->get('product_id')) { // We are redirecting to a product page (no variant selected). $product = ShopifyProduct::loadByProductId($request->get('product_id')); if ($product instanceof ShopifyProduct) { return new RedirectResponse($product->url()); } } if ($request->get('collection_id')) { // We are redirecting to a collection page. $collection = shopify_collection_load($request->get('collection_id')); if ($collection instanceof Term) { return new RedirectResponse($collection->url()); } } return new Response('', Response::HTTP_NOT_FOUND); } }
src/Entity/ShopifyProduct.php +2 −0 Changes for src/Entity/ShopifyProduct.php: 2 added lines, 0 removed lines. Original line number Diff line number Diff line Loading @@ -226,9 +226,11 @@ class ShopifyProduct extends ContentEntityBase implements ShopifyProductInterfac */ public static function loadByVariantId($variant_id) { $variant = ShopifyProductVariant::loadByVariantId($variant_id); if ($variant instanceof ShopifyProductVariant) { $products = (array) self::loadByProperties(['variants' => $variant->id()]); return reset($products); } } /** * Load products by their properties. Loading
src/Entity/ShopifyProductVariant.php +26 −0 Changes for src/Entity/ShopifyProductVariant.php: 26 added lines, 0 removed lines. Original line number Diff line number Diff line Loading @@ -155,6 +155,32 @@ class ShopifyProductVariant extends ContentEntityBase implements ShopifyProductV ->loadByProperties($props); } /** * Returns the associated parent product. * * @return \Drupal\shopify\Entity\ShopifyProduct */ public function getProduct() { return ShopifyProduct::loadByVariantId($this->variant_id->value); } /** * {@inheritdoc} */ public function url($rel = 'canonical', $options = array()) { // While self::toUrl() will throw an exception if the entity has no id, // the expected result for a URL is always a string. if ($this->id() === NULL || !$this->hasLinkTemplate($rel)) { return ''; } // URL should point to the product page with a variant_id param set. $product = $this->getProduct(); $options['query'] = ['variant_id' => $this->variant_id->value]; $uri = $product->toUrl($rel); $uri->setOptions($options); return $uri->toString(); } /** * {@inheritdoc} */ Loading
src/Form/ShopifyVariantOptionsForm.php +15 −0 Changes for src/Form/ShopifyVariantOptionsForm.php: 15 added lines, 0 removed lines. Original line number Diff line number Diff line Loading @@ -35,6 +35,21 @@ class ShopifyVariantOptionsForm extends FormBase { $variant_id = \Drupal::request()->get('variant_id', FALSE); $default_options = \Drupal::request()->get('options', []); if (empty($default_options) && $variant_id) { // No options set from the query. // Set default options from the active variant. $variant = ShopifyProductVariant::loadByVariantId($variant_id); $variant_options = $variant->getFormattedOptions(); foreach ($options as $option) { foreach ($option->values as $option_value) { if (in_array($option_value, $variant_options)) { $default_options[$option->id] = $option_value; } } } } $form['options']['#tree'] = TRUE; foreach ($options as $option) { Loading