I'm trying to write a custom bulk importer for store locations but I am struggling with which action hook to use.
I have written it and tested it on a clean WordPress installation and it works fine there but when I try to use it alongside WooCommerce I get an error on every admin page.
I've traced the fault back and I think it is being caused by the filter hook I am trying to use.
add_filter('pre_update_option',[$this,'importLocations'],10,3);
My import method looks like this:
public function importLocations($newvalue,$option,$oldvalue){
if('stockist_settings'==$option && !empty($newvalue['fileimport'])){
$file=get_attached_file($newvalue['fileimport'][0]);
$locations=$this->parseCSVToArray($file);
if(!empty($locations)){
$prefix = $this->getAttributes()['slug']."_";
foreach($locations as $location){
// Checks that the longitude and latitude are filled out
if(empty($location['Lat']) || empty($location['Lng'])){
$geocoder=new Geocoder();
$lnglat = $geocoder->geocodePostcode($location['Postcode']);
$location['Lat']=$lnglat['latitude'];
$location['Lng']=$lnglat['longitude'];
}
$social=$this->parseSocialMedia($location);
$v = new Valitron\Validator($location);
$v->rules($this->setImportRules());
if($v->validate()) {
$payload=[
'post_title' => $location['CompanyName'],
'post_type' => $this->getAttributes()['slug'],
'post_status' => 'publish',
'meta_input' => [
$prefix.'address1' => $location['Address1'],
$prefix.'address2' => $location['Address2'],
$prefix.'address3' => $location['Address3'],
$prefix.'city' => $location['City'],
$prefix.'postcode' => $location['Postcode'],
$prefix.'phonenumber' => $location['PhoneNumber'],
$prefix.'website' => $location['Website'],
$prefix.'email' => $location['Email'],
$prefix.'longitude' => $location['Lng'],
$prefix.'latitude' => $location['Lat'],
$prefix.'socialrepeat_group'=>$social
]
];
$id=wp_insert_post($payload);
} else {
// Errors
print_r($v->errors());
}
}
wp_redirect(admin_url('/edit.php?post_type=stockists'),301);
}
}
}
Is there a better filter hook this I should use or am I approaching the bulk import from the wrong direction entirely?
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745077925a4609944.html
评论列表(0条)