tags:

views:

4285

answers:

6

Very left of field, but worth a shot.

I've got a client database with a large range of stock items, which are being uploaded to Magento as simple products.

Now I need to group them up and assign them to configurable products with their size and colour being their configurable attributes.

The Magento API has a Product_Link class, with a promising looking method: catalogue-product-link.assign (link), but I can't for the life of me figure out what arguments I need to make it work with configurable products, providing this is how assign was meant to be used.

Thanks, Keith

A: 

I this is an un-educated guess, but I think what your asking for can't be done with the existing API. You will have to write your own or just got directly to the DB.

Dan
With the EAV db schema they use there is no 'just' when directly accessing the DB. The Pain!!!
Martijn Heemels
+4  A: 

I wrote a post recently (http://www.omnisubsole.com/blog/2009/07/01/configurable-products-in-magento.html) describing how I accomplished this. I do not use the api; instead a use the actual php classes to import products, both simple and confiigurable

Dave Jones
is that blog post removed?
dimitris mistriotis
A: 

Like all programmers, I'm lazy. I actually load Magento and access it's API directly (instead of as a webservice). The advantage is that, in most cases, it's simpler and more succinct than working with the model directly.

This is how I used the Product Link API.

public function assignProduct($product, $linkedProduct, $qty)
{
    $linkData['qty'] = $qty;

    $product_link_api = Mage::getModel('catalog/product_link_api');
    /*@var $product_link_api Mage_Catalog_Model_Product_Link_Api */

    // find out if 'configurable' is the proper string for your type
    // I'm only sure of 'simple' and 'grouped'
    $product_link_api->assign('configurable', $product, $linkedProduct, $linkData);

}

// ID of product to be linked
$link_product_id = 55;

// ID of your configurable product
$product_id = 123;

assignProduct($product_id, $link_product_id, 2);
Phil R.
does this REALLY work? 'configurable' does not seem to be one of the allowed parameters of assign() method.
Ree
'configurable' does not work..
Martin
A: 

I'm working on doing this right now.

So far I've found these items helpful as references:

I'll post my code so far, and hopefully update it once it works..

// Set 'item_size' as the super attribute  # choose your own attribute!
// this is the 'choose-able' field that differenciates products
$super_attributes=array( Mage::getModel('eav/entity_attribute')
  ->loadByCode('catalog_product','item_size')
  ->getData('attribute_id')
  );  
$product_collection=Mage::getModel('catalog/product')->getCollection();

// Fetch configurable orders
$product_collection->addFieldToFilter('type_id',Array('eq'=>"configurable"));
#$product_collection->addFieldToFilter('sku',Array('eq'=>"ASMCL000002"));  

$product_collection->addAttributeToSelect('*');

$count=0;
foreach($product_collection as $product) {
  $sku = $product->getSku();
  echo "SKU: $sku\n";

  $simple_children_collection = Mage::getModel('catalog/product')->getCollection();
  $simple_children_collection->addAttributeToSelect('*');
  $simple_children_collection->addFieldToFilter('sku',Array('like'=>$sku . "-%"));
  echo "children: ";
  foreach($simple_children_collection as $child) {
      $child_sku = $child->getSku();
      echo "$child_sku ";
      #visiblity should be 'nowhere'
  }
  echo "\n";

if (!$product->getTypeInstance()->getUsedProductAttributeIds()) {
  # This is a new product without the Configurable Attribue Ids set
  $product->getTypeInstance()
    ->setUsedProductAttributeIds( $super_attributes );

  //$product->setConfigurableAttributesData(array($_attributeData));
  $product->setCanSaveConfigurableAttributes(true); # Not sure if this is needed.

  $product->setConfigurableProductsData(''); # Use this to add child products.

}


  $count++;

  try {
      $product->save();
      $productId = $product->getId();
      echo $product->getId() . ", $sku updated\n";
  }
  catch (Exception $e){
      echo "$sku not added\n";
      echo "exception:$e";
  }

}
echo "\nCount is $count\n";

Okay, this uses 'item_size' as the attribute that differentiates the "simple" products. Also, this assumes that the "configurable" parent SKU is the root of the child SKU. For example, ABC001 is the parent while ABC001-SMALL and ABC001-LARGE are the simple children.

Hope that helps someone.

xer0x
I don't know if you're still working on this but I think I've cracked it.
Scimon
A: 

Using our commercial API extension package NETZKOLLEKTV Core API enables you to add configurable products via Magento Core API.

For more information see NETZKOLLEKTIV Core API

NETZKOLLEKTIV
A: 

Well the notes here helped me get this running. So I thought I'd share with you the code to add a simple product to an existing Configurable Product.

This code assumes the simple product is a valid one to add, I'm not sure what would happen if it wasn't.

private function _attachProductToConfigurable( $_childProduct, $_configurableProduct ) {
   $loader = Mage::getResourceModel( 'catalog/product_type_configurable' )->load( $_configurableProduct );

   $ids = $_configurableProduct->getTypeInstance()->getUsedProductIds(); 
   $newids = array();
   foreach ( $ids as $id ) {
      $newids[$id] = 1;
   }

   $newids[$_childProduct->getId()] = 1;

   $loader->saveProducts( $_configurableProduct->getId(), array_keys( $newids ) );                
}
Scimon
I am trying to do this from a command line script and I fail here:$loader = Mage::getResourceModel( 'catalog/product_type_configurable' )->load( $_configurableProduct );(first line)Any ideas? I'm currently investigating it and will inform in case of a result.
dimitris mistriotis