Commit 66d6222f authored by bojanz's avatar bojanz

Delete the matching variations / line items when the parent product / order is deleted.

parent 42e2f24f
......@@ -100,6 +100,21 @@ class Order extends ContentEntityBase implements OrderInterface {
}
}
/**
* {@inheritdoc}
*/
public static function postDelete(EntityStorageInterface $storage, array $entities) {
// Delete the line items of a deleted order.
$lineItems = [];
foreach ($entities as $entity) {
foreach ($entity->line_items as $item) {
$lineItems[$item->target_id] = $item->entity;
}
}
$lineItemStorage = \Drupal::service('entity.manager')->getStorage('commerce_line_item');
$lineItemStorage->delete($lineItems);
}
/**
* {@inheritdoc}
*/
......
......@@ -8,6 +8,7 @@
namespace Drupal\commerce_order\Tests;
use Drupal\commerce_order\Entity\Order;
use Drupal\commerce_line_item\Entity\LineItem;
/**
* Tests the commerce_order entity forms.
......@@ -17,18 +18,41 @@ use Drupal\commerce_order\Entity\Order;
class OrderTest extends CommerceOrderTestBase {
/**
* Tests creating a Order programaticaly and through the add form.
* Tests creating an order programaticaly and through the UI.
*/
public function testCreateOrder() {
// Create a order programmaticaly.
$order = $this->createEntity('commerce_order', array(
'type' => 'order',
'mail' => $this->loggedInUser->getEmail(),
)
);
$lineItem = $this->createEntity('commerce_line_item', [
'type' => 'product_variation',
]);
$order = $this->createEntity('commerce_order', [
'type' => 'order',
'mail' => $this->loggedInUser->getEmail(),
'line_items' => [$lineItem],
]);
$orderExists = (bool) Order::load($order->id());
$this->assertTrue($orderExists, 'The new order has been created in the database.');
$this->assertEqual($order->id(), $order->getOrderNumber(), 'The order number matches the order ID');
}
/**
* Tests deleting an order programaticaly and through the UI.
*/
public function testDeleteOrder() {
$lineItem = $this->createEntity('commerce_line_item', [
'type' => 'product_variation',
]);
$order = $this->createEntity('commerce_order', [
'type' => 'order',
'mail' => $this->loggedInUser->getEmail(),
'line_items' => [$lineItem],
]);
$order->delete();
$orderExists = (bool) Order::load($order->id());
$lineItemExists = (bool) LineItem::load($lineItem->id());
$this->assertFalse($orderExists, 'The new order has been deleted from the database.');
$this->assertFalse($lineItemExists, 'The matching line item has been deleted from the database.');
}
}
......@@ -179,6 +179,21 @@ class Product extends ContentEntityBase implements ProductInterface {
return $this->set('uid', $uid);
}
/**
* {@inheritdoc}
*/
public static function postDelete(EntityStorageInterface $storage, array $entities) {
// Delete the product variations of a deleted product.
$variations = [];
foreach ($entities as $entity) {
foreach ($entity->variations as $item) {
$variations[$item->target_id] = $item->entity;
}
}
$variationStorage = \Drupal::service('entity.manager')->getStorage('commerce_product_variation');
$variationStorage->delete($variations);
}
/**
* {@inheritdoc}
*/
......
......@@ -8,6 +8,7 @@
namespace Drupal\commerce_product\Tests;
use Drupal\commerce_product\Entity\Product;
use Drupal\commerce_product\Entity\ProductVariation;
/**
* Create, view, edit, delete, and change products and product types.
......@@ -16,33 +17,26 @@ use Drupal\commerce_product\Entity\Product;
*/
class ProductTest extends CommerceProductTestBase {
/**
* Tests creating a product.
*/
function testAddCommerceProduct() {
$this->createEntity('commerce_product', [
'title' => $this->randomMachineName(),
'type' => 'product'
]);
}
/**
* Tests deleting a product.
*/
function testDeleteProduct() {
// Create a new product.
$values = [
$variation = $this->createEntity('commerce_product_variation', [
'type' => 'default',
'sku' => strtolower($this->randomMachineName()),
]);
$product = $this->createEntity('commerce_product', [
'type' => 'default',
'title' => $this->randomMachineName(),
'type' => "product"
];
$product = $this->createEntity('commerce_product', $values);
$productExists = (bool) Product::load($product->id());
$this->assertTrue($productExists, 'The new product has been created in the database.');
'variations' => [$variation],
]);
// Delete the product and verify deletion.
$product->delete();
$productExists = (bool) Product::load($product->id());
$variationExists = (bool) ProductVariation::load($variation->id());
$this->assertFalse($productExists, 'The new product has been deleted from the database.');
$this->assertFalse($variationExists, 'The matching product variation has been deleted from the database.');
}
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment