Hello, guys.

First of all - thanks for a great job you're doing. [=

I've found myself confused a little when I was not able to delete a product with commerce_product_delete() inside hook_node_delete(). It just increase $product id by 1.
commerce_product_delete() is returning TRUE, so it works actually, but the product is not removed. I've walked thru the function's life path and didn't found anything suspicious.

But when commerce_product_delete() is used outside the hook (form submit as example) - everything is perfect.

Do I missed something?

Comments

amateescu’s picture

Status: Active » Closed (cannot reproduce)

You must have missed something :) I have tried this with two products, one that was referenced from a product display node and one that wasn't, and both worked just fine.

giorgio79’s picture

Version: 7.x-1.2 » 7.x-1.x-dev
Status: Closed (cannot reproduce) » Active

I am getting this as well.

#1 How did you try to delete a product without a display with hook_node_delete? I don't think that is possible as hook_node_delete operates on the product display deletion. :)

Here ist he code I am using


	function mymodule_node_delete($node) {
		if ($node->type == "item" AND isset($node->field_item_product["und"][0]["product_id"])) {
			commerce_product_delete($node->field_item_product["und"][0]["product_id"]);
		}
	}

PS: Even Rules fails, wow :)

{ "rules_delete_product_after_item_deletion" : {
    "LABEL" : "Delete Product after Item Deletion",
    "PLUGIN" : "reaction rule",
    "REQUIRES" : [ "rules" ],
    "ON" : [ "node_delete" ],
    "IF" : [
      { "node_is_of_type" : { "node" : [ "node" ], "type" : { "value" : { "item" : "item" } } } }
    ],
    "DO" : [
      { "entity_fetch" : {
          "USING" : {
            "type" : "commerce_product",
            "id" : [ "node:field-item-product:product-id" ]
          },
          "PROVIDE" : { "entity_fetched" : { "entity_fetched" : "Fetched entity" } }
        }
      },
      { "entity_delete" : { "data" : [ "entity-fetched" ] } }
    ]
  }
}
rszrama’s picture

Category: bug » support
Status: Active » Closed (works as designed)

My hunch is that the node relation here is incidental but not the main issue. The function commerce_product_delete() will fail if commerce_product_can_delete() returns FALSE, and the main implementation to consider is in the product reference module, commerce_product_reference_commerce_product_can_delete(). If a product is referenced by a line item (i.e. if you have ever added it to the cart), then the product cannot be deleted. You can still delete the node, and its product reference field data will be wiped just fine, but you cannot delete the product itself; you have to just disable it.

This should be covered in the documentation from a UI standpoint on drupalcommerce.org, and from an API standpoint you can look at the documentation for hook_commerce_product_can_delete() in commerce_product.api.php.

giorgio79’s picture

Thanks Ryan, much appreciated. will sniff around those functions. Re the product I tested was never added to a cart or anything.

rszrama’s picture

Hmm, ok, then I'm going to need more information to go on if you can reproduce the issue.

giorgio79’s picture

Sure, thx Ryan. Don't want to waste your time, as this issue will become obsolete once Inline Entity Form matures.

I suspect the problem is that during http://api.drupal.org/api/drupal/modules%21node%21node.api.php/function/... the node still exists in the db, and commerce product warns if it is deleted but still referenced.

So, the issue is consistent. I set up the usual stuff, a product, plus a random content type, with a product reference field in it. I hide the product ref field from the user, and I do Entity CRUD in the background with hook_nodes (inline entity form will do this but I had some styling issues with fieldsets that are still to be sorted..,).

In the code, ïtem is my display content type.

function mymodule_node_presave($node) {
		
		//replace this with inline entity form
		if ($node->type == "item") {
			//new product entity if node does not yet have one
			if (!isset($node->field_item_product["und"][0]["product_id"])) {
				$product = commerce_product_new("product");
			}
			else {
			//load existing product entity if it exists
				$product = commerce_product_load($node->field_item_product["und"][0]["product_id"]);
			}
			$product->status = 1;
			$product->uid = $node->uid;
			$product->language = $node->language;
			
			//sku is taken care of by commerce autosku
			//$product->sku = token_replace($extras['sku_pattern'], $data, array('sanitize' => FALSE));
			$product->title = $node->title;
			
			$product->commerce_price = $node->field_item_price;
			
			commerce_product_save($product);
			
			$node->field_item_product["und"][0]["product_id"] = $product->product_id;
		}
	}
	
	function mymodule_node_delete($node) {
		if ($node->type == "item" AND isset($node->field_item_product["und"][0]["product_id"])) {
			commerce_product_delete($node->field_item_product["und"][0]["product_id"]);

		}
	}

Pretty much this is it. Once deletion fails, I go to Store, and can delete it manually easily.

rszrama’s picture

I still don't get what in Commerce is preventing deletion. Is it an implementation of hook_commerce_product_can_delete() or just a warning in the Entity API?

giorgio79’s picture

I revisited the issue, and the rule works now (after deleting hook_node_delete from my module). It works with a rule, so I wont be pursuing this. :)