Ustabilt php script, virker for det meste Hjælp :)
HejJeg har været igang med at lave et php script med hjælp her inde fra. det virker fint for det meste og får ikke memory fejl, men nogle gange køre den ikke loopet færdig, så stopper den pludselig.
Kan ikke se fejlen kan i?
Koden:
global $wpdb;
$con = $wpdb->__get('dbh');
$stmt1 = $con->prepare("SELECT ID, post_type FROM nw_posts WHERE post_type = 'product'");
$stmt1->execute();
$rs1 = $stmt1->get_result();
$lng = $rs1->num_rows+200;
$sql = "SELECT distinct ID ,post_id, post_type, meta_key, meta_value, post_title, guid, post_modified, post_parent FROM **_postmeta
INNER JOIN **_posts ON post_id = ID WHERE post_type ='product' AND meta_key in ( '_sku') GROUP BY post_title LIMIT ".$lng." ";
$rs = $con->query($sql);
$array = array();
$brand = "";
$meta_stock = "";
$xml_file=fopen("***_nyhedsbrev.xml","w");
$xml = "<?xml version='1.0' encoding='UTF-8'?>\n";
$xml .= "<items>\n";
$i = 0;
while ($value = $rs->fetch_array(MYSQLI_ASSOC)) {
$array[] = $value;
$i++;
$product = wc_get_product($value['ID']);
$stock = $product->get_stock_status();
if ($stock == 'instock') {
if ($value['meta_key'] == "_sku") {
global $meta_sku;
$meta_sku = $value['meta_value'];
}
if (is_array(get_the_terms($value['ID'], 'product_brand'))) {
foreach (get_the_terms($value['ID'], 'product_brand') as $item) {
global $brand;
$brand = $item->name;
}
}
if (is_array(get_the_terms($value['ID'], 'product_cat'))) {
foreach (get_the_terms($value['ID'], 'product_cat') as $item) {
global $koenet;
$koenet = $item->parent;
}
}
global $koen;
$koen = $koenet;
$image = get_the_post_thumbnail_url($value['ID']);
if (is_array(get_the_terms($value['ID'], 'product_cat'))) {
foreach (get_the_terms($value['ID'], 'product_cat') as $term) {
if ($term->parent == 0) {
$koen = $term->name;
}
}
}
$post_title = $value['post_title'];
$guid = get_permalink($value['ID']);
$post_modified = $value['post_modified'];
$post_parent = $value['post_id'];
$product = wc_get_product($value['ID']);
if ($product->is_type('variable')) {
// Min variation price
$regular_price = $product->get_variation_regular_price(); // Min regular price
$sale_price = $product->get_variation_sale_price(); // Min sale price
if($regular_price == $sale_price) {
$meta_regular_price = $product->get_variation_regular_price(); // Min regular price
$meta_sale_price = "";
} elseif ($sale_price != $regular_price) {
$meta_regular_price = $product->get_variation_regular_price(); // Min regular price
$meta_sale_price = $product->get_variation_sale_price(); // Min sale price
}
elseif ($sale_price == false) {
$meta_regular_price = $product->get_variation_regular_price(); // Min regular price
}
}
if ($stock == "outofstock") {
$meta_stock = 0;
} elseif ($stock == "instock") {
$meta_stock = 1;
}
global $meta_sku;
$xml = "<item>\n"; // start Item
$xml .= "<ExternalId>". $meta_sku. "</ExternalId>\n";
$xml .= "<Modified>". $post_modified. "</Modified>\n";
$xml .= "<Name>". htmlspecialchars($post_title, ENT_QUOTES) ."</Name>\n";
$xml .= "<ImageLink>". $image ."</ImageLink>\n";
$xml .= "<AdditionalImageLink>". $image. "</AdditionalImageLink>\n";
$xml .= "<Link>". $guid ."</Link>\n";
$xml .= "<Gender>". $koen ."</Gender>\n";
$xml .= "<Brand>". htmlspecialchars($brand, ENT_QUOTES) ."</Brand>\n";
$xml .= "<InStock>". $meta_stock ."</InStock>\n";
$xml .= "<ItemGroupId>". $post_parent . "</ItemGroupId>\n";
$xml .= "<RegularPrice>". $meta_regular_price ."</RegularPrice>\n";
$xml .= "<SalePrice>". $meta_sale_price . "</SalePrice>\n";
$xml .= "</item>\n";
fwrite($xml_file, $xml);
}
}
$xml .= "</items>";
fwrite($xml_file, $xml);
fclose($xml_file);
Jeg valgte at lave den uden XMLwirter da det blev mere stabilt, og blev udført.
Men har stadig nogle problemer.
Med venlig hilsen
Morten