Skip to content

Instantly share code, notes, and snippets.

@steveoliver
Last active November 5, 2016 04:55
Show Gist options
  • Save steveoliver/449a7f0425787ee99e0b53b8cccb39bc to your computer and use it in GitHub Desktop.
Save steveoliver/449a7f0425787ee99e0b53b8cccb39bc to your computer and use it in GitHub Desktop.
\Drupal\Commerce\Context
<?php
namespace Drupal\commerce_price\Resolver;
use Drupal\commerce\Context;
use Drupal\commerce\PurchasableEntityInterface;
/**
* Default implementation of the chain base price resolver.
*/
class ChainPriceResolver implements ChainPriceResolverInterface {
/**
* The resolvers.
*
* @var \Drupal\commerce_price\Resolver\PriceResolverInterface[]
*/
protected $resolvers = [];
/**
* Constructs a new ChainBasePriceResolver object.
*
* @param \Drupal\commerce_price\Resolver\PriceResolverInterface[] $resolvers
* The resolvers.
*/
public function __construct(array $resolvers = []) {
$this->resolvers = $resolvers;
}
/**
* {@inheritdoc}
*/
public function addResolver(PriceResolverInterface $resolver) {
$this->resolvers[] = $resolver;
}
/**
* {@inheritdoc}
*/
public function getResolvers() {
return $this->resolvers;
}
/**
* {@inheritdoc}
*/
public function resolve(PurchasableEntityInterface $entity, $quantity = 1) {
$time = \Drupal::service('commerce.time');
$user = \Drupal::currentUser();
$stores = $entity->getStores();
$context = new Context($user, $stores, $time);
foreach ($this->resolvers as $resolver) {
$result = $resolver->resolve($entity, $quantity, $context);
if ($result) {
return $result;
}
}
}
}
<?php
namespace Drupal\commerce;
use Drupal\commerce_store\Entity\Store;
use Drupal\profile\Entity\Profile;
class Context {
/**
* The customer profile.
*
* @var \Drupal\profile\Entity\Profile
*/
protected $customer;
/**
* The stores.
*
* @var \Drupal\commerce_store\Entity\Store[]
*/
protected $stores;
/**
* The time.
*
* @var \Drupal\commerce\TimeInterface
*/
protected $time;
/**
* Constructs a new Commerce Context object.
*
* @param \Drupal\profile\Entity\Profile $customer
* The customer profile.
*
* @param \Drupal\commerce_store\Entity\Store[] $store
* The store.
*
* @param \Drupal\commerce\TimeInterface $time
* The time.
*/
public function __construct(Profile $customer, Store[] $stores, TimeInterface $time) {
$this->customer = $customer;
$this->stores = $stores;
$this->time = $time;
}
/**
* Gets the current customer.
*
* @return \Drupal\profile\Entity\Profile $customer
*/
public function getCustomer() {
return $this->customer;
}
/**
* Gets the current store.
*
* @return \Drupal\commerce_store\Entity\Store[]
*/
public function getStores() {
return $this->stores;
}
/**
* Gets the current date.
*
* @return \Drupal\commerce\TimeInterface
*/
public function getTime() {
return $this->time;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment