Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //put this in functions.php, then go to yoursite.com/?runmagic=1
- //Run on Init
- add_filter("init", "magical_variation_function");
- function magical_variation_function() {
- //If the ?runmagic=1 isn't there, skip this
- if (!isset($_GET['runmagic'])) return;
- //Name of Variation You Want To Add
- $reference_name = ""; //Reference Name <-- must match the saved variation you are targeting
- //Setup Query to Determine Which Products We Are Grabbing
- $args = array(
- "post_type" => "foxyshop_product",
- "posts_per_page" => -1, //no paging
- //if you want to clarify which products, do so here
- //"foxyshop_categories" => 'sub-cat-1', //slug of product category
- //"page_id" => 1, //just one product
- //"post__in" => array(1, 2, 3), // does products 1, 2, and 3
- );
- //You can ignore the rest of this
- //----------------------------------
- //Get Saved Variations to make sure it exists
- $saved_variations = get_option('foxyshop_saved_variations');
- $skip = 1;
- foreach($saved_variations as $saved_variation) {
- if ($reference_name === $saved_variation['refname']) {
- $skip = 0;
- $variation_name = $saved_variation['name'];
- }
- }
- if ($skip) {
- echo '<span style="color: red">' . $reference_name . " Not a Valid Variation</span><br>\n";
- die;
- }
- //Now let's run the query and loop through all the products returned
- $the_query = new WP_Query($args);
- while ($the_query->have_posts()) {
- //Init the Product
- $skip = 0;
- $the_query->the_post();
- $product = foxyshop_setup_product();
- $variations = $product['variations'];
- //Does the variation already exist?
- foreach ($variations as $variation) {
- if ($variation['type'] === sanitize_title($reference_name)) {
- $skip = 1;
- }
- }
- if ($skip) {
- echo '<span style="color: gray">' . get_the_title() . " Variation Already In Place</span><br>\n";
- continue;
- }
- //Build the New Variation
- $variations[count($variations)] = array(
- "name" => $variation_name,
- "type" => sanitize_title($reference_name),
- "value" => "",
- "displayKey" => "",
- "required" => "",
- );
- update_post_meta($product['id'], "_variations", $variations);
- echo get_the_title() . " Variation Added<br>\n";
- }
- die("All Done");
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement