1

I have a script that allows me to add a sample request button to my product pages, I have a script called getSample.php but when I click on the sample button on the product page I'm taken to getSample.php which is showing this error:

Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (japecilot_mage2.mage_catalog_product_entity, CONSTRAINT FK_OZSS_CAT_PRD_ENTT_ATTR_SET_ID_OZSS_EAV_ATTR_SET_ATTR_SET_ID FOREIGN KEY (attribute_set_id) REFERENCES `mage_eav_attribute_set)' in /var/www/vhosts/webcontrolcentre.co.uk/httpdocs/perfume/lib/Zend/Db/Statement/Pdo.php:228 Stack trace: #0 /var/www/vhosts/webcontrolcentre.co.uk/httpdocs/perfume/lib/Zend/Db/Statement/Pdo.php(228): PDOStatement->execute(Array) #1 /var/www/vhosts/webcontrolcentre.co.uk/httpdocs/perfume/lib/Varien/Db/Statement/Pdo/Mysql.php(110): Zend_Db_Statement_Pdo->_execute(Array) #2 /var/www/vhosts/webcontrolcentre.co.uk/httpdocs/perfume/app/code/core/Zend/Db/Statement.php(291): Varien_Db_Statement_Pdo_Mysql->_execute(Array) #3 /var/www/vhosts/webcontrolcentre.co.uk/httpdocs/perfume/lib/Zend/Db/Adapter/Abstract.php(479): Zend_Db_Statement->execute(Array) #4 /var/w in /var/www/vhosts/webcontrolcentre.co.uk/httpdocs/perfume/lib/Zend/Db/Statement/Pdo.php on line 234

the code in the getSample.php is:

<?php

 ini_set('display_errors', 1);
 error_reporting(E_ALL);

 //print_r($_POST);

 require_once 'app/Mage.php';
 Mage::app();



 Mage::getSingleton("core/session", array("name" => "frontend"));
 Mage::getSingleton('core/session')->setSampleError("");
 // Store users session
 $session = Mage::getSingleton("customer/session");

 //Store cart items
 $totalNumOfCartItem = Mage::getModel('checkout/cart')->getItemsQty();

 //Store cart items
 $theitems = Mage::getModel('checkout/cart')->getItems();

 $thecount = 0;

 foreach($theitems as $ti){

 $thename = $ti['name'];

 $test = strpos($thename,'Free Sample');

 if($test !== false){
 $thecount = $thecount+1;
 }

 }

 if(isset($_POST['name'])){
 $name = $_POST['name'];

 $id = $_POST['id'];
 if(isset($_POST['simpleid']) && !empty($_POST['simpleid'])){
 $simpleid = $_POST['simpleid'];
 }
 }
 if(isset($_GET['name'])){
 $name = $_GET['name'];
 if(isset($_GET['simpleid']) && !empty($_GET['simpleid'])){
 $simpleid = $_GET['simpleid'];
 }
 $id = $_GET['id'];
 }

 // retrict shopping cart to only allow 5 samples
 if ($totalNumOfCartItem <= 4)
 {
 $theproduct = Mage::getModel('catalog/product')->load($id);

 if($simpleid){
 $thesimple = Mage::getModel('catalog/product')->load($simpleid);
 }

 if($thecount < 2)
 {
 // instatiate Product
 $product = Mage::getModel('catalog/product');
 if($simpleid){
 $product->setSku($thesimple->getSku()."*".rand());
 } else {
 $product->setSku($theproduct->getSku()."*".rand());
 }
 $product->setName("Sample of ".$name);
 // $product->setDescription("Sample of ".$name);
 $product->setShortDescription($name." – Sample");
 $product->setPrice(0.00);
 $product->setTypeId('simple');

 $product->setImageUrl($theproduct->getImageUrl()); // set the images from the original product
 $product->setThumbnailUrl($theproduct->getImageUrl());
 $product->setImage($theproduct->getSmallImage());
 $product->setMediaGallery (array('images'=>array (), 'values'=>array ()));
 $product->addImageToMediaGallery(Mage::getBaseDir('media')."/catalog/product".$theproduct->getSmallImage(), array('image','small_image','thumbnail'), false, false);
 $product->setAttributeSetId("9"); // need to look this up
 $product->setCategoryIds("3"); // need to look these up
 $product->setWeight(21212121.21);
 $product->setTaxClassId(2); // taxable goods
 $product->setVisibility(1); // catalog, search
 $product->setStatus(1); // enabled
 // assign product to the default website
 $product->setWebsiteIds(array(Mage::app()->getStore(true)->getWebsite()->getId()));


 // get product’s general info such price, status, description

 $stockData = $product->getStockData();

 // update stock data using new data
 $stockData['qty'] = 1;
 $stockData['is_in_stock'] = 1;
 $stockData['manage_stock'] = 1;
 $stockData['max_sale_qty'] = 5;

 // then set product’s stock data to update
 $product->setStockData($stockData);
 //die();

 // call save() method to save your product with updated data
 $product->save();


 header("Location: /checkout/cart/add/product/".$product->getId()."/");
 }
 }

 else
 {
 Mage::getSingleton('checkout/session')->addError("You are only allowed a maximum of 5 sample products");

 header("Location: /checkout/cart");
 }
 ?>

Any ideas on how to fix this?

Thankyou.

1 Answer 1

1

The error message looks like your product is referencing an attribute_set_id which is not present in the database.

Take a look into your database into the table mage_catalog_product_entity, find your product and look for the column attribute_set_id. The number in there e.g. 4 should be present in the table mage_eav_attribute_set in the id column. If not, your product uses an attribute set which is not available any more.

This page kind of explains what attribute sets are: http://inchoo.net/magento/importance-of-planning-attribute-sets/

One remark: Your code totally works around the framework, there might even be some security issues. (using POST, header()). You're also updating stock data. Magento does that when the customer actually finishes the checkout process, there's no need to do it manually.

3
  • Thankyou for your comment, I have the attribute_set_id of 4 in my database which is also present as an ID in the mage_eav_attribute_set table.
    – Duggy G
    Commented Oct 13, 2014 at 22:56
  • Your code sets the attributeSetId to 9. Check your mage_eav_attribute_set table and figure our which attribute set is used for products. You can do that by looking for the entity_type catalog_product in mage_eav_entity_type. Get the entity_type_id from there and compare it to mage_eav_attribute_set. $product->setAttributeSetId("9"); // need to look this up
    – RobM84
    Commented Oct 14, 2014 at 6:21
  • Thankyou for your help, that has now solved the errors.
    – Duggy G
    Commented Oct 14, 2014 at 10:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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