Advertisement
sparkweb

FoxyShop: Connection to Self-Hosted Script

Dec 16th, 2011
468
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.89 KB | None | 0 0
  1. //Show New Field in Admin
  2. add_action('foxyshop_admin_product_details','my_custom_file_download_link');
  3. function my_custom_file_download_link($post_id) {
  4.     $_download_link = get_post_meta($post_id,'_download_link',1);
  5.     ?>
  6.     <div style="clear:both;"></div>
  7.     <div class="foxyshop_field_control">
  8.         <select name="_download_link" id="_download_link" style="max-width: 256px; margin-top: 6px;">
  9.         <option value="">- - Selected File - -</option>
  10.         <?php
  11.         if ($handle = opendir('/YOUR/FILE/DOWNLOAD/PATH/HERE/')) {
  12.             $thefile = array();
  13.             while (false !== ($file = readdir($handle))) {
  14.                 if ($file != "." && $file != "..") $thefile[] = $file;
  15.             }
  16.             closedir($handle);
  17.             sort($thefile);
  18.             foreach($thefile as $filename) {
  19.                 echo '<option value="'.$filename.'"' . ($_download_link == $filename ? ' selected="selected"' : "") . '>'.$filename.'</option>' . "\n";
  20.             }
  21.         }
  22.             ?>
  23.         </select>
  24.        
  25.     </div>
  26.     <div style="clear:both"></div>
  27.     <?php
  28. }
  29.  
  30. //Save New Field
  31. add_action('foxyshop_save_product','my_custom_file_download_link_save');
  32. function my_custom_file_download_link_save($post_id) {
  33.     global $post_id, $wpdb;
  34.     foxyshop_save_meta_data('_download_link', $_POST['_download_link']);
  35.  
  36.     //Sync with Self Hosted Script
  37.     $table_prefix = "Foxy"; //UPDATE HERE IF YOU USED SOMETHING OTHER THAN "Foxy"
  38.    
  39.     //Get Values
  40.     $product_code = $post_id;
  41.     $product_name = get_the_title($post_id);
  42.     $price = get_post_meta($post_id,'_price',1);
  43.     $file_name = $_POST['_download_link'];
  44.     $file_location = $_POST['_download_link'];
  45.  
  46.     // Check if the product already exists.
  47.     $product_result = $wpdb->get_row("SELECT `product_id`, `product_code`, `file_location` FROM `" . $table_prefix . "Product` WHERE `product_code` = '$product_code';");
  48.    
  49.     // Edit existing.
  50.    
  51.     if ($product_result) {
  52.         $query = "
  53.             UPDATE `" . $table_prefix . "Product` SET
  54.                 product_name    = '".mysql_real_escape_string($product_name)."',
  55.                 product_code    = '".mysql_real_escape_string($product_code)."',
  56.                 price       = '".mysql_real_escape_string($price)."',
  57.                 file_name   = '".mysql_real_escape_string($file_name)."',
  58.                 file_location   = '".mysql_real_escape_string($file_location)."'
  59.             WHERE `product_code` = '$product_code';
  60.         ";
  61.         $wpdb->query($query);
  62.         $output = "<div class=\"productRow\">&raquo; Updated <em>$product_name</em> ($product_code)</div>\n";
  63.    
  64.     // Add
  65.     } else {
  66.         // Add as new.
  67.         $query = "
  68.             INSERT INTO `" . $table_prefix . "Product` SET
  69.                 product_code    = '".mysql_real_escape_string($product_code)."',
  70.                 product_name    = '".mysql_real_escape_string($product_name)."',
  71.                 price       = '".mysql_real_escape_string($price)."',
  72.                 file_name   = '".mysql_real_escape_string($file_name)."',
  73.                 file_location   = '".mysql_real_escape_string($file_location)."'
  74.         ";
  75.         $wpdb->query($query);
  76.         $output = "<div class=\"productRow\">&raquo; Added <em>$product_name</em> ($product_code)</div>\n";
  77.     }
  78.     return $post_id;
  79. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement