1

I have a global attribute called vouchercode_amount (textfield) and I want to set text data when ever the value is calculated.

Everything works fine till I try to update the value of that attribute for that specific product.

Code is below :

    $simple_product = Mage::getModel('catalog/product')->load("sku",$sku);
    $realProduct = Mage::getModel('catalog/product')->load($simple_product->getId());
    $vouchercode_amount = $couponCode."-".$voucherRule->getData('discount_amount')."-".$voucherRule->getData('description');
    $realProduct->setData("vouchercode_amount", $vouchercode_amount);
    //Tried also with 
    // $realProduct->setVouchercodeAmount($vouchercode_amount);
    // No difference
    try{
        $realProduct->save();
    }
    catch(Exception $e){
        echo "{$e}";
    }

I got always the same error saying that :

Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (**********.catalog_product_entity, CONSTRAINT FK_CAT_PRD_ENTT_ATTR_SET_ID_EAV_ATTR_SET_ATTR_SET_ID FOREIGN KEY (attribute_set_id) REFERENCES eav_attribute_set (attribute_set_id))

Product is there, attribute is there and put inside the related attribute_set already.

How can I solve this issue?

2

reverse the order of the parameters for the load method.

$simple_product = Mage::getModel('catalog/product')->load("sku",$sku);

should be

$simple_product = Mage::getModel('catalog/product')->load($sku,"sku");  

Also you might want to wrap the rest of the code in if ($simple_product->getId()).
This means that your code will be executed only if the product with that specific sku exists.
If the sku does not exist, your current code will try to create a new product and you get the error you mentioned.

[EDIT]
If you are running this from the frontend it won't work. Product save works only from the backend.
To make it work for frontend you have to add this before the save call

Mage::app()->setCurrentStore(
    Mage::getModel('core/store')->load(0)
);

[EDIT TO EDIT]

Try this code:

$productId = Mage::getSingleton('catalog/product')->getIdBySku($sku);
if ($productId) {
    $realProduct = Mage::getModel('catalog/product')->load($productId);
    $vouchercode_amount = $couponCode."-".$voucherRule->getData('discount_amount')."-".$voucherRule->getData('description');
    $realProduct->setData("vouchercode_amount", $vouchercode_amount);
    try{
        $realProduct->save();
    }
    catch(Exception $e){
        echo "{$e}";
    }
  • Thanks for your answer but it has nothing to do with that. Both does the same thing. Product is there, I do print it out and I see all the fields. For that product. In earlier stages before the $sku parameter is passed into that one that check is already done. So it is ok. – CntkCtn Jul 23 '15 at 15:15
  • Just for fun try to var_dump($realProduct->getId());exit; after the line $realProduct = Mage::getModel('catalog/product')->load($simple_product->getId());. If that's null then I'm right. If it's not null, then the problem is different. Also see my edit. – Marius Jul 23 '15 at 15:18
  • UUPPPSSS, you are right. It is NULL. How can that be? – CntkCtn Jul 23 '15 at 15:26
  • @CntkCtn. See my update. – Marius Jul 23 '15 at 15:28

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.