diff --git a/commerce_pricelist.services.yml b/commerce_pricelist.services.yml index c8da8c1..32d619d 100644 --- a/commerce_pricelist.services.yml +++ b/commerce_pricelist.services.yml @@ -1,6 +1,7 @@ services: commerce_pricelist.default_base_price_resolver: class: Drupal\commerce_pricelist\Resolver\PriceListDefaultBasePriceResolver + arguments: ['@request_stack'] tags: - - { name: commerce_pricing.base_price_resolver, priority: 0 } + - { name: commerce_price.base_price_resolver, priority: 600 } diff --git a/config/optional/field.field.commerce_product_bundle.default.field_price_list_item.yml b/config/optional/field.field.commerce_product_bundle.default.field_price_list_item.yml index dc9ed5c..9051200 100644 --- a/config/optional/field.field.commerce_product_bundle.default.field_price_list_item.yml +++ b/config/optional/field.field.commerce_product_bundle.default.field_price_list_item.yml @@ -12,7 +12,7 @@ bundle: default label: 'Price List Item' description: '' required: false -translatable: false +translatable: true default_value: { } default_value_callback: '' settings: diff --git a/config/optional/field.storage.commerce_product_bundle.field_price_list_item.yml b/config/optional/field.storage.commerce_product_bundle.field_price_list_item.yml index a940c65..d1ed478 100644 --- a/config/optional/field.storage.commerce_product_bundle.field_price_list_item.yml +++ b/config/optional/field.storage.commerce_product_bundle.field_price_list_item.yml @@ -11,7 +11,7 @@ type: entity_reference settings: target_type: price_list_item module: core -locked: false +locked: true cardinality: -1 translatable: true indexes: { } diff --git a/config/optional/field.storage.commerce_product_variation.field_price_list_item.yml b/config/optional/field.storage.commerce_product_variation.field_price_list_item.yml index 4cf2201..2536c77 100644 --- a/config/optional/field.storage.commerce_product_variation.field_price_list_item.yml +++ b/config/optional/field.storage.commerce_product_variation.field_price_list_item.yml @@ -11,7 +11,7 @@ type: entity_reference settings: target_type: price_list_item module: core -locked: false +locked: true cardinality: -1 translatable: true indexes: { } diff --git a/src/Entity/PriceList.php b/src/Entity/PriceList.php index 0ea849e..83232ec 100644 --- a/src/Entity/PriceList.php +++ b/src/Entity/PriceList.php @@ -174,6 +174,7 @@ class PriceList extends CommerceContentEntityBase implements PriceListInterface $fields['name'] = BaseFieldDefinition::create('string') ->setLabel(t('Name')) ->setDescription(t('The name of the Price list entity.')) + ->setRequired(TRUE) ->setSettings(array( 'max_length' => 50, 'text_processing' => 0, diff --git a/src/Form/PriceListItemInlineForm.php b/src/Form/PriceListItemInlineForm.php index 2b40ec4..f1ba738 100644 --- a/src/Form/PriceListItemInlineForm.php +++ b/src/Form/PriceListItemInlineForm.php @@ -3,6 +3,7 @@ namespace Drupal\commerce_pricelist\Form; use Drupal\commerce_price\Price; +use Drupal\Component\Utility\NestedArray; use Drupal\Core\Form\FormStateInterface; use Drupal\Core\Entity\EntityInterface; use Drupal\inline_entity_form\Form\EntityInlineForm; @@ -59,6 +60,13 @@ class PriceListItemInlineForm extends EntityInlineForm { */ public function entityForm(array $entity_form, FormStateInterface $form_state) { $entity_form = parent::entityForm($entity_form, $form_state); + $entity_form['purchased_entity']['widget'][0]['target_id']['#ajax'] = [ + 'callback' => [get_class($this), 'purchasedRefresh'], + 'event' => 'autocompleteclose', + 'wrapper' => 'purchased_entity_refresh', + ]; + $entity_form = $this->priceForm($entity_form, $form_state); + $entity_form['price']['#attributes']['id'] = 'purchased_entity_refresh'; $routeName = \Drupal::routeMatch()->getRouteName(); switch ($routeName) { case 'entity.price_list.add_page':unset($entity_form['price_list_id']);break; @@ -70,10 +78,58 @@ class PriceListItemInlineForm extends EntityInlineForm { case 'entity.commerce_product_variation.add_form':unset($entity_form['purchased_entity']);break; case 'entity.commerce_product_variation.edit_form':unset($entity_form['purchased_entity']);break; } + return $entity_form; + } + /** + * @param array $entity_form + * @param FormStateInterface $form_state + * @return array + * @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException + */ + public function priceForm(array $entity_form, FormStateInterface $form_state) + { + $entity = $entity_form['#entity']; + $entity_form['price']['#disabled'] = false; + $target_id = null; + $target_type = $entity_form['purchased_entity']['widget'][0]['target_id']['#target_type']; + if ($entity->hasPurchasedEntity()) { + $target_id = $entity->getPurchasedEntityId(); + } else { + $values = $form_state->getValues(); + $inline_entity_form = $values['field_price_list_item']['form']['inline_entity_form']; + if ($inline_entity_form) $target_id = $inline_entity_form['purchased_entity'][0]['target_id']; + } + if ($target_id) { + $entity_storage = \Drupal::entityManager()->getStorage($target_type); + $entity_service = \Drupal::service('commerce_pricelist.default_base_price_resolver'); + $price = $entity_service->getPrice($entity_storage->load($target_id)); + if ($price->getNumber() == '0.00') { + $entity_form['price']['#disabled'] = true; + } + } return $entity_form; } + /** + * @param array $form + * @param FormStateInterface $form_state + * @return mixed + */ + public function purchasedRefresh(array $form, FormStateInterface $form_state) + { + $element = []; + $triggering_element = $form_state->getTriggeringElement(); + + // Remove the action and the actions container. + $array_parents = array_slice($triggering_element['#array_parents'], 0, -2); + while (!(isset($element['#type']) && ($element['#type'] == 'inline_entity_form'))) { + $element = NestedArray::getValue($form, $array_parents); + array_pop($array_parents); + } + return $element['price']; + } + /** * {@inheritdoc} */ @@ -94,15 +150,17 @@ class PriceListItemInlineForm extends EntityInlineForm { // set price if price is null if ($product && !$entity->getPrice()) { - if ($product->getPrice()) { - $entity->setPrice($product->getPrice()); + $entity_service = \Drupal::service('commerce_pricelist.default_base_price_resolver'); + $price = $entity_service->getPrice($product); + if ($price->getNumber() != '0.00') { + $entity->setPrice($price); } } $entity->save(); $entity_id = $entity->id(); - if ($product) { + if ($product && $product->field_price_list_item) { $target_id = []; $field_price_list_item = $product->field_price_list_item->getValue(); foreach ($field_price_list_item as $item) { @@ -114,7 +172,7 @@ class PriceListItemInlineForm extends EntityInlineForm { } } - if ($priceList) { + if ($priceList && $priceList->field_price_list_item) { $target_id = []; $field_price_list_item = $priceList->field_price_list_item->getValue(); foreach ($field_price_list_item as $item) { diff --git a/src/Resolver/PriceListBasePriceResolverInterface.php b/src/Resolver/PriceListBasePriceResolverInterface.php deleted file mode 100644 index d862d9f..0000000 --- a/src/Resolver/PriceListBasePriceResolverInterface.php +++ /dev/null @@ -1,15 +0,0 @@ -applies($entity) ? $entity->price->first() : NULL; + public function resolve(PurchasableEntityInterface $entity, $quantity, Context $context) { + // Make sure that product variation has a field called Saleprice. + if (!$entity->hasField('bundle_price')) { + return; + } + + if ($entity->get('bundle_price')->isEmpty()) { + return; + } + + /** @var \Drupal\commerce_price\Price $sale_price */ + $sale_price = $entity->get('bundle_price')->first()->toPrice(); + $sale_price_number = $sale_price->getNumber(); + $sale_price_currency_code = $sale_price->getCurrencyCode(); + + if (!$sale_price_number || $sale_price_number == 0) { + return; + } + + return new Price($sale_price_number, $sale_price_currency_code); } /** - * Determines whether the resolver applies to the given purchasable entity. - * - * @param \Drupal\commerce\PurchasableEntityInterface $entity - * The purchasable entity. - * + * @param PurchasableEntityInterface $entity * @return bool - * TRUE if the resolver applies to the given purchasable entity, FALSE - * otherwise. + * @throws \Drupal\Core\TypedData\Exception\MissingDataException */ - public function applies(PurchasableEntityInterface $entity) { - return $entity->hasField('price') && !$entity->get('price')->isEmpty(); + public function getPrice(PurchasableEntityInterface $entity) { + $type_id = $entity->getEntityType()->id(); + $currency_code = \Drupal::service('commerce_store.current_store')->getStore()->getDefaultCurrencyCode(); + $price = new Price('0.00', $currency_code); + if ($type_id == 'commerce_product_bundle') { + if (!$entity->get('bundle_price')->isEmpty() && !$entity->get('bundle_price')->first()->toPrice()->isZero()) { + $price = $entity->get('bundle_price')->first()->toPrice(); + } + } else { + if (!$entity->getPrice()->isZero()) { + $price = $entity->getPrice(); + } + } + return $price; } } diff --git a/src/Resolver/PriceListPriceResolverInterface.php b/src/Resolver/PriceListPriceResolverInterface.php new file mode 100644 index 0000000..9ab1ce0 --- /dev/null +++ b/src/Resolver/PriceListPriceResolverInterface.php @@ -0,0 +1,12 @@ +