我创建了一个自定义函数,用于读取XML文件并将其导入woocommerce产品。XML文件每天由外部平台在网站ftp中导出。
我将wordpress cronjob替换为真正的cronjob,cronjob实际上调用了函数,但函数从未执行过(参见屏幕截图)。
我的职能:
<?php
/**
* DynamiX Child Theme Functions
* Load languages directory for translation
*/
function XML_import() {
$dir = $_SERVER[\'DOCUMENT_ROOT\'] . \'import_pw/\';
$files1 = scandir($dir);
$files = array_diff($files1, array(\'.\', \'..\'));
foreach($files as $file) {
$filename = $dir . $file;
if (file_exists($filename))
{
$time = strtotime(\'2010-04-28 17:25:43\');
$time = filemtime($filename);
$elapsedTime = time()-filemtime($filename);
if( $elapsedTime < (24*3600) ) {
$xml = simplexml_load_file($filename) or die ("Error, cannot create object");
foreach($xml->Machine as $Machine) {
if (!get_page_by_title($Machine->OMSCHR1, OBJECT, \'product\')) :
// Empty array
$attributes = array();
if($Machine->BOUWJAAR != 0) {
// Bouwjaar Attribute
$attribute = new WC_Product_Attribute();
$attribute->set_id(0);
$attribute->set_name(\'Bouwjaar\');
$attribute->set_options(array($Machine->BOUWJAAR));
$attribute->set_position(1);
$attribute->set_visible(true);
$attribute->set_variation(false);
$attributes[\'bouwjaar\'] = $attribute;
}
if($Machine->MERK != \'\') {
// Merk Attribute
$attribute = new WC_Product_Attribute();
$attribute->set_id(0);
$attribute->set_name(\'Merk\');
$attribute->set_options(array($Machine->MERK));
$attribute->set_position(0);
$attribute->set_visible(true);
$attribute->set_variation(false);
$attributes[\'merk\'] = $attribute;
}
if($Machine->configuratie->NRR_1034 != \'\') {
// Brandstof Attribute
$attribute = new WC_Product_Attribute();
$attribute->set_id(0);
$attribute->set_name(\'Brandstof\');
$attribute->set_options(array($Machine->configuratie->NRR_1034));
$attribute->set_position(2);
$attribute->set_visible(true);
$attribute->set_variation(false);
$attributes[\'brandstof\'] = $attribute;
}
if($Machine->URENST1 != 0) {
// Draaiuren Attribute
$attribute = new WC_Product_Attribute();
$attribute->set_id(0);
$attribute->set_name(\'Draaiuren\');
$attribute->set_options(array($Machine->URENST1));
$attribute->set_position(3);
$attribute->set_visible(true);
$attribute->set_variation(false);
$attributes[\'draaiuren\'] = $attribute;
}
$description = \'\';
if($Machine->TYPE != \'\') {
$description .= \'Type: \' . $Machine->TYPE . \'<br>\';
}
if($Machine->MERKCODE != \'\') {
$description .= \'NO: \' . $Machine->MERKCODE . \'<br>\';
}
if($Machine->KENTEKEN != \'\') {
$description .= \'Kenteken: \' . $Machine->KENTEKEN . \'<br>\';
}
if($Machine->BOUWJAAR != 0) {
$description .= \'Bouwjaar: \' . $Machine->BOUWJAAR . \'<br>\';
}
if($Machine->URENST1 != 0) {
$description .= \'Urenstaat: \' . $Machine->URENST1 . \'<br>\';
}
if($Machine->configuratie->NRR_1034 != \'\') {
$description .= \'Brandstof: \' . $Machine->configuratie->NRR_1034 . \'<br>\';
}
if($Machine->configuratie->NRR_1093 != \'\') {
$description .= \'Hefvermogen: \' . $Machine->configuratie->NRR_1093 . \'<br>\';
}
if($Machine->configuratie->NRR_1035 != \'\') {
$description .= \'Hefhoogte: \' . $Machine->configuratie->NRR_1035 . \'<br>\';
}
if($Machine->configuratie->NRR_1026 != \'\') {
$description .= \'Voorzetapparatuur: \' . $Machine->configuratie->NRR_1026 . \'<br>\';
}
if($Machine->configuratie->NRR_1020 != \'\') {
$description .= \'Cabine: \' . $Machine->configuratie->NRR_1020 . \'<br>\';
}
if($Machine->configuratie->NRR_1008 != \'\') {
$description .= \'Extra hydrauliek ventielen: \' . $Machine->configuratie->NRR_1008 . \'<br>\';
}
$base64_img = $Machine->IMAGE;
// Upload dir.
$upload_dir = wp_upload_dir();
$upload_path = str_replace( \'/\', DIRECTORY_SEPARATOR, $upload_dir[\'path\'] ) . DIRECTORY_SEPARATOR;
$img = str_replace( \'data:image/jpeg;base64,\', \'\', $base64_img );
$img = str_replace( \' \', \'+\', $img );
$decoded = base64_decode( $img );
$filename = $title . \'.jpeg\';
$file_type = \'image/jpeg\';
$hashed_filename = md5( $filename . microtime() ) . \'_\' . $filename;
// Save the image in the uploads directory.
$upload_file = file_put_contents( $upload_path . $hashed_filename, $decoded );
$attachment = array(
\'post_mime_type\' => $file_type,
\'post_title\' => preg_replace( \'/\\.[^.]+$/\', \'\', basename( $hashed_filename ) ),
\'post_content\' => \'\',
\'post_status\' => \'inherit\',
\'guid\' => $upload_dir[\'url\'] . \'/\' . basename( $hashed_filename )
);
$attach_id = wp_insert_attachment( $attachment, $upload_dir[\'path\'] . \'/\' . $hashed_filename );
var_dump($attach_id);
// Set woocommerce product
$getters_and_setters = array(\'name\' => $Machine->OMSCHR1, \'slug\' => $Machine->OMSCHR1, \'category_ids\' => array(70),\'description\' => $description, \'image_id\' => $attach_id, \'attributes\' => $attributes);
$product = new WC_Product();
foreach ($getters_and_setters as $function => $value) {
$product->{"set_{$function}"}($value);
}
// Save product
$product->save();
endif;
}
}
}
}
}
// Schedule Cron Job Event
function custom_cron_job() {
if ( ! wp_next_scheduled( \'XML_import\' ) ) {
wp_schedule_event( time(), \'hourly\', \'XML_import\' );
}
}
add_action( \'wp\', \'custom_cron_job\' );
我在wp配置中添加了。php:
define(\'DISABLE_WP_CRON\', true);
有人能帮我吗?我被困在这件事上了。
谢谢伦斯