Created
June 13, 2013 16:19
-
-
Save Darkwing371/5775079 to your computer and use it in GitHub Desktop.
BULKSET SWATCH TYPE OPTIONS
FOR WOOCOMMERCE’S PLUGIN 'VARIATION SWATCHES AND PHOTOS' • It’s 'a one-time action'
• Run the script inside of a test plugin, or wrap it in an AJAX action, or … – you decide BUT FIRST:
• Read the code
• Understand the code
• Make it you own
• Use it in a test environment
• If it acts like expected: use it in your live…
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* BULKSET PRODUCT SWATCH OPTIONS | |
/* FOR WOOCOMMERCE’S PLUGIN 'VARIATION SWATCHES AND PHOTOS' */ | |
/* PROVIDE THE DESIRED TAXONOMY WHERE SWATCH OPTION CHANGE SHALL APPEAR */ | |
$taxonomy = 'pa_yourdesiredtaxname'; | |
/* NOTE FOR SAFETY REASONS */ | |
/* DECOMMENT THE TWO '$res=…' IN THE LOOP BELOW */ | |
/* TO ACTUALLY PERFORM THE DATABASE OPERATION */ | |
/* SET SWATCH TYPE OPTION HERE – USUALLY 'TAXONOMY COLORS AND IMAGES' */ | |
//$swatchtype = 'default'; /* None */ | |
//$swatchtype = 'product_custom'; /* Custom Product Colors and Images */ | |
$swatchtype_options = 'term_options'; /* Taxonomy Colors and Images */ | |
$swatchtype = 'pickers'; /* OR: default, when default above */ | |
echo '<h1>' . $taxonomy .' ⇉ ' . $swatchtype_options .'/'. $swatchtype . '</h2>'; | |
<br><br>'; | |
// Get all product ids | |
// With a little helper function; see below */ | |
$ids = get_all_products(); | |
// For every product id do | |
foreach ($ids as $id) | |
{ | |
// Read from postmeta meta_key _swatch_type_options | |
$query = "SELECT meta_value | |
FROM $wpdb->postmeta | |
WHERE post_id = $id | |
AND meta_key = '_swatch_type_options'"; | |
$res = $wpdb->get_results($query, ARRAY_A); | |
// Sanitize and unserialize array | |
$res = maybe_unserialize($res['0']['meta_value']); | |
// Make change to swatchtype | |
if ( array_key_exists($taxonomy, $res) ) { | |
$res[$taxonomy]['type'] = $swatchtype_options; | |
} | |
$res = maybe_serialize($res); | |
// UPDATE option in postmeta | |
$query = "UPDATE $wpdb->postmeta | |
SET meta_value = '$res' | |
WHERE post_id = $id | |
AND meta_key = '_swatch_type_options'"; | |
//$res = $wpdb->get_results($query); /* DECOMMENT */ | |
// UPDATE pickers | |
$query = "UPDATE $wpdb->postmeta | |
SET meta_value = '$swatchtype' | |
WHERE post_id = $id | |
AND meta_key = '_swatch_type'"; | |
//$res = $wpdb->get_results($query); /* DECOMMENT */ | |
echo 'id ' .$id . ' updated.<br>'; | |
} | |
/* HELPER FUNCTIONS */ | |
// Gets the product ids | |
function get_all_products($what = 'id') { | |
global $wpdb; | |
// Select the right posts/products of interest | |
$query = "SELECT $what | |
FROM $wpdb->posts | |
WHERE post_type = 'product' | |
AND ( post_status = 'publish' | |
OR post_status = 'draft' ) | |
ORDER BY $wpdb->posts.ID ASC"; | |
$res = $wpdb->get_results($query, ARRAY_N); | |
// Sanitize DB result to a proper array | |
$result = array(); | |
foreach ( $res as $r ) { | |
$result[] = ( is_integerable($r[0]) ) ? (int) $r[0] : $r[0]; | |
} | |
return $result; | |
} | |
// Small integer test | |
function is_integerable( $v ){ | |
return is_numeric($v) && $v*1 == (int) ($v*1); | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment