2

I am trying, unsuccessfully, to add products into a store in a multi-website, multi store site. I am basing the script on one which works fine on single store projects so I am guessing that the error is down to something like store and website ids or "current store" settings. The product appears in the DB and I can load it from a script - however it does not appear in the backend or front end of the site. Can anyone assist in explaining the correct combination of store Ids (or what else I might be missing).

Here is the site/store structure:

Main Website (1)
UK Site (2)
    -> Uk store (2)
EU Site (3)
    -> Spanish store (3)
    -> German store (4)
    -> French store (5)

Here is are the main/important bits of the script. In this instance I am trying to save the product to the UK site (website id 2) and UK store (store id 2):

Mage::app();
Mage::setIsDeveloperMode(true);
Mage::app()->setCurrentStore(Mage::getModel('core/store')->load(Mage_Core_Model_App::ADMIN_STORE_ID));
//NOTE: have also tried Mage::app()->setCurrentStore(Mage::getModel('core/store')->load(2));
$cstore = Mage::app()->getStore(); //quick check

/* when setCurrentStore is set to 2 $cstore includes:
* [_data:protected] => Array
*       (
*           [store_id] => 2
*           [code] => uk
*           [website_id] => 2
*           [group_id] => 2
*           [name] => UK
*           [sort_order] => 1
*           [is_active] => 1
*       )
*
*    when setCurrentStire is done with ADMIN_STORE_ID, we get this
*
*        [_data:protected] => Array
*        (
*            [store_id] => 0
*            [code] => admin
*            [website_id] => 0
*            [group_id] => 0
*            [name] => Admin
*            [sort_order] => 0
*            [is_active] => 1
*        )
*/

//attributeSetID is  pulled out but for the purpose of this..
$attributeSetId= 4; // default

//dummy item
$item = array("name"=>"test product","id"=>"123456");

//I have tried using setWebsiteId and setWebsiteIds  (and same for storeId)

$this->magObject = Mage::getModel("catalog/product"); //new     
$this->magObject ->setTypeId('grouped')
        ->setSku($item["id"])
        ->setAttributeSetId($attributeSetId)
        ->setStatus(Mage_Catalog_Model_Product_Status::STATUS_ENABLED)
        //->setWebsiteIds(array(2))
        ->setWebsiteId(2)
        //->setStoreIds(array(2))
        ->setStoreId(2)
        ->setName($item["name"])
        ->setCreatedAt(strtotime('now'))
        ->setMetaTitle($item["name"])
        ->setMetaDescription('test meta description')
        ->setDescription('This is a long description')
        ->setTaxClassId(0) //tax class (0 - none, 1 - default, 2 - taxable, 4 - shipping)
        ->setPrice(9999.99)
        ->setVisibility(Mage_Catalog_Model_Product_Visibility::VISIBILITY_BOTH)
        ->setWeight(4.0000)
        ->setCreatedAt(strtotime('now'))
        ->setStockData(
            array(
            'is_in_stock' => 1,
            'qty' => 99999,
            'manage_stock' => 0,
            'use_config_manage_stock' => 0
            )
        );


$this->magObject->save();
$newRangeId = $this->magObject->getId(); // this correctly returns the new Id of the product in the DB, which can then be loaded and updated
$this->echoOut(" - New Range ID is ".$newRangeId . " - now updating");

UPDATE:

I added in the code to set the CategoryIds and the items do appear in the admin now - all sorted. One would think that you could have a product not in a category (yet).

  • Do I need to set a category ID at this stage? – Jon Holland Aug 17 '15 at 12:11
  • I have seen that there is no entry for the newly created product(s) in catalog_product_website – Jon Holland Aug 17 '15 at 12:36
2

The problem turned out to be that I was not assigned the products to a category or categories.

By adding in

$this->magObject->setCategoryIds(array(#some ids here));

The objects appeared in the admin.

By changing setWebsiteId(2) and setStoreId(2) back to setWebsiteIds(array(1, 2)) and setStoreIds(array(1, 2)) the products were then correctly assigned in the admin too.

Seems a bit wrong that you have to assign a product to a category just to get it to show - that may be something one wanted to do at a later date, but there you go.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.