Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

US-1685: Required products are not added to cart when adding a ProductVariation #88

Merged
merged 1 commit into from
Sep 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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