Skip to content

Commit

Permalink
tweak/clean product deletion by moving action into model from control…
Browse files Browse the repository at this point in the history
…ler; remove parent product when last child product deleted
  • Loading branch information
dleffler committed Feb 24, 2018
1 parent f15fb5c commit d12a4ac
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 26 deletions.
22 changes: 11 additions & 11 deletions framework/modules/ecommerce/controllers/storeController.php
Expand Up @@ -1495,24 +1495,24 @@ function delete() {
$product_type = $db->selectValue('product', 'product_type', 'id=' . $this->params['id']);
$product = new $product_type($this->params['id'], true, false);

// remove any associated records
$db->delete('option', 'product_id=' . $product->id . " AND optiongroup_id IN (SELECT id from " . $db->prefix . "optiongroup WHERE product_id=" . $product->id . ")");
$db->delete('optiongroup', 'product_id=' . $product->id);
$db->delete('product_storeCategories', 'product_id=' . $product->id . ' AND product_type="' . $product_type . '"');
$db->delete('crosssellItem_product', 'product_type="' . $this->product_type . '" AND (product_id=' . $this->id . ' OR crosssellItem_id=' . $this->id . ')');
// // remove any associated records
// $db->delete('option', 'product_id=' . $product->id . " AND optiongroup_id IN (SELECT id from " . $db->prefix . "optiongroup WHERE product_id=" . $product->id . ")");
// $db->delete('optiongroup', 'product_id=' . $product->id);
// $db->delete('product_storeCategories', 'product_id=' . $product->id . ' AND product_type="' . $product_type . '"');
// $db->delete('crosssellItem_product', 'product_type="' . $this->product_type . '" AND (product_id=' . $this->id . ' OR crosssellItem_id=' . $this->id . ')');

// remove any childen products
if ($product->product_type === "product") {
if ($product->hasChildren()) {
$this->deleteChildren();
}
}
// if ($product->product_type === "product") {
// if ($product->hasChildren()) {
// $this->deleteChildren();
// }
// }

// remove the product
$product->delete();

// remove search index entry
$db->delete('search', "category='Products' AND ref_module='store' AND original_id = " . $product->id);
// $db->delete('search', "category='Products' AND ref_module='store' AND original_id = " . $product->id);

flash('message', gt('Product deleted successfully.'));
expHistory::back();
Expand Down
15 changes: 14 additions & 1 deletion framework/modules/ecommerce/models/storeCategory.php
Expand Up @@ -48,8 +48,15 @@ public function beforeDelete() {
// note sub categories are removed in parent nestedNode->delete()

// delete product storeCategory connections for sub categories
$subcats = $this->getChildren();
$subcats = $this->getBranch();
foreach ($subcats as $cat) {
// first delete all the products assigned to this sub category
// $products = $db->selectObjects('product_storeCategories', 'storecategories_id=' . $cat->id);
// foreach($products as $product) {
// $product->delete();
// }

// delete product storeCategory connections
$db->delete('product_storeCategories', 'storecategories_id=' . $cat->id);
}
}
Expand All @@ -59,6 +66,12 @@ public function afterDelete() {

// note we've already deleted all sub categories in the parent nestedNode->delete() method

// first delete all the products assigned to this category
// $products = $db->selectObjects('product_storeCategories', 'storecategories_id=' . $this->id);
// foreach($products as $product) {
// $product->delete();
// }

// delete product storeCategory connections
$db->delete('product_storeCategories', 'storecategories_id=' . $this->id);
}
Expand Down
38 changes: 24 additions & 14 deletions framework/modules/ecommerce/products/models/product.php
Expand Up @@ -588,30 +588,40 @@ public function afterDelete() {
global $db;

// delete all child products
if ($this->parent_id === 0) {
if ($this->parent_id == 0) {
$children = $this->find('all', 'parent_id=' . $this->id);
foreach ($children as $child) {
$child->delete();
}
}

// delete product storeCategory connections
$db->delete('product_storeCategories', 'product_id=' . $this->id);
// delete product storeCategory connections
$db->delete('product_storeCategories', 'product_id=' . $this->id . ' AND product_type="' . $this->product_type . '"');

// delete product notes
$db->delete('product_notes', 'product_id=' . $this->id);

// delete product options
$db->delete('option', 'product_id=' . $this->id . " AND optiongroup_id IN (SELECT id from " . $db->prefix . "optiongroup WHERE product_id=" . $this->id . ")");

// delete product notes
$db->delete('product_notes', 'product_id=' . $this->id);
// delete product option groups
$db->delete('optiongroup', 'product_id=' . $this->id);

// delete product options
$db->delete('option', 'product_id=' . $this->id);
// delete model aliases
$db->delete('model_aliases', 'product_id=' . $this->id);

// delete product option groups
$db->delete('optiongroup', 'product_id=' . $this->id);
// delete related product connections
$db->delete('crosssellItem_product', 'product_type="' . $this->product_type . '" AND (product_id=' . $this->id . ' OR crosssellItem_id=' . $this->id . ')');

// delete model aliases
$db->delete('model_aliases', 'product_id=' . $this->id);
// delete search index entry
$db->delete('search', "ref_type='" . $this->product_type . "' AND ref_module='" . $this->classname . "' AND original_id = " . $this->id);
} else {
// if the last child product is deleted, delete the parent
if (!$db->countObjects($this->table, 'parent_id=' . $this->parent_id)) {
$parent = new product($this->parent_id);
$parent->delete(); // do it this way to get other tables artifacts above
}
}

// delete related product connections
$db->delete('crosssellItem_product', 'product_id=' . $this->id);
}

protected function getAttachableItems() {
Expand Down

0 comments on commit d12a4ac

Please sign in to comment.