Skip to content

Commit

Permalink
Merge pull request #88 from iqual-ch/issue/US-1685_required_products_…
Browse files Browse the repository at this point in the history
…regression
  • Loading branch information
pvbergen committed Sep 1, 2023
2 parents bf3dc9c + 0a938c2 commit 1a76383
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@

use Drupal\commerce_product\Entity\Product;
use Drupal\commerce_product\Entity\ProductVariation;
use Drupal\commerce_product\Entity\ProductVariationInterface;
use Drupal\Core\Entity\EntityRepositoryInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\iq_commerce\Event\IqCommerceAfterCartAddEvent;
use Drupal\iq_commerce\Event\IqCommerceCartEvents;
use Drupal\iq_commerce_related_product\Form\RelatedProductSettingsForm;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Drupal\iq_commerce\Event\IqCommerceAfterCartAddEvent;

/**
* Handles related products on adding products to cart.
Expand Down Expand Up @@ -67,21 +68,20 @@ public function suggestRelatedProducts(IqCommerceAfterCartAddEvent $event) {
// Get all related field references in the product from the settings.
$iqCommerceProductSettingsConfig = RelatedProductSettingsForm::getSettings();
$related_field_names = $iqCommerceProductSettingsConfig['related_product_fields'];

/** @var \Drupal\commerce_order\Entity\OrderItem $order_item */
foreach ($order_items as $order_item) {
/** @var \Drupal\commerce_product\Entity\ProductVariation $purchased_entity */
$purchased_entity = $order_item->getPurchasedEntity();
if (!$purchased_entity) {
continue;
}
if ($purchased_entity instanceof ProductVariationInterface) {
$purchased_entity = $purchased_entity->getProduct();
}
foreach ($related_field_names as $related_field_name => $field_settings) {
if ($purchased_entity->hasField($related_field_name)) {
$related_products = $purchased_entity->get($related_field_name)->getValue();
$related_products_reference_type = $purchased_entity->get($related_field_name)->getFieldDefinition()->getTargetEntityTypeId();
}
elseif ($purchased_entity->getProduct()->hasField($related_field_name)) {
$related_products = $purchased_entity->getProduct()->get($related_field_name)->getValue();
$related_products_reference_type = $purchased_entity->getProduct()->get($related_field_name)->getFieldDefinition()->getTargetEntityTypeId();
}

if (!empty($related_products)) {
foreach ($related_products as $related_product) {
// If the reference is of type product, then load all variations for each product and suggest them.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Drupal\commerce_cart\Event\CartOrderItemUpdateEvent;
use Drupal\commerce_product\Entity\Product;
use Drupal\commerce_product\Entity\ProductVariation;
use Drupal\commerce_product\Entity\ProductVariationInterface;
use Drupal\Core\Entity\EntityRepositoryInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\iq_commerce\Event\IqCommerceAfterCartAddEvent;
Expand Down Expand Up @@ -86,11 +87,13 @@ public function addRequiredProducts(IqCommerceBeforeCartAddEvent $event) {
$storage = $this->entityTypeManager->getStorage($order_item_data['purchased_entity_type']);

$purchased_entity = $storage->load($order_item_data['purchased_entity_id']);
$purchased_entity = $this->entityRepository->getTranslationFromContext($purchased_entity);
if (!$purchased_entity || !$purchased_entity instanceof PurchasableEntityInterface) {
continue;
}
/** @var \Drupal\commerce_product\Entity\Product $purchased_entity */
$purchased_entity = $this->entityRepository->getTranslationFromContext($purchased_entity);
if ($purchased_entity instanceof ProductVariationInterface) {
$purchased_entity = $purchased_entity->getProduct();
}

// Get all required field references in the product from the settings.
$iqCommerceProductSettingsConfig = RequiredProductSettingsForm::getSettings();
Expand All @@ -102,10 +105,6 @@ public function addRequiredProducts(IqCommerceBeforeCartAddEvent $event) {
$required_products = $purchased_entity->get($required_field_name)->getValue();
$required_products_reference_type = $purchased_entity->get($required_field_name)->getFieldDefinition()->getTargetEntityTypeId();
}
elseif ($purchased_entity->hasField($required_field_name)) {
$required_products = $purchased_entity->get($required_field_name)->getValue();
$required_products_reference_type = $purchased_entity->get($required_field_name)->getFieldDefinition()->getTargetEntityTypeId();
}
if (!empty($required_products)) {
foreach ($required_products as $required_product) {
if (!empty($required_product['target_id'])) {
Expand Down

0 comments on commit 1a76383

Please sign in to comment.