There are two records for products: Group and Dishes (see 605898-1.png), which refer to the same product variations, and if I delete the Group, the product variations will also be deleted, It causes product variations missing in the Dishes.

I think the reason is that when Group is deleted, it is not checked whether variations is being used by Dishes. If variations is being used by Dishes, then variations should not be deleted.

Comments

ywhe created an issue. See original summary.

ywhe’s picture

Issue summary: View changes
bojanz’s picture

Category: Bug report » Support request

Variations always belong to a single product. When the product is deleted, the variations are deleted.
Commerce provides no UI nor API functionality for sharing variations between products.

If you want product group functionality, then it needs to be made one level above products, just like the commerce_product_bundle module does it.

ywhe’s picture

StatusFileSize
new3.24 KB

This is my patch

bojanz’s picture

Status: Active » Fixed

Thanks! As I explained in #3, I won't be committing this to Commerce.

skyredwang’s picture

Title: Delete an entity, the quoted product variations will be deleted » When deleting product, variations are deleted even if they are referenced by different products.
Status: Fixed » Closed (won't fix)

Change the status to won't fix. Update the issue title to make more sense.

ywhe’s picture

StatusFileSize
new909 bytes

I need to finish my job.
2937918-2.patch can replace 2937918-1.patch to better solve the problem.

ywhe’s picture

StatusFileSize
new2.2 KB

Another option (2937918-3.patch) :
remove the variations before removing the entity (because variations_target_id will be removed after deleting the entity, and will not be able to retrieve other entities).
Each variation will be checked for variations, and the variation referenced by other entities will be retained and not References will be deleted.

lisastreeter’s picture

@ywhe It seems like it's taking you a lot of time/effort to make commerce products/variations work as you'd like. Perhaps you might consider making things easier on yourself by rethinking your overall information architecture. I'd suggest creating a new Content Type called "Product" with whatever fields you need for your product information in addition to an entity reference field that references the Commerce Product entity. "Group" would then be one node, and "Dishe"s would be another. As for the Commerce Product type, you can think of them as "variation groups". Each commerce product can have either a single variation or multiple variation, whichever is appropriate. For maximum flexibility, each commerce product could have just a single variation. Then your new, "node" Products could reference any combination of variations.

With this change to your information architecture, multiple node-type Products can refer to the same variations (by referencing the same "variation group" commerce products.) If you delete variations ("commerce products") from one of these "node" Products, it will not be automatically deleted from other products. I think this will achieve the flexibility you need without needing custom code.

It looks like you're relatively new to Drupal; I'm relatively new myself. One of the mistakes I made early on was thinking I needed to solve every problem by writing custom code. (I have a background in php/mysql, so that seemed better and "simpler" than trying to find "workarounds" using existing Drupal core and contrib elements.) Now I find I can do a lot more by creating custom content types (node types) and taxonomies and using contrib modules to solve problems than I originally thought was possible.

ywhe’s picture

StatusFileSize
new1.97 KB

@lisastreeter Thank you for your advice, I will seriously consider.

skyredwang’s picture

+++ b/modules/product/src/Entity/Product.php
@@ -314,6 +321,25 @@ class Product extends CommerceContentEntityBase implements ProductInterface {
+    $connection = Database::getConnection();

This database connection needs to get the connection from dependency injection, which is a pattern used by D8 and Commerce

skyredwang’s picture

I had the discussion with @bojanz on Slack. He doesn't think this use case is supported by the current Commerce 2.x. I disagreed with him, my argument is that a site builder can easily change IEF widget to allow referencing existing product variations (This is supported by Drupal itself and IEF), which Commerce has no control of. Therefore, the suit builder is facing this "bug".

Obviously, @bojanz has the final say on what patches get in. But, we have a good intention without causing problems to provide a fix to this bug. Other people who interested in this patch can use composer patch management.

freelock’s picture

+1

We have this use case. We have a limited number of actual products, but we want to market them in different ways on different landing pages.

For example, we have 3 different monthly plans - "Protection", "Vision", and "Results". We support both Drupal and WordPress on each plan -- however we charge more for WordPress. This leads to 6 different product variations.

For some campaigns, we send people to the "Vision Plan", where customers can select between Drupal or WordPress. For others, we send people to the "Drupal plans" page, with choices between Protection, Vision, and Results. This works fine now with 5 products covering the 6 variations (the 3 levels, the 2 CMSs). It adds a bunch of work to add another layer of abstraction here, if we change to 5 nodes to point to 6 different products with 6 different variations.

Thanks for maintaining the patch!

ywhe’s picture

StatusFileSize
new1.93 KB
new787 bytes

I improved the database connection.

skyredwang’s picture

+++ b/modules/product/src/Entity/Product.php
@@ -327,7 +326,7 @@
+    $connection = \Drupal::database();

This still doesn't use Depedency Injection

ywhe’s picture

I think the following method is Dependency Injection. But I have no way to complete the query on the commerce_product__variations. I can only query the contents of the commerce_product_variation table. I need help.

My code:
//$connection = \Drupal::service('entity.query')->get('commerce_product_variation'); or
$connection = \Drupal::entityTypeManager()->getStorage('commerce_product_variation')->getQuery();
$query = $connection->condition('variation_id',$target_id);
$result=$query->execute();

freelock’s picture

@ywhe looking at your patch, it looks like the PreDelete() method gets passed an EntityStorageInterface... assuming that's for Product entities, you can get a query from that (and that is dependency injection right there -- the object is injected into your method, rather than you creating/grabbing one from a global).

Something like this:

  $query = $storage->getQuery();
  $results = $query->condition('variations', $item->target_id, 'IN')
    ->execute();
  // $results should now have list of product entities that have this item variation
ywhe’s picture

StatusFileSize
new1.22 KB
new1.32 KB

@freelock Thanks for your help.

lawxen’s picture

StatusFileSize
new232.29 KB
new845 bytes
new1.35 KB
  1. Just about the issue's patch 2937918-5.patch
    1. -  public static function postDelete(EntityStorageInterface $storage, array $entities) {
      +  public static function preDelete(EntityStorageInterface $storage, array $entities) {
      

      We shouldn't change postDelete to preDelete

    2. + $entity_ids = $query->condition('variations', $item->target_id, 'IN')
      We should use '=', not 'IN'
    3. +        if (count($entity_ids) > 1) {
      +          continue;
      +        }
               $variations[$item->target_id] = $item->entity;
      
      1. We shouldn't use continue, just check whether we should give the result
      2. Because we should use postDelete, So we can just check whether the $entity_ids is empty.
  2. Idea about this issue:
    Whether this issue is a bug depends on below image:

    If commerce don't accept this issue's patch, we should open another issue to disallow above modification about variation's form display.
lawxen’s picture

StatusFileSize
new837 bytes
new598 bytes
-        // Do not delete variation if it is referenced by other entities.
+        // Don't delete variation if it's used by other products.

Change comment.

lawxen’s picture

lawxen’s picture

I think the following logic maybe right:

  1. Delete a “related existing variation”, commerce won’t delete the origin product variation.
  2. Delete the origin product, the variation must be deleted.

But the origin variation and the “related existing variation” has no difference, entity_reference can't support above vision, So

  1. This issue's patch can't be accepted
  2. Commerce should forbid the "Allow users to add existing variations" choice in issue: https://www.drupal.org/project/commerce/issues/2956095