M1 Error when adding products to website using Mage_Catalog_Model_Product_Website addProducts($websiteIds, $productIds)

magento.stackexchange https://magento.stackexchange.com/questions/328625

  •  15-04-2021
  •  | 
  •  

Question

I am trying to add products to a website using addProducts($websiteIds, $productIds). I have debugged the passed data and it looks fine (see below). The error we encounter is below.

What are we doing wrong here?

And it seems to error almost at random, sometimes when we update website 1 next time it is website 3 that results in an error

Maybe of interest, because of this error we introduced a stepped/sliced approach because this did seem to solve it temporarily.

Walk over array and update in steps to reduce the errors

* Function to execute filtering on collection 
*/
function addProductsFunction($website, $ids) {

    $j =0;
    $slicesize = 10; // set the step value (in your case it will 100.)
    $steps = ceil(count($ids)/$slicesize);
    for($i = 0 ;$i<$steps;$i++) {
        $b = array_slice($ids, $j, $slicesize);
        $j = $j+$slicesize; // increment counter by to step position. 
        Mage::getModel('catalog/product_website')->addProducts(array($website), $b);
    }
}

The original error

Fatal error: Uncaught Mage_Core_Exception: An error occurred while adding products to websites. in app/Mage.php:657
Stack trace:
#0 app/code/core/Mage/Catalog/Model/Product/Website.php(94): Mage::throwException('An error occurr...')
#1 cs/cronscripts/update_websites_membership.php(195): Mage_Catalog_Model_Product_Website->addProducts(Array, Array)
#2 cs/cronscripts/update_websites_membership.php(169): addProductsFunction(9, Array)
#3 cs/cronscripts/update_websites_membership.php(66): AddtoWebsites(Object(Mage_Catalog_Model_Resource_Product_Collection), 9, false, NULL, 'Start update at...')
#4 {main}
  thrown in app/Mage.php on line 657

addProducts functions

/**
 * Add products to websites
 *
 * @param array $websiteIds
 * @param array $productIds
 * @return $this
 */
public function addProducts($websiteIds, $productIds)
{
    try {
        $this->_getResource()->addProducts($websiteIds, $productIds);
    } catch (Exception $e) {

        Mage::throwException(
            Mage::helper('catalog')->__('An error occurred while adding products to websites.')
        );
    }
    return $this;
}

the array data we are passing

website id array(1) {
  [0]=>
  int(9)
}
product ids array(20) {
  [0]=>
  string(6) "115430"
  [1]=>
  string(6) "115431"
  [2]=>
  string(6) "115432"
  [3]=>
  string(6) "115433"
  [4]=>
  string(6) "115434"
  [5]=>
  string(6) "115430"
  [6]=>
  string(6) "115431"
  [7]=>
  string(6) "115432"
  [8]=>
  string(6) "115433"
  [9]=>
  string(6) "115435"
  [10]=>
  string(6) "115436"
  [11]=>
  string(6) "115437"
  [12]=>
  string(6) "115438"
  [13]=>
  string(6) "115439"
  [14]=>
  string(6) "115435"
  [15]=>
  string(6) "115436"
  [16]=>
  string(6) "115437"
  [17]=>
  string(6) "115438"
  [18]=>
  string(6) "115440"
  [19]=>
  string(6) "115441"
}

Was it helpful?

Solution

Duplicates!

We are looping over a product collection filtering for an attribute. It should find only parent products, then sub loop to include the children. But! when the simple product also has the attribute set it is separately included too. The result: a DUPE!

enter image description here

Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top