Ok I’v created a admin plugin to do this… If you have better idea 🙂 I’m in
Create a admin button to open a csv file of meta_value to compare with existing products. and update products as featured if exist.
add_action('admin_menu', 'featured_button_menu');
function featured_button_menu()
{
add_menu_page('Featured Products Page', 'Mise en avant produits', 'manage_options', 'featured-button-slug', 'featured_button_admin_page');
}
function featured_button_admin_page()
{
// General check for user permissions.
if (!current_user_can('manage_options'))
{
wp_die( __('You do not have sufficient pilchards to access this page.'));
}
// Start building the page
echo '<div class="wrap">';
echo '<h2>Mettre à jour les produits mise en avant (CSV)</h2>';
// Check whether the button has been pressed AND also check the nonce
if (isset($_POST['featured_button']) && check_admin_referer('featured_button_clicked'))
{
// the button has been pressed AND we've passed the security check
featured_button_action();
}
echo '<form action="options-general.php?page=featured-button-slug" method="post">';
// WordPress security feature
wp_nonce_field('featured_button_clicked');
echo '<input type="hidden" value="true" name="featured_button" />';
submit_button('Lancer la procédure');
echo '</form>';
echo '</div>';
}
function featured_button_action()
{
global $wpdb;
$filepath = $_SERVER['DOCUMENT_ROOT']."/data/featured.csv";
$cip_array = [];
$resInt = [];
$handle = fopen($filepath, "r") or die("Error opening file");
while(($line = fgetcsv($handle, 1000, ";")) !== FALSE) {
$cip_array[] = $line;
}
fclose($handle);
foreach($cip_array as $res) {
$resUTF8 = mb_convert_encoding($res[0], 'UTF-8');
$resInt[] = intval($resUTF8);
}
$List = implode(',', $resInt);
$products = $wpdb->get_results("SELECT phiz_posts.ID
FROM phiz_posts
JOIN phiz_postmeta ON ( phiz_posts.ID = phiz_postmeta.post_id )
JOIN phiz_term_relationships ON ( phiz_posts.ID = phiz_term_relationships.object_id )
JOIN phiz_term_taxonomy ON ( phiz_term_relationships.term_taxonomy_id = phiz_term_taxonomy.term_taxonomy_id )
JOIN phiz_terms ON ( phiz_term_taxonomy.term_id = phiz_terms.term_id )
WHERE phiz_posts.post_status="publish"
AND phiz_posts.post_type="product"
AND phiz_postmeta.meta_key = 'product_cip'
AND phiz_postmeta.meta_value IN (.$List.) ");
foreach ($products as $prd)
{
$term_taxonomy_ids = wp_set_object_terms( $prd->ID, 'featured', 'product_visibility' );
if ( is_wp_error( $term_taxonomy_ids ) ) {
echo "There was an error somewhere and the terms couldn't be set.<br>";
}
}
}