Advertisement
sparkweb

FoxyShop: Select a Product As A Widget

Oct 21st, 2011
950
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 5.14 KB | None | 0 0
  1. <?php
  2. add_action('widgets_init', 'foxyshop_load_widgets_custom');
  3.  
  4. function foxyshop_load_widgets_custom() {
  5.     register_widget('FoxyShop_SingleProduct');
  6. }
  7.  
  8.  
  9. class FoxyShop_SingleProduct extends WP_Widget {
  10.  
  11.     //Widget Setup
  12.     function FoxyShop_SingleProduct() {
  13.         $widget_ops = array( 'classname' => 'foxyshop_single-rpduct', 'description' => __('Show a ') . strtolower(FOXYSHOP_PRODUCT_NAME_SINGULAR));
  14.         $control_ops = array( 'width' => 300, 'height' => 350, 'id_base' => 'foxyshop-single-product-widget' );
  15.         $this->WP_Widget( 'foxyshop-single-product-widget', __('FoxyShop ') . FOXYSHOP_PRODUCT_NAME_SINGULAR, $widget_ops, $control_ops );
  16.     }
  17.  
  18.     //Widget Display
  19.     function widget( $args, $instance ) {
  20.         extract( $args );
  21.  
  22.         //Our variables from the widget settings
  23.         $title = apply_filters('widget_title', $instance['title'] );
  24.         $productID = $instance['productID'];
  25.         $showMoreDetails = isset( $instance['showMoreDetails'] ) ? $instance['showMoreDetails'] : false;
  26.         $showAddToCart = isset( $instance['showAddToCart'] ) ? $instance['showAddToCart'] : false;
  27.  
  28.         echo $before_widget;
  29.         if ($title) echo $before_title . $title . $after_title;
  30.  
  31.         echo '<div class="foxyshop_category_widget">';
  32.  
  33.  
  34.  
  35.         global $product;
  36.         echo '<ul class="foxyshop_featured_product_list">';
  37.  
  38.         $args = array('post_type' => 'foxyshop_product', "p" => $productID);
  39.         $featuredlist = get_posts($args);
  40.         foreach($featuredlist as $featuredprod) {
  41.             $product = foxyshop_setup_product($featuredprod);
  42.             if ($product['hide_product']) continue;
  43.  
  44.             $thumbnailSRC = foxyshop_get_main_image("thumbnail");
  45.             echo '<li class="foxyshop_product_box">'."\n";
  46.             echo '<div class="foxyshop_product_image">';
  47.             echo '<a href="' . $product['url'] . '"><img src="' . $thumbnailSRC . '" alt="' . esc_attr($product['name']) . '" class="foxyshop_main_image" /></a>';
  48.             echo "</div>\n";
  49.  
  50.             echo '<div class="foxyshop_product_info">';
  51.             echo '<h2><a href="' . $product['url'] . '">' . apply_filters('the_title', $product['name']) . '</a></h2>';
  52.  
  53.             foxyshop_price();
  54.  
  55.             if ($showMoreDetails) echo '<a href="' . $product['url'] . '" class="foxyshop_button">' . __('More Details') . '</a>';
  56.             if ($showAddToCart) echo '<a href="' . foxyshop_product_link("", true) . '" class="foxyshop_button">' . __('Add To Cart') . '</a>';
  57.  
  58.             echo "</div>\n";
  59.             echo '<div class="clr"></div>';
  60.             echo "</li>\n";
  61.         }
  62.         echo "</ul><div class=\"clr\"></div>\n";
  63.  
  64.  
  65.  
  66.  
  67.         echo '</div>';
  68.  
  69.         echo $after_widget;
  70.     }
  71.  
  72.     //Update Widget Settings
  73.     function update( $new_instance, $old_instance ) {
  74.         $instance = $old_instance;
  75.  
  76.         /* Strip tags for title and name to remove HTML (important for text inputs). */
  77.         $instance['title'] = strip_tags( $new_instance['title'] );
  78.         $instance['productID'] = strip_tags( $new_instance['productID'] );
  79.  
  80.         /* No need to strip tags */
  81.         $instance['showAddToCart'] = $new_instance['showAddToCart'];
  82.         $instance['showMoreDetails'] = $new_instance['showMoreDetails'];
  83.  
  84.         return $instance;
  85.     }
  86.  
  87.     //Widget Control Panel
  88.     function form( $instance ) {
  89.  
  90.         //Defaults
  91.         $defaults = array(
  92.             'title' => "",
  93.             'productID' => "",
  94.             'showAddToCart' => "",
  95.             'showMoreDetails' => "on",
  96.         );
  97.         $instance = wp_parse_args( (array) $instance, $defaults ); ?>
  98.  
  99.         <!-- Widget Title: Text Input -->
  100.         <p>
  101.             <label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php _e('Title:'); ?></label>
  102.             <input id="<?php echo $this->get_field_id( 'title' ); ?>" name="<?php echo $this->get_field_name( 'title' ); ?>" value="<?php echo $instance['title']; ?>" style="width:260px;" />
  103.         </p>
  104.  
  105.         <!-- Select Product -->
  106.         <p>
  107.             <label for="<?php echo $this->get_field_id( 'productID' ); ?>"><?php _e('Product:'); ?></label>
  108.             <select id="<?php echo $this->get_field_id( 'productID' ); ?>" name="<?php echo $this->get_field_name( 'productID' ); ?>" class="widefat" style="width:100%;">
  109.                 <option value="">- - Select Product - -</option>
  110.                 <?php
  111.                
  112.                 $args = array('post_type' => 'foxyshop_product', 'post_status' => 'publish', 'posts_per_page' => -1, 'orderby' => "name", "order" => "ASC");
  113.                 $products = get_posts($args);
  114.                 foreach ($products as $theproduct) {
  115.                     echo '<option value="' . $theproduct->ID .'"';
  116.                     if ($instance['productID'] == $theproduct->ID) echo ' selected="selected"';
  117.                     echo '>' . $theproduct->post_title . '</option>';
  118.                 }
  119.                 ?>
  120.             </select>
  121.         </p>
  122.  
  123.         <!-- Show Checkboxes for Button Selection -->
  124.         <p>
  125.             <input class="checkbox" type="checkbox" <?php checked( $instance['showAddToCart'], 'on' ); ?> id="<?php echo $this->get_field_id( 'showAddToCart' ); ?>" name="<?php echo $this->get_field_name( 'showAddToCart' ); ?>" />
  126.             <label for="<?php echo $this->get_field_id( 'showAddToCart' ); ?>"><?php _e('Show Add To Cart Button'); ?></label>
  127.         </p>
  128.         <p>
  129.             <input class="checkbox" type="checkbox" <?php checked( $instance['showMoreDetails'], 'on' ); ?> id="<?php echo $this->get_field_id( 'showMoreDetails' ); ?>" name="<?php echo $this->get_field_name( 'showMoreDetails' ); ?>" />
  130.             <label for="<?php echo $this->get_field_id( 'showMoreDetails' ); ?>"><?php _e('Show More Details Button'); ?></label>
  131.         </p>
  132.  
  133.  
  134.     <?php
  135.     }
  136. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement