Advertisement
sparkweb

FoxyShop: Put Saved Variation in All Products

Feb 4th, 2015
379
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.23 KB | None | 0 0
  1. //put this in functions.php, then go to yoursite.com/?runmagic=1
  2.  
  3. //Run on Init
  4. add_filter("init", "magical_variation_function");
  5. function magical_variation_function() {
  6.  
  7.     //If the ?runmagic=1 isn't there, skip this
  8.     if (!isset($_GET['runmagic'])) return;
  9.  
  10.     //Name of Variation You Want To Add
  11.     $reference_name = ""; //Reference Name <-- must match the saved variation you are targeting
  12.  
  13.     //Setup Query to Determine Which Products We Are Grabbing
  14.     $args = array(
  15.         "post_type" => "foxyshop_product",
  16.         "posts_per_page" => -1, //no paging
  17.        
  18.         //if you want to clarify which products, do so here
  19.         //"foxyshop_categories" => 'sub-cat-1', //slug of product category
  20.         //"page_id" => 1, //just one product
  21.         //"post__in" => array(1, 2, 3), // does products 1, 2, and 3
  22.     );
  23.  
  24.  
  25.  
  26.  
  27.     //You can ignore the rest of this
  28.     //----------------------------------
  29.  
  30.     //Get Saved Variations to make sure it exists
  31.     $saved_variations = get_option('foxyshop_saved_variations');
  32.     $skip = 1;
  33.     foreach($saved_variations as $saved_variation) {
  34.         if ($reference_name === $saved_variation['refname']) {
  35.             $skip = 0;
  36.             $variation_name = $saved_variation['name'];
  37.         }
  38.     }
  39.     if ($skip) {
  40.         echo '<span style="color: red">' . $reference_name . " Not a Valid Variation</span><br>\n";
  41.         die;
  42.     }
  43.  
  44.     //Now let's run the query and loop through all the products returned
  45.     $the_query = new WP_Query($args);
  46.     while ($the_query->have_posts()) {
  47.  
  48.         //Init the Product
  49.         $skip = 0;
  50.         $the_query->the_post();
  51.         $product = foxyshop_setup_product();
  52.         $variations = $product['variations'];
  53.  
  54.         //Does the variation already exist?
  55.         foreach ($variations as $variation) {
  56.             if ($variation['type'] === sanitize_title($reference_name)) {
  57.                 $skip = 1;
  58.             }
  59.         }
  60.         if ($skip) {
  61.             echo '<span style="color: gray">' . get_the_title() . " Variation Already In Place</span><br>\n";
  62.             continue;
  63.         }
  64.  
  65.         //Build the New Variation
  66.         $variations[count($variations)] = array(
  67.             "name" => $variation_name,
  68.             "type" => sanitize_title($reference_name),
  69.             "value" => "",
  70.             "displayKey" => "",
  71.             "required" => "",
  72.         );
  73.  
  74.         update_post_meta($product['id'], "_variations", $variations);
  75.         echo get_the_title() . " Variation Added<br>\n";
  76.     }
  77.  
  78.  
  79.     die("All Done");
  80. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement