Skip to content

Instantly share code, notes, and snippets.

@agoradesign
Created September 10, 2019 07:52
Show Gist options
  • Save agoradesign/4ff9eb64c784b7532bc418d59618b5d9 to your computer and use it in GitHub Desktop.
Save agoradesign/4ff9eb64c784b7532bc418d59618b5d9 to your computer and use it in GitHub Desktop.
commerce_discounted_product: example implementation for categories
<?php
namespace Drupal\fp_shop\Applicability;
use Drupal\commerce_discounted_product\Applicability\ApplicabilityCheckerInterface;
use Drupal\commerce_promotion\Entity\PromotionInterface;
use Drupal\commerce_promotion\Plugin\Commerce\PromotionOffer\OrderItemPromotionOfferInterface;
use Drupal\fp_shop\Commerce\CategoryRepositoryInterface;
use Drupal\fp_shop\Commerce\ProductRepositoryInterface;
/**
* Applicability checker implementation for 'order_item_category' condition.
*/
class ProductCategoryApplicabilityChecker implements ApplicabilityCheckerInterface {
/**
* The category repository.
*
* @var \Drupal\fp_shop\Commerce\CategoryRepositoryInterface
*/
protected $categoryRepository;
/**
* The product repository.
*
* @var \Drupal\fp_shop\Commerce\ProductRepositoryInterface
*/
protected $productRepository;
/**
* Constructs a new ProductCategoryApplicabilityChecker object.
*
* @var \Drupal\fp_shop\Commerce\CategoryRepositoryInterface $category_repository
* The category repository.
* @var \Drupal\fp_shop\Commerce\ProductRepositoryInterface $product_repository
* The product repository.
*/
public function __construct(CategoryRepositoryInterface $category_repository, ProductRepositoryInterface $product_repository) {
$this->categoryRepository = $category_repository;
$this->productRepository = $product_repository;
}
/**
* {@inheritdoc}
*/
public function applies(PromotionInterface $promotion) {
$offer = $promotion->getOffer();
if (empty($offer) || !($offer instanceof OrderItemPromotionOfferInterface)) {
return FALSE;
}
foreach ($offer->getConditions() as $condition) {
if ($condition->getPluginId() == 'order_item_category') {
return TRUE;
}
}
return FALSE;
}
/**
* {@inheritdoc}
*/
public function determineAffectedProductIds(PromotionInterface $promotion) {
$results = [];
$offer = $promotion->getOffer();
if (empty($offer)) {
return $results;
}
foreach ($offer->getConditions() as $condition) {
if ($condition->getPluginId() != 'order_item_category') {
continue;
}
$configuration = $condition->getConfiguration();
$category_ids = $configuration['categories'];
$children = [];
foreach ($category_ids as $category_id) {
$children = array_merge($children, $this->categoryRepository->getChildCategoryIds($category_id));
}
$category_ids = array_merge($category_ids, $children);
$single_result = [];
foreach ($category_ids as $category_id) {
$single_result = array_merge($single_result, $this->productRepository->getProductIdsByCategory($category_id, NULL, TRUE));
}
$results[] = $single_result;
}
$product_ids = [];
foreach ($results as $result) {
if ($promotion->getConditionOperator() === 'OR') {
$product_ids = array_merge($product_ids, $result);
}
else {
if (empty($result)) {
return [];
}
$product_ids = !empty($product_ids) ? array_intersect($product_ids, $result) : $result;
if (empty($product_ids)) {
return [];
}
}
}
return $product_ids;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment