Magento Forum

   
How do I add Crosssell Products correctly? (in php, not using the admin backend) [SOLVED]
 
vinai
Sr. Member
 
Avatar
Total Posts:  218
Joined:  2008-04-04
Heidelberg, Germany
 

I’m trying to add a linked crosssell product to a Mage_Catalog_Model_Product object.
But, my code generates the following SQL Error:

SQLSTATE[23000]Integrity constraint violation1452 Cannot add or update a child row:
a foreign key constraint fails (`perlkoenig/catalog_product_link`,
CONSTRAINT `FK_PRODUCT_LINK_LINKED_PRODUCTFOREIGN KEY (`linked_product_id`)
REFERENCES `catalog_product_entity` (`entity_id`) ON DELETE CASCADE ON UPDATE C)

Here is my code:

<?php
    $product 
Mage::getModel('catalog/product')->load($productId);
    
// $linkProduct is not yet linked to $product as a crosssell product
    
$linkProduct Mage::getModel('catalog/product')->load($linkProductId);
    
    
// add $linkProduct to the array of crosssell products of $product
    
$xsProducts $product->getCrossSellProducts();
    
$xsProducts[] $linkProduct;
    
$product->setCrossSellLinkData($xsProducts);
    
    
$product->save();
?>

What am I missing??
Pointers very much appreciated,

Vinai

 
Magento Community Magento Community
Magento Community
Magento Community
 
LeeSaferite
Guru
 
Avatar
Total Posts:  322
Joined:  2007-08-31
Lake City, FL
 

Does this help any?

$product Mage::getModel('catalog/product')->load($productId);
    
// $linkProduct is not yet linked to $product as a crosssell product
    
$linkProduct Mage::getModel('catalog/product')->load($linkProductId);
    
    
// add $linkProduct to the array of crosssell products of $product
    
$xsLinkData $product->getCrossSellLinkData();
    
$xsLinkData[$linkProduct->getId()= array('position' => 0);
    
$product->setCrossSellLinkData($xsLinkData);
    
    
$product->save();

See:

Mage_Catalog_Model_Resource_Eav_Mysql4_Product_Link

 
Magento Community Magento Community
Magento Community
Magento Community
 
vinai
Sr. Member
 
Avatar
Total Posts:  218
Joined:  2008-04-04
Heidelberg, Germany
 

Lee - I think you once again saved the day!

A first test seems to show that your code works.
I’ll look at Mage_Catalog_Model_Resource_Eav_Mysql4_Product_Link tomorrow, and test some more, too, but I think it’s handled.

Thanks a million,
Vinai

 
Magento Community Magento Community
Magento Community
Magento Community
 
vinai
Sr. Member
 
Avatar
Total Posts:  218
Joined:  2008-04-04
Heidelberg, Germany
 

Okay, adding crosssell products works! But…

$xsLinkData $product->getCrossSellLinkData();
always returns an empty value, so I loose all existing crosssell information.

Do you happen to have some wisdom to share on that, too?
How do I populate that array?
Thanks!
Vinai

 
Magento Community Magento Community
Magento Community
Magento Community
 
LeeSaferite
Guru
 
Avatar
Total Posts:  322
Joined:  2007-08-31
Lake City, FL
 

You might try calling

$product->getCrossSellLinkCollection();

That may initialize the data for you.  I don’t have a setup to test it though. wink I’m just tracing the code path to find this stuff for you.

 
Magento Community Magento Community
Magento Community
Magento Community
 
vinai
Sr. Member
 
Avatar
Total Posts:  218
Joined:  2008-04-04
Heidelberg, Germany
 

i tried that already before posting wink
Thanks anyway!
Now I’m building the $xsLinkData Array from the getCrossSellLinkCollection() myself instead of calling getCrossSellLinkData().
It works, and that’s the main thing smile
Thanks for your help!

Vinai

 
Magento Community Magento Community
Magento Community
Magento Community
 
LeeSaferite
Guru
 
Avatar
Total Posts:  322
Joined:  2007-08-31
Lake City, FL
 

Hey, I think this may help a bit

/magento/app/code/core/Mage/Catalog/Model/Product/Link/Api.php Line 94

 
Magento Community Magento Community
Magento Community
Magento Community
 
LeeSaferite
Guru
 
Avatar
Total Posts:  322
Joined:  2007-08-31
Lake City, FL
 

Or, just to post the code you need to get the CrossSellLinkData

$link $product->getLinkInstance()->setLinkTypeId(Mage_Catalog_Model_Product_Link::LINK_TYPE_CROSSSELL);
$collection $link->getProductCollection()->setIsStrongMode()->setProduct($product);
$result = array();
foreach (
$collection as $linkedProduct{
   $result[$linkedProduct
->getId()= array();
   foreach (
$collection->getLinkModel()->getAttributes() as $attribute{
      $result[$linkedProduct
->getId()][$attribute['code']] $linkedProduct->getData($attribute['code']);
   
}
}

$result should be your CrossSellLinkData

But I suggest you either use the code from the API verbatum or even better, call the API functions directly.

 
Magento Community Magento Community
Magento Community
Magento Community
 
vinai
Sr. Member
 
Avatar
Total Posts:  218
Joined:  2008-04-04
Heidelberg, Germany
 

Great idea, use the Api smile
My hope in you was well placed :D

So far I steered clear of the api since I’m making extensions and no external calls, but hey - why not use it since it’s there.
Thanks for the pointer!

Vinai

 
Magento Community Magento Community
Magento Community
Magento Community
Magento Community
Magento Community
    Back to top