OrderBillingAddress.php in Commerce Core 8.2
File
modules/order/src/Plugin/Commerce/Condition/OrderBillingAddress.php
View source
<?php
namespace Drupal\commerce_order\Plugin\Commerce\Condition;
use CommerceGuys\Addressing\Zone\Zone;
use Drupal\commerce\Plugin\Commerce\Condition\ConditionBase;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Form\FormStateInterface;
class OrderBillingAddress extends ConditionBase {
public function defaultConfiguration() {
return [
'zone' => NULL,
] + parent::defaultConfiguration();
}
public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
$form = parent::buildConfigurationForm($form, $form_state);
$form['zone'] = [
'#type' => 'address_zone',
'#default_value' => $this->configuration['zone'],
'#required' => TRUE,
];
return $form;
}
public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
parent::submitConfigurationForm($form, $form_state);
$values = $form_state
->getValue($form['#parents']);
foreach ($values['zone']['territories'] as &$territory) {
unset($territory['remove']);
}
unset($values['zone']['label']);
$this->configuration['zone'] = $values['zone'];
}
public function evaluate(EntityInterface $entity) {
$this
->assertEntity($entity);
$order = $entity;
$billing_profile = $order
->getBillingProfile();
if (!$billing_profile) {
return FALSE;
}
$zone = new Zone([
'id' => 'billing',
'label' => 'N/A',
] + $this->configuration['zone']);
$address = $billing_profile
->get('address')
->first();
return $zone
->match($address);
}
}