Commit fe74f627 authored by bojanz's avatar bojanz

Merge pull request #224 from crbanman/8.x-2.x

Issue #2511252 by cbanman: Create a service for getting enabled produ…
parents 4894d15a f29359e8
services:
commerce_product.product_variation_manager:
class: Drupal\commerce_price\ProductVariationManager
arguments: ['@entity.manager', '@event_dispatcher']
<?php
/**
* @file
* Contains \Drupal\commerce_product\Event\FilterVariationsEvent.
*/
namespace Drupal\commerce_product\Event;
use Drupal\commerce_product\ProductInterface;
use Symfony\Component\EventDispatcher\Event;
class FilterVariationsEvent extends Event {
/**
* The parent product.
*
* @var \Drupal\commerce_product\ProductInterface
*/
protected $product;
/**
* The enabled variations.
*
* @var array
*/
protected $variations;
/**
* Constructs a new FilterVariationsEvent object.
*
* @param \Drupal\commerce_product\ProductInterface $product
* The product.
* @param array $variations
* The enabled variations.
*/
public function __construct(ProductInterface $product, array $variations) {
$this->product = $product;
$this->variations = $variations;
}
/**
* Sets the enabled variations.
*
* @param array $variations
*/
public function setVariations(array $variations) {
$this->variations = $variations;
}
/**
* Gets the enabled variations.
*
* @return array
*/
public function getVariations() {
return $this->variations;
}
}
<?php
/**
* @file
* Contains \Drupal\commerce_product\Event\ProductEvents.
*/
namespace Drupal\commerce_product\Event;
final class ProductEvents {
/**
* Name of the event fired when filtering variations.
*
* @Event
*
* @see \Drupal\commerce_product\Event\FilterVariationsEvent
*/
const FILTER_VARIATIONS = "commerce_product.filter_variations";
}
<?php
/**
* @file
* Contains \Drupal\commerce_product\ProductVariationManager.
*/
namespace Drupal\commerce_product;
use Drupal\commerce_product\Event\FilterVariationsEvent;
use Drupal\commerce_product\Event\ProductEvents;
use Drupal\Core\Entity\EntityManagerInterface;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
class ProductVariationManager implements ProductVariationManagerInterface {
/**
* The variation storage.
*
* @var \Drupal\Core\Entity\EntityStorageInterface
*/
protected $variationStorage;
/**
* The event dispatcher.
*
* @var \Symfony\Component\EventDispatcher\EventDispatcherInterface
*/
protected $eventDispatcher;
/**
* Constructs a ProductVariationManager object.
*
* @param \Drupal\Core\Entity\EntityManagerInterface $entityManager
* The entity manager.
* @param \Symfony\Component\EventDispatcher\EventDispatcherInterface $eventDispatcher
* The event dispatcher.
*/
public function __construct(EntityManagerInterface $entityManager, EventDispatcherInterface $eventDispatcher) {
$this->variationStorage = $entityManager->getStorage('commerce_product_variation');
$this->eventDispatcher = $eventDispatcher;
}
/**
* {@inheritdoc}
*/
public function getEnabledVariations(ProductInterface $product) {
$ids = [];
foreach($product->variations as $variation) {
$ids[] = $variation->target_id;
}
$query = \Drupal::entityQuery('commerce_product_variation')
->condition('status', TRUE)
->condition('variation_id', $ids, "IN");
$result = $query->execute();
if(empty($result)){
return [];
}
$enabledVariations = $this->variationStorage->loadMultiple($result);
$event = new FilterVariationsEvent($product, $enabledVariations);
$this->eventDispatcher->dispatch(ProductEvents::FILTER_VARIATIONS, $event);
$enabledVariations = $event->getVariations();
return $enabledVariations;
}
}
<?php
/**
* @file
* Contains \Drupal\commerce_product\ProductVariationManagerInterface.
*/
namespace Drupal\commerce_product;
/**
* Defines the interface for product variation managers.
*/
interface ProductVariationManagerInterface {
/**
* Gets a list of enabled variations for the given product.
*
* @param \Drupal\commerce_product\ProductInterface $product
* The product.
*
* @return array
* Array of enabled variations.
*/
public function getEnabledVariations(ProductInterface $product);
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment