okay probeer ik het opnieuw ;-) ik wilde alleen het topic beetje kort houden kwa code leek me namelijk in dit geval niet heel erg nodig maar goed als jullie het graag willen post ik alle code wel die in het script zitten.
ik doe me best. deels is code wat al in het systeem zit opencart en deels is wat er is gemaakt in ene php script die via een api verbinding maakt met een leverancier en zodoende producten importeert in de database. mits er nieuwe producten zijn.
<?php
public function checkProduct($data) {
$search = '';
if ($data['sku']) {
$search .= "sku = '" . $this->db->escape($data['sku']) . "'";
}
if ($search) {
$query = $this->db->query("SELECT * FROM " . DB_PREFIX . "product WHERE " . $search . " LIMIT 1");
if ($query->num_rows) {
return $query->row['product_id'];
}
}
$query = $this->db->query("SELECT * FROM " . DB_PREFIX . "product_description WHERE name = '" . $this->db->escape(trim($data['name'])) ."' LIMIT 1");
if ($query->num_rows) {
return $query->row['product_id'];
}
return false;
}
public function addProduct($data) {
$language_id = 2;
$prijsje = "";
if (isset($data['customer_group_price_rrp'])) {
$prijs_excl_btw = round($data['customer_group_price_rrp']['website_price'] / 1.21, 4);
//$prijs_excl_btw = $prijs_excl_btw / 1.21;
// Vermenigvuldigen met 1,21 voor 21% btw
$prijs_incl_btw = 1.21 * $prijs_excl_btw;
$prijs_incl_btw = round($prijs_incl_btw, 2);
// Vermenigvuldigen met 4, casten naar integer en weer delen door 4
$afgeronde_prijs_incl_btw = 4 * $prijs_incl_btw;
$afgeronde_prijs_incl_btw = (int)$afgeronde_prijs_incl_btw;
$afgeronde_prijs_incl_btw = $afgeronde_prijs_incl_btw / 4;
$afgeronde_prijs_incl_btw = ceil($afgeronde_prijs_incl_btw) - 0.05;
$prijsje = $afgeronde_prijs_incl_btw / 1.21;
//$prijsje = ceil($prijsje2) - 0.05;
}
if (isset($details['customer_group_price']['special_price'])) {
$prijs_excl_btw = round($data['customer_group_price_rrp']['website_price'] / 1.21, 4);
$prijs_excl_btw = $prijs_excl_btw / 1.21;
// Vermenigvuldigen met 1,21 voor 21% btw
$prijs_incl_btw = 1.21 * $prijs_excl_btw;
$prijs_incl_btw = round($prijs_incl_btw, 2);
// Vermenigvuldigen met 4, casten naar integer en weer delen door 4
$afgeronde_prijs_incl_btw = 4 * $prijs_incl_btw;
$afgeronde_prijs_incl_btw = (int)$afgeronde_prijs_incl_btw;
$afgeronde_prijs_incl_btw = $afgeronde_prijs_incl_btw / 4;
$afgeronde_prijs_incl_btw = ceil($afgeronde_prijs_incl_btw) - 0.05;
$prijsje = $afgeronde_prijs_incl_btw / 1.21;
//$data['price'] = round($data['customer_group_price_rrp']['special_price'] / 1.60, 4);
}
$default_status = 1;
$default_category = 1;
$this->db->query("INSERT INTO " . DB_PREFIX . "product SET model = '" . $this->db->escape(trim($data['model'])) . "', sku = '" . $this->db->escape($data['sku']) . "', ean = '" . $this->db->escape($data['eancode']) . "', quantity = '100', minimum = '1', subtract = '1', manufacturer_id = '6', shipping = '1', price = '" . (float)$prijsje . "', status = '". $default_status ."', tax_class_id = '9', date_added = NOW()") or die(mysqli_error($db));
$product_id = $this->db->getLastId();
$path = 'data/hvpolo/';
if (isset($data['image'])) {
$image = explode('/', $data['image']);
if (count($image) > 1) {
$image = $image[count($image) - 1];
} else {
$image = $image[0];
}
if (!file_exists(DIR_IMAGE . $path . $image)) {
$this->getImage($data['image'], $image);
}
$this->db->query("UPDATE " . DB_PREFIX . "product SET image = '" . $this->db->escape($path . $image) . "' WHERE product_id = '" . (int)$product_id . "'") or die(mysqli_error($db));
}
$description = str_replace('. ', '.<br /><br />', $data['short_description']) . '<br /><br />' . str_replace('. ', '.<br /><br />', $data['description']) . '.';
$description = str_replace('..', '.', $description);
if (strpos($data['name'], 'Zweetdeken') OR strpos($data['name'], 'Zweetdeken')!== false) {
$array = array("1", "5", "21");
foreach($array as $cat){
$this->db->query("INSERT INTO " . DB_PREFIX . "product_to_category SET product_id = '" . (int)$product_id . "', category_id = '" . $cat . "'") or die(mysqli_error($db));
}
echo "ik heb het volgend artikel toevoegd ". $data['name'] .", check je het even ?<br /> aan categorie " . $cat . " zweetdeken";
} elseif (strpos($data['name'], 'rok') OR strpos($data['name'], 'blouse') OR strpos($data['name'], 'Blouse') OR strpos($data['name'], 'jurk') OR strpos($data['name'], 'skirt') OR strpos($data['name'], 'dress') !== false) {
$array = array("1", "2", "25");
foreach($array as $cat){
$this->db->query("INSERT INTO " . DB_PREFIX . "product_to_category SET product_id = '" . (int)$product_id . "', category_id = '" . $cat . "'") or die(mysqli_error($db));
}
echo "ik heb het volgend artikel toevoegd ". $data['name'] .", check je het even ?<br /> aan categorie " . $cat . " (110, 137)";
} elseif (strpos($data['name'], 'baseballcap') OR strpos($data['name'], 'Baseball pet') !== false) {
$array = array("1", "2", "17", "28", "32");
foreach($array as $cat){
$this->db->query("INSERT INTO " . DB_PREFIX . "product_to_category SET product_id = '" . (int)$product_id . "', category_id = '" . $cat . "'") or die(mysqli_error($db));
}
echo "ik heb het volgend artikel toevoegd ". $data['name'] .", check je het even ?<br /> aan categorie " . $cat . " (110, 137)";
}
// en dan nog veel meer met andere keywords die delete ik even voor het aantal regels .
else {
$this->db->query("INSERT INTO " . DB_PREFIX . "product_to_category SET product_id = '" . (int)$product_id . "', category_id = '1'") or die(mysqli_error($db));
}
$keyword = str_replace(' ', ', ', strtolower($data['name']));
$this->db->query("INSERT INTO " . DB_PREFIX . "product_description SET product_id = '" . (int)$product_id . "', language_id = '" . (int)$language_id . "', name = '" . $this->db->escape($data['name']) . "', description = '" . $this->db->escape($description) . "', meta_title = '" . $this->db->escape($data['name']) . "', meta_description = '" . $this->db->escape($data['short_description']) . "', meta_keyword = '". $this->db->escape($keyword) ."'") or die(mysqli_error($db));
$this->db->query("INSERT INTO " . DB_PREFIX . "product_to_store SET product_id = '" . (int)$product_id . "', store_id = '0'");
$color_id = 48;
$size_id = 47;
// functie voor variations
if ($data['variations']) {
// Product
$this->db->query("INSERT INTO " . DB_PREFIX . "power_options_product SET pwr_product_id = '" . (int)$product_id . "', pwr_subtract = '1', pwr_price_prefix = '+', pwr_price_standard = '1', pwr_special_prefix = '=', pwr_special_standard = '1', pwr_points_prefix = '=', pwr_weight_prefix = '=', pwr_dimensions_prefix = '=', pwr_code = '0'") or die(mysqli_error($db));
// Kleur
$this->db->query("INSERT INTO " . DB_PREFIX . "power_options SET pwr_product_id = '" . (int)$product_id . "', pwr_level = '0', pwr_option_id = '" . (int)$color_id . "'") or die(mysqli_error($db));
// Maat
$this->db->query("INSERT INTO " . DB_PREFIX . "power_options SET pwr_product_id = '" . (int)$product_id . "', pwr_level = '1', pwr_option_id = '" . (int)$size_id . "'") or die(mysqli_error($db));
foreach ($data['variations'] as $key => $variation) {
$image = '';
// Kleur
$color = trim($variation['color']);
$check = $this->db->query("SELECT option_value_id FROM " . DB_PREFIX . "option_value_description WHERE option_id = '" . (int)$color_id . "' AND name = '" . $this->db->escape($color) . "' LIMIT 1");
if ($check->num_rows) {
$color_value_id = $check->row['option_value_id'];
} else {
// Insert option value
$this->db->query("INSERT INTO " . DB_PREFIX . "option_value SET option_id = '" . (int)$color_id . "'");
$color_value_id = $this->db->getLastId();
$this->db->query("INSERT INTO " . DB_PREFIX . "option_value_description SET option_value_id = '" . (int)$color_value_id . "', language_id = '" . (int)$language_id . "', option_id = '" . (int)$color_id . "', name = '" . $this->db->escape($color) . "'");
}
// Maat
$size = trim($variation['shirt_size']);
$check = $this->db->query("SELECT option_value_id FROM " . DB_PREFIX . "option_value_description WHERE option_id = '" . (int)$size_id . "' AND name = '" . $this->db->escape($size) . "' LIMIT 1");
if ($check->num_rows) {
$size_value_id = $check->row['option_value_id'];
} else {
// Insert option value
$this->db->query("INSERT INTO " . DB_PREFIX . "option_value SET option_id = '" . (int)$size_id . "'");
$size_value_id = $this->db->getLastId();
$this->db->query("INSERT INTO " . DB_PREFIX . "option_value_description SET option_value_id = '" . (int)$size_value_id . "', language_id = '" . (int)$language_id . "', option_id = '" . (int)$size_id . "', name = '" . $this->db->escape($size) . "'");
}
// Power option aanmaken
$this->db->query("INSERT INTO " . DB_PREFIX . "power_options_data SET pwr_product_id = '" . (int)$product_id . "', pwr_model = '" . $key . "', pwr_sku = '" . (int)$data['sku'] . "', pwr_product_code = '" . $variation['eancode'] . "', pwr_quantity = '" . $variation['stock'] . "', pwr_points = '0', pwr_weight = '0', pwr_length = '0', pwr_width = '0', pwr_height = '0'");
$power_id = $this->db->getLastId();
// Kleur en maat aan power koppelen
$this->db->query("INSERT INTO " . DB_PREFIX . "power_options_value SET pwr_id = '" . (int)$power_id . "', pwr_product_id = '" . (int)$product_id . "', pwr_level = '0', pwr_option_value_id = '" . (int)$color_value_id . "'");
$this->db->query("INSERT INTO " . DB_PREFIX . "power_options_value SET pwr_id = '" . (int)$power_id . "', pwr_product_id = '" . (int)$product_id . "', pwr_level = '1', pwr_option_value_id = '" . (int)$size_value_id . "'");
if ($variation['image'] && $variation['image'] != $data['image']) {
$image = explode('/', $variation['image']);
if (count($image) > 1) {
$image = $image[count($image) - 1];
} else {
$image = $image[0];
}
if (!file_exists(DIR_IMAGE . $path . $image)) {
$this->getImage($variation['image'], $image);
}
}
// Kleur koppelen als code als er een afbeelding is
if ($image) {
$this->db->query("INSERT INTO " . DB_PREFIX . "power_options_code SET pwr_product_id = '" . (int)$product_id . "', pwr_code = '" . (int)$color_value_id . "'");
$power_code_id = $this->db->getLastId();
// Kleur koppelen aan afbeelding code
$check = $this->db->query("SELECT pwr_code_id FROM " . DB_PREFIX . "power_options_image WHERE pwr_product_id = '" . (int)$product_id . "' AND pwr_image = '" . $this->db->escape($path . $image) . "' LIMIT 1");
if (!$check->num_rows) {
$this->db->query("INSERT INTO " . DB_PREFIX . "power_options_image SET pwr_code_id = '" . (int)$power_code_id . "', pwr_product_id = '" . (int)$product_id . "', pwr_image = '" . $this->db->escape($path . $image) . "', pwr_sort_order = '0'");
}
}
}
}
if ($data['name']) {
$keyword = str_replace(' ', '-', strtolower($data['name']));
$query = $this->db->query("SELECT url_alias_id FROM " . DB_PREFIX . "url_alias WHERE keyword = '" . $this->db->escape($keyword) . "' LIMIT 1");
if ($query->num_rows) {
$keyword = $keyword . '-' . $product_id;
}
$this->db->query("INSERT INTO " . DB_PREFIX . "url_alias SET query = 'product_id=" . (int)$product_id . "', keyword = '" . $this->db->escape($keyword) . "'");
}
if ($data['name']) {
$keyword = str_replace(' ', '-', strtolower($data['name']));
$query = $this->db->query("SELECT seo_url_id FROM " . DB_PREFIX . "seo_url WHERE keyword = '" . $this->db->escape($keyword) . "' LIMIT 1");
if ($query->num_rows) {
$keyword = $keyword . '-' . $product_id;
}
$this->db->query("INSERT INTO " . DB_PREFIX . "seo_url SET store_id = '0', language_id = '2', query = 'product_id=" . (int)$product_id . "', keyword = '" . $this->db->escape($keyword) . "'");
}
}
private function getImage($url, $image) {
$path = 'data/hvpolo/';
$content = file_get_contents($url);
$fp = fopen(DIR_IMAGE . $path . $image, "w");
fwrite($fp, $content);
fclose($fp);
return DIR_IMAGE . $path . $image;
}
?>
dan heb je nog het model
omdat het MVC is opgebouwd
api.php
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
class ControllerApiHvpolo extends Controller {
protected $client;
protected $session_id;
protected $user = 'userid';
protected $key = 'wachtwoord';
protected $DefaultCategoryID = '0000';
protected $storeID = '25';
protected $emails = ['[email protected]', '[email protected]'];
// protected $emails = ['[email protected]', '[email protected]'];
public function index() {
$this->client = new SoapClient('http://website.com/api/soap/?wsdl');
$this->session_id = $this->client->login($this->user, $this->key);
// Get all products from HV Polo
try {
$products = $this->client->call($this->session_id, 'ipad_api.customerproducts', [null, $this->DefaultCategoryID, $this->storeID]);
if ($products) {
$this->load->model('api/hvpolo');
$counter = 0;
$new = '';
$duration = 0;
$i=0;
$max_aantal_producten_toevoegen = 50;
$max_products = $max_aantal_producten_toevoegen;
$mailsturen = '';
$message = '<html><body>';
$message .= '<table rules="all" style="border-color: #666;" cellpadding="10">';
$message .= "Er zijn nieuwe producten toegevoegd. ";
foreach ($products as $product) if ($i < $max_products) {
// Check if product exists
if (!$this->model_api_hvpolo->checkProduct($product)) {
$details = $this->client->call($this->session_id, 'ipad_api.customerproduct', [$product['product_id'], $this->storeID]);
if ($details['customer_group_price']['special_price'] > 0 ) {
$special = "<span style='color:red;'> Ja </span>";
}
else {
$special = " Nee";
}
if ($details) {
$keyword = str_replace(' ', '-', strtolower($details['name']));
// Add product if (does not exist)
$mailsturen = 'yes';
$message .= "<tr style='background: #eee;'><td><strong>Naam:</strong> </td><td><a href='https://rsruitersport.nl/clone2/".$keyword."'>".$details['name']."</a></td></tr>";
$message .= "<tr><td><strong>Product id: </strong> </td><td>".$details['product_id']."</td></tr>";
if (isset($details['customer_group_price']['website_price'])) {
$message .= "<tr><td><strong>Product (advies) Prijs: </strong> </td><td>€ ". $details['customer_group_price_rrp']['website_price'] ."</td></tr>";
}
if ($details['customer_group_price']['special_price'] > 0) {
$message .= "<tr><td><strong>Product special price: </strong> </td><td><span style='color:red;font-width:bold;'> € ". $details['customer_group_price']['special_price'] ." </span></td></tr>";
}
$starttime = microtime(true);
$this->model_api_hvpolo->addProduct($details);
$endtime = microtime(true);
$duration += ($endtime - $starttime);
$counter++;
$i+=1;
}
}
}
if ($mailsturen == 'yes') {
$email = "[email protected]\r\n";
$subject = 'Er zijn '.$counter.' nieuwe producten toegevoegd aan HV Polo';
$headers = "From: [email protected]\r\n";
$headers .= "Reply-To: [email protected]\r\n";
$headers .= "CC: [email protected], [email protected], [email protected]\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
$message .= "<tr><td><strong> </strong> </td><td></td></tr>";
$message .= "<tr><td><strong>Stastistiekjes </strong> </td><td></td></tr>";
$message .= "<tr><td><strong>Aantal producten toegevoegd: </strong> </td><td>".$counter."</td></tr>";
$message .= "<tr><td><strong>En dat duurde: </strong> </td><td>".$duration." ms</td></tr>";
$message .= "</table>";
$message .= "</body></html>";
if ($counter > 0) {
mail($email, $subject, $message, $headers);
$this->db->query("INSERT INTO " . DB_PREFIX . "api_log SET type = 'import_new', aantal_producten = '". $counter ."', duration = '" . $duration . "', datetime = NOW(), site = 'Hv Polo'");
}
// Duration to DB table
$this->db->query("INSERT INTO " . DB_PREFIX . "api_log SET type = 'import_new', aantal_producten = '". $counter ."', duration = '" . $duration . "', datetime = NOW(), site = 'Hv Polo'");
}
echo "<h2> Er zijn geen producten meer.</h2>";
}
} catch(Exception $e) {
foreach ($this->emails as $email) {
mail($email, 'HV Polo API error', $e->getMessage());
}
}
}
public function update() {
$this->client = new SoapClient('http://website.com/api/soap/?wsdl');
$this->session_id = $this->client->login($this->user, $this->key);
try {
$products = $this->client->call($this->session_id, 'ipad_api.customerproducts', [null, $this->DefaultCategoryID, $this->storeID]);
if ($products) {
$this->load->model('api/hvpolo');
$counter = 0;
$new = '';
$duration = 0;
$i=0;
$max_aantal_producten_toevoegen = 1;
$max_products = $max_aantal_producten_toevoegen;
foreach ($products as $product) if ($i < $max_products) {
if (!$this->model_api_hvpolo->checkProduct2($product)) {
$details = $this->client->call($this->session_id, 'ipad_api.customerproduct', [$product['product_id'], $this->storeID]);
if ($details) {
if (isset($details['customer_group_price']['special_price'])) {
}
$starttime = microtime(true);
$this->model_api_hvpolo->updateProduct($details);
$endtime = microtime(true);
$duration += ($endtime - $starttime);
$counter++;
// echo "<pre>";
// print_r($product);
// print_r($details);
// echo "</pre>";
$new .= ''. $details['name']. '\r\n';
$new .= '<br />';
$i+=1;
}
}
}
if ($new) {
//echo "dit is gedaan?";
if ($counter > 0) {
$this->db->query("INSERT INTO " . DB_PREFIX . "api_log SET type = 'update', aantal_producten = '". $new ."', duration = '" . $duration . "', datetime = NOW(), site = 'Hv Polo'");
}
// Duration to DB table
$this->db->query("INSERT INTO " . DB_PREFIX . "api_log SET type = 'update', aantal_producten = '". $new ."', duration = '" . $duration . "', datetime = NOW(), site = 'Hv Polo'");
}
}
echo "dit is 2x";
} catch(Exception $e) {
foreach ($this->emails as $email) {
mail($email, 'HV Polo API error', $e->getMessage());
}
}
}
public function update_orgineel() {
$this->client = new SoapClient('http://website.com/api/soap/?wsdl');
$this->session_id = $this->client->login($this->user, $this->key);
//Hoeveel dagen geschiedenis moet de update draaien?
$aantal_dagen_update = '-100 day';
// Get all products from HV Polo with updated stock
try {
$products = $this->client->call($this->session_id, 'ipad_api.customerstock', [$this->DefaultCategoryID, $this->storeID, 1]);
if ($products) {
$this->load->model('api/hvpolo');
$duration = 0;
foreach ($products as $product) {
$starttime = microtime(true);
$this->model_api_hvpolo->updateProduct($product);
$endtime = microtime(true);
$duration += ($endtime - $starttime);
}
// Duration to DB table
$this->db->query("INSERT INTO " . DB_PREFIX . "api_log SET type = 'import_update', aantal_producten = '". $new ."', duration = '" . $duration . "', datetime = NOW(), site = 'main'");
}
} catch(Exception $e) {
foreach ($this->emails as $email) {
mail($email, 'API error', $e->getMessage());
}
}
}
public function clean() {
$this->client = new SoapClient('http://website.com/api/soap/?wsdl');
$this->session_id = $this->client->login($this->user, $this->key);
//Hoeveel dagen geschiedenis moet de update draaien?
$aantal_dagen_update = '-300 day';
try {
$products = $this->client->call($this->session_id, 'ipad_api.customerstock', [$this->DefaultCategoryID, $this->storeID, 1, ['stock_last_change' => date('Y-m-d', strtotime('".$aantal_dagen_update."')) . ' 00:00:00']]);
if ($products) {
$this->load->model('api/hvpolo');
$duration = 0;
foreach ($products as $product) {
$starttime = microtime(true);
$this->model_api_hvpolo->deleteProduct($product);
$endtime = microtime(true);
$duration += ($endtime - $starttime);
}
// Duration to DB table
$this->db->query("INSERT INTO " . DB_PREFIX . "api_log SET type = 'import_clean', aantal_producten = '". $new ."', duration = '" . $duration . "', datetime = NOW(), site = 'hv polo'");
}
} catch(Exception $e) {
foreach ($this->emails as $email) {
mail($email, 'HV Polo API error', $e->getMessage());
}
}
}
}
?>
moet ik nog meer code posten ? dan zal ik dat doen.