View source
<?php
namespace Drupal\commerce_product;
use Drupal\commerce\CommerceContentEntityStorage;
use Drupal\commerce_product\Entity\ProductInterface;
use Drupal\commerce_product\Event\FilterVariationsEvent;
use Drupal\commerce_product\Event\ProductEvents;
use Drupal\Core\Cache\CacheBackendInterface;
use Drupal\Core\Cache\MemoryCache\MemoryCacheInterface;
use Drupal\Core\Database\Connection;
use Drupal\Core\Entity\EntityFieldManagerInterface;
use Drupal\Core\Entity\EntityTypeBundleInfoInterface;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Language\LanguageManagerInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\HttpFoundation\RequestStack;
class ProductVariationStorage extends CommerceContentEntityStorage implements ProductVariationStorageInterface {
protected $requestStack;
public function __construct(EntityTypeInterface $entity_type, Connection $database, EntityFieldManagerInterface $entity_field_manager, CacheBackendInterface $cache, LanguageManagerInterface $language_manager, MemoryCacheInterface $memory_cache, EntityTypeBundleInfoInterface $entity_type_bundle_info, EntityTypeManagerInterface $entity_type_manager, EventDispatcherInterface $event_dispatcher, RequestStack $request_stack) {
parent::__construct($entity_type, $database, $entity_field_manager, $cache, $language_manager, $memory_cache, $entity_type_bundle_info, $entity_type_manager, $event_dispatcher);
$this->requestStack = $request_stack;
}
public static function createInstance(ContainerInterface $container, EntityTypeInterface $entity_type) {
return new static($entity_type, $container
->get('database'), $container
->get('entity_field.manager'), $container
->get('cache.entity'), $container
->get('language_manager'), $container
->get('entity.memory_cache'), $container
->get('entity_type.bundle.info'), $container
->get('entity_type.manager'), $container
->get('event_dispatcher'), $container
->get('request_stack'));
}
public function loadBySku($sku) {
$variations = $this
->loadByProperties([
'sku' => $sku,
]);
$variation = reset($variations);
return $variation ?: NULL;
}
public function loadFromContext(ProductInterface $product) {
$current_request = $this->requestStack
->getCurrentRequest();
if ($variation_id = $current_request->query
->get('v')) {
if (in_array($variation_id, $product
->getVariationIds())) {
$variation = $this
->load($variation_id);
if ($variation
->isPublished() && $variation
->access('view')) {
return $variation;
}
}
}
return $product
->getDefaultVariation();
}
public function loadEnabled(ProductInterface $product) {
$ids = [];
foreach ($product->variations as $variation) {
$ids[$variation->target_id] = $variation->target_id;
}
$query = $this
->getQuery()
->accessCheck(TRUE)
->addTag('entity_access')
->condition('status', TRUE)
->condition('variation_id', $ids, 'IN');
$result = $query
->execute();
if (empty($result)) {
return [];
}
$result = array_intersect_key($ids, $result);
$enabled_variations = $this
->loadMultiple($result);
$event = new FilterVariationsEvent($product, $enabled_variations);
$this->eventDispatcher
->dispatch(ProductEvents::FILTER_VARIATIONS, $event);
$enabled_variations = $event
->getVariations();
foreach ($enabled_variations as $variation_id => $enabled_variation) {
if (!$enabled_variation
->access('view')) {
unset($enabled_variations[$variation_id]);
}
}
return $enabled_variations;
}
}