Jump to content

Assign Multiple products to a Category


jboras

Recommended Posts

Hi Guys,

 

I have searched through the topics but haven't found anything. Can anyone assist if they know how?

 

We have uploaded a number of products to a catagory. This category has become quite congested and we have split it up into a few child categories.

 

Is there a way to bulk assign products to their appropriate child categories?

 

We have tried filtering products that would go to a child category; tries "Assign Category" but it is not working.

 

Any clues would be greatly appreciated

 

Latest CC version

Link to comment
Share on other sites

Not aware of a quick way to do this in admin. You could update the category_index table if you have access to it (various ways you could do this), might be quicker than doing it through admin but personally I would do it manually for each product in admin if you don't have too many.

Link to comment
Share on other sites

The Assign to Category tab in the admin Products screens does not currently have the ability to distinguish which of the various selected categories would be the product(s) primary category.

 

This function first destroys every relationship a given product has to the categories. Then it will assign all of the selected items to all of the selected categories -- but only as secondary categories.  I believe this is why you perceive the assignments is not working.

 

(I would hope the CubeCart code would note there is no primary category specified in the Category Index table, and then use the cat_id in the Inventory table to update the Category Index table.)

 

So, your task is to update the primary category for a selection of products. Are you in a position to experiment with making some small changes to the code that assigns categories to products in bulk?

Link to comment
Share on other sites

Here are two edits for the bulk product/category assignment function. This makes it possible for the assignment to additionally specify which category is to be the primary category. The original code has not changed from CC524 to CC5212.

 

In /admin/sources/products.assign.inc.php, lines 8-19:

Was:
## Assign products to categories
if (is_array($_POST['category']) && is_array($_POST['product'])) {
  foreach ($_POST['product'] as $product_id) {
    if (!is_numeric($product_id) || !is_array($_POST['category'])) continue;
    //Delete all the category related to comming product id  to fix bug 2840
    $GLOBALS['db']->delete('CubeCart_category_index', array('product_id' => (int)$product_id));
    foreach ($_POST['category'] as $category_id) {
      if (!is_numeric($category_id)) continue;
      $GLOBALS['db']->insert('CubeCart_category_index', array('cat_id' => (int)$category_id, 'product_id' => (int)$product_id));
    }
  }
}
 
Now:
## Assign products to categories
if ((is_array($_POST['category']) || isset($_POST['category_pri'])) && is_array($_POST['product'])) {
  foreach ($_POST['product'] as $product_id) {
    if ( !is_numeric($product_id) ) continue;
    //Delete all the category related to comming product id  to fix bug 2840
    $GLOBALS['db']->delete('CubeCart_category_index', array('product_id' => (int)$product_id));
    if (isset($_POST['category_pri']) && (int)$_POST['category_pri']) {
      $GLOBALS['db']->insert('CubeCart_category_index', array('cat_id' => (int)$_POST['category_pri'], 'product_id' => (int)$product_id, 'primary' => 1));
    }
    if(is_array($_POST['category'])) {
      foreach ($_POST['category'] as $category_id) {
        if (!is_numeric($category_id)) continue;
        $GLOBALS['db']->insert('CubeCart_category_index', array('cat_id' => (int)$category_id, 'product_id' => (int)$product_id));
      }
    }
  }
}

In the file /admin/skins/default/templates/products.assign.php, line 41:

Was:
<span><input type="checkbox" name="category[]" value="{$category.id}" /></span>
 
Now:
<span><input type="radio" name="category_pri" value="{$category.id}" /></span>
<span><input type="checkbox" name="category[]" value="{$category.id}" /></span>

Requirements: You should always choose a primary category (use the radio button). You can choose none or many secondary categories (use the checkboxes).

 

As was mentioned earlier, using this function removes all the records in the Category Cross-index table for the product(s) chosen - including the records that identified the primary category for that product. So, it is best to always re-indicate the primary category.

 

This still leaves open the matter of the 'cat_id' column in the Inventory table versus the 'cat_id - primary' value in the Cross-Index table. Which functions in the codebase use the Inventory value, and which functions in the codebase use the Cross-Index value? And is there code that routinely sync's these values?
 

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...