我尝试使用MySQL和PHP(v7。*)以XML格式从WooCommerce导出产品。我使用代码创建了自定义表格并添加了产品:
INSERT INTO `xml_export`(`id`, `product_name`, `product_link`) SELECT p.id, p.post_title, p.guid FROM wp_posts p WHERE p.post_type='product';
UPDATE xml_export, wp_postmeta
SET xml_export.product_price=wp_postmeta.meta_value
WHERE wp_postmeta.meta_key='_price'
AND wp_postmeta.post_id=xml_export.id;
UPDATE xml_export, wp_postmeta
SET xml_export.product_image=wp_postmeta.meta_key
WHERE wp_postmeta.meta_key='_thumbnail_id'
AND wp_postmeta.post_id=xml_export.id;
之后我使用Google guide创建了php文件。
我的PHP代码是(我已从此代码段中排除了连接数据):
<?php /*here comes connection data*/ ?>
<?php
require("phpsqlajax_dbinfo.php");
function parseToXML($htmlStr)
{
$xmlStr=str_replace('<','<',$htmlStr);
$xmlStr=str_replace('>','>',$xmlStr);
$xmlStr=str_replace('"','"',$xmlStr);
$xmlStr=str_replace("'",''',$xmlStr);
$xmlStr=str_replace("&",'&',$xmlStr);
return $xmlStr;
}
// Opens a connection to a MySQL server
$connection=mysql_connect ('localhost', $username, $password);
if (!$connection) {
die('Not connected : ' . mysql_error());
}
// Set the active MySQL database
$db_selected = mysql_select_db($database, $connection);
if (!$db_selected) {
die ('Can\'t use db : ' . mysql_error());
}
// Select all the rows in the table
$query = "SELECT * FROM xml_export WHERE 1";
$result = mysql_query($query);
if (!$result) {
die('Invalid query: ' . mysql_error());
}
header("Content-type: text/xml");
// Start XML file, echo parent node
echo '<items>';
// Iterate through the rows, printing XML nodes for each
while ($row = @mysql_fetch_assoc($result)){
// Add to XML document node
echo '<item ';
echo 'name="' . parseToXML($row['product_name']) . '" ';
echo 'link="' . parseToXML($row['product_link']) . '" ';
echo 'price="' . $row['product_price'] . '" ';
echo 'image="' . $row['product_image'] . '" ';
echo '/>';
}
// End XML file
echo '</items>';
?>
但是当我启动代码时没有任何反应。没有有趣的XML与产品列表!任何想法如何获得我需要的产品?