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
, CONSTRAINTFK_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.