DemoCouponCallout.php in Commerce Demo 8
File
src/Plugin/Commerce/CheckoutPane/DemoCouponCallout.php
View source
<?php
namespace Drupal\commerce_demo\Plugin\Commerce\CheckoutPane;
use Drupal\commerce_checkout\Plugin\Commerce\CheckoutPane\CheckoutPaneBase;
use Drupal\commerce_checkout\Plugin\Commerce\CheckoutPane\CheckoutPaneInterface;
use Drupal\commerce_promotion\Entity\CouponInterface;
use Drupal\Core\Form\FormStateInterface;
class DemoCouponCallout extends CheckoutPaneBase implements CheckoutPaneInterface {
public function defaultConfiguration() {
return [
'weight' => 50,
] + parent::defaultConfiguration();
}
public function buildPaneForm(array $pane_form, FormStateInterface $form_state, array &$complete_form) {
$build = [
'#type' => 'container',
'#attributes' => [
'class' => [
'well',
],
],
];
$build['heading'] = [
'#type' => 'inline_template',
'#template' => '<h4 class="text-muted"><strong>{{ title }}</strong></h4>',
'#context' => [
'title' => $this
->t('Try out a coupon in our demo!'),
],
];
$build['coupon_codes'] = [
'#theme' => 'item_list',
'#items' => [],
'#attributes' => [
'class' => [
'text-muted',
],
],
];
$coupon_storage = $this->entityTypeManager
->getStorage('commerce_promotion_coupon');
$coupons = array_filter($coupon_storage
->loadMultiple(), function (CouponInterface $coupon) {
return $coupon
->available($this->order) && $coupon
->getPromotion()
->applies($this->order);
});
$build['coupon_codes']['#items'] = array_map(function (CouponInterface $coupon) {
return $coupon
->getCode();
}, $coupons);
return $build;
}
}