1

I've this code in Magento:

<?php

    include_once('C:/xampp/htdocs/magento/app/Mage.php');

    Mage::app();
    $rootMagento = mage::getBaseDir();
    $files = scandir($rootMagento);
    $count = sizeof($files);
    for ($i =0; $i< $count; $i++) {
        if ($files[$i]=='productData.txt'){
            $fileAperto = fopen($files[$i], "r");
            $buffer = fread($fileAperto,filesize("productData.txt"));
            list($sku, $name, $description, $shortDescription, $price) = split("[\n]", $buffer);
            if ($sku == '' || $name == '' || $description == '' 
                || $shortDescription == '' || $price == '') {
                mage::log('esportazione non corretta: manca uno dei dati');
            } 
            fclose($fileAperto);
        }
    }

    //// prodotto ////

    $product_model = mage::getModel('catalog/product');

    $productId = $product_model->getIdBySku($sku);
    if (!$productId){
        //inserimento
        $productData = array ('sku' => $sku,
                              'name' => $name,
                              'description' => $description,
                              'short_description' => $shortDescription,
                              'status' => 0,
                              'visibility' => 0
                              );

        $product_model->setData($productData);
        try{
            $productId = $product_model->save()->getId();
            $product = $product_model->load($productId);
            $product->setQty($qty);
            $product->setIsInStock(1); //->isInStock(1)
            $product->save();
        }catch(Exception $e){
            mage::log($e->getMessage());
        }

    }else{
        //aggiornamento
        $product = $product_model->load($productId);
        // $productData... ... .. 
        $product->setData($productData); // + codice x stock
    }

When I run it, I've this error:

Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (magento.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) ON DE), query was: INSERT INTO catalog_product_entity (entity_type_id, sku, created_at, updated_at) VALUES (?, ?, '2016-08-24 16:15:22', '2016-08-24 16:15:22')

Can you help me please?

Thank you very much!

1

In else part $productData variable is not accessing. It is local scope. Can you run the code without else part.

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.