Advertisement
rejuancse

wpmm-grid-woocommerce

Nov 27th, 2018
207
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 19.54 KB | None | 0 0
  1. <?php
  2. add_action('widgets_init','register_wpmm_grid_post_woocommerce_widget');
  3. function register_wpmm_grid_post_woocommerce_widget(){
  4.     register_widget('wpmm_grid_woocommerce_widget');
  5. }
  6.  
  7. class wpmm_grid_woocommerce_widget extends WP_Widget{
  8.  
  9.     function __construct(){
  10.         parent::__construct( 'wpmm_grid_woocommerce_widget','WPMM Grid WooCommerce',array('description' => 'Grid WooCommerce widget to display in WP Mega Menu'));
  11.     }
  12.  
  13.     /*-------------------------------------------------------
  14.      *              Front-end display of widget
  15.      *-------------------------------------------------------*/
  16.     function widget($args, $instance)
  17.     {
  18.         extract($args);
  19.  
  20.         $instance_title = isset($instance['title']) ? $instance['title'] : '';
  21.         $title = apply_filters('widget_title', $instance_title);
  22.  
  23.         if ( isset($instance['count']) ) {
  24.             $count  = $instance['count'];
  25.         } else {
  26.             $count  = 4;
  27.         }
  28.         if ( isset($instance['no_column']) ) {
  29.             $no_column  = $instance['no_column'];
  30.         } else {
  31.             $no_column  = 'col4';
  32.         }
  33.  
  34.         echo $args['before_widget'];
  35.  
  36.         $output = '';
  37.  
  38.         if ( $title )
  39.             echo $args['before_title'] . esc_attr($title) . $args['after_title'];
  40.  
  41.         global $post;
  42.  
  43.         if ( !empty($instance['order_by']) && $instance['order_by'] == 'popular') {
  44.             $args = array(
  45.                 'post_type'         => 'product',
  46.                 'posts_per_page'    => esc_attr($count),
  47.                 'meta_key'          => 'wpmm_postgrid_views',
  48.                 'orderby'           => 'meta_value_num',
  49.                 'post_status'       => 'publish',
  50.                 'order'             => 'DESC'
  51.             );
  52.         } else {
  53.            
  54.             $args = array(
  55.                 'post_type' => 'product',
  56.                 'showposts' => $count,
  57.                 'tax_query' => array(
  58.                                     array(
  59.                                         'taxonomy' => 'product_cat',
  60.                                         'field'    => 'slug',
  61.                                         'terms'    => $instance,
  62.                                     ),
  63.                                 ),
  64.                 'order'         => 'ASC',
  65.                 'post_status'   => 'publish'
  66.             );
  67.         }
  68.  
  69.         if( ! empty($instance['show_cat']) && $instance['show_cat'] == 'on' ){
  70.  
  71.             if( !empty($instance['category']) ){
  72.                 $output .='<div class="wpmm-vertical-tabs">';
  73.                 $output .='<div class="wpmm-vertical-tabs-nav">';
  74.                 $output .='<ul class="wpmm-tab-btns">';
  75.                 $i = 1;
  76.                 foreach ( $instance['category'] as $value ) {
  77.                     $catName = __('All Post','wp-megamenu');
  78.                     if( $value != 'allpost' ){
  79.                         //$catObj = get_term_by( 'name', $value , 'product_cat' );
  80.                         $catObj = get_term_by( 'slug', $value , 'product_cat' );
  81.                         if( isset($catObj->name) ){ $catName = $catObj->name; }
  82.                     }
  83.                     if( $i==1 ){
  84.                         $output .='<li class="active"><a href="#">'.$catName.'</a></li>';
  85.                     } else {
  86.                         $output .='<li class=""><a href="#">'.$catName.'</a></li>';
  87.                     }
  88.                     $i++;
  89.                 }
  90.                 $output .='</ul>';
  91.                 $output .='</div>';
  92.                 $output .='<div class="wpmm-vertical-tabs-content">';
  93.                 $output .='<div class="wpmm-tab-content">';
  94.                 $i = 1;
  95.                 foreach ( $instance['category'] as $value ) {
  96.                     if( $value ){
  97.                         $cat_data = array();
  98.                         $cat_data['relation'] = 'AND';
  99.                         if( 'allpost' != $value ){
  100.                             $cat_data[] = array(
  101.                                 'taxonomy'  => 'product_cat',
  102.                                 'field'     => 'slug',
  103.                                 'terms'     => $value
  104.                             );
  105.                         }
  106.                         $args['tax_query'] = $cat_data;
  107.                     }
  108.                     $data = new WP_Query( $args );
  109.                     $output .='<div class="wpmm-tab-pane '.(($i==1)?"active":"").'">';
  110.                     if( $data->have_posts()){
  111.  
  112.                         $output .='<div class="wpmm-grid-post-addons wpmm-grid-post-row">';
  113.                         while ( $data->have_posts() ) {
  114.                             $data->the_post();
  115.                             $output .='<div class="wpmm-grid-post '.esc_attr($no_column).'">';
  116.                             $output .='<div class="wpmm-grid-post-content">';
  117.  
  118.                             if ( has_post_thumbnail() ) {
  119.                                 $img = wp_get_attachment_image_src( get_post_thumbnail_id( get_the_ID() ), 'large-thumb' );
  120.                                 $image ='style="background: url('.esc_url($img[0]).') no-repeat;background-size: cover;"';
  121.  
  122.                             }else {
  123.                                 $image ='style="background: #333;"';
  124.                             }
  125.  
  126.                             $output .='<div class="wpmm-grid-post-img-wrap">';
  127.  
  128.                             if (has_post_thumbnail(get_the_ID())){
  129.                                 $output .='<a href="'.get_permalink(get_the_ID()).'">';
  130.                                 $output .='<div class="wpmm-grid-post-img" '.$image.'>';
  131.                                 $output .= '</div>';
  132.                                 $output .= '</a>';
  133.                             }
  134.  
  135.                             if( $instance['show_category'] == 'on' ){
  136.                                 $output .= '<span class="post-in-image">';
  137.                                 $var = get_the_term_list( get_the_ID(), 'product_cat' );
  138.                                 if( !is_wp_error($var) ){
  139.                                     $output .= $var;
  140.                                 }
  141.                                 $output .= '</span>';
  142.                             }
  143.  
  144.                             $output .= '</div>';//wpmm-grid-post-img-wrap
  145.                             $output .= '<h4 class="grid-post-title"><a href="'.get_permalink().'">'. get_the_title() .'</a></h4>';
  146.                             //Regular And Sales Price
  147.                             $output .= '<span class="post-in-price">';
  148.                             $price  = get_post_meta( get_the_ID(), '_regular_price', true);
  149.                             if( $price ){
  150.                                 $output .= '<span class="post-regular-price">'.$price.' '.get_option('woocommerce_currency').'</span>';
  151.                             }
  152.                             $sale   = get_post_meta( get_the_ID(), '_sale_price', true);
  153.                             if( $sale ){
  154.                                 $output .= '<span class="post-sales-price">'.$sale.get_option('woocommerce_currency').'</span>';
  155.                             }
  156.                             $output .= '</span>';
  157.  
  158.                             $output .= '</div>'; //.wpmm-grid-post-content
  159.                             $output .= '</div>'; //.wpmm-grid-post
  160.                         }
  161.                         wp_reset_postdata();
  162.                         $output .= '</div>'; //wpmm-grid-post-addons
  163.  
  164.                         if( $instance['show_nav'] == 'on' ){
  165.                             $output .= '<span data-showcat="'.$instance['show_category'].'" data-type="woocommerce" data-category="'.$value.'" data-current="1" data-oderby="'.$instance["order_by"].'" data-column="'.$no_column.'"  data-total="'.$data->max_num_pages.'" class="dashicons dashicons-arrow-left-alt2 wpmm-left wpmm-gridcontrol-left disablebtn"></span>';
  166.                             $var = ($data->max_num_pages == 1)? 'disablebtn' : '';
  167.                             $output .= '<span data-showcat="'.$instance['show_category'].'" data-type="woocommerce" data-category="'.$value.'"  data-current="1" data-oderby="'.$instance["order_by"].'" data-column="'.$no_column.'"  data-total="'.$data->max_num_pages.'" class="dashicons dashicons-arrow-right-alt2 wpmm-right wpmm-gridcontrol-right '.$var.'"></span>';
  168.                         }
  169.                     }
  170.                     $output .='</div>';
  171.                     $i++;
  172.                 }
  173.  
  174.  
  175.                 $output .='</div>';
  176.                 $output .='</div>';
  177.                 $output .='</div>';
  178.             }
  179.  
  180.         } else {
  181.             if( isset($instance['category']) ){
  182.                 $cat_data = array();
  183.                 $cat_data['relation'] = 'AND';
  184.                 if( !in_array( 'allpost', $instance['category'] ) ){
  185.                     $cat_data[] = array(
  186.                         'taxonomy'  => 'product_cat',
  187.                         'field'     => 'slug',
  188.                         'terms'     => $instance['category']
  189.                     );
  190.                 }
  191.                 $args['tax_query'] = $cat_data;
  192.             }
  193.             $data = new WP_Query( $args );
  194.  
  195.             if( $data->have_posts()){
  196.                 $output .='<div class="wpmm-grid-post-addons wpmm-grid-post-row">';
  197.                 while ( $data->have_posts() ) {
  198.                     $data->the_post();
  199.                     $output .='<div class="wpmm-grid-post '.esc_attr($no_column).'">';
  200.                     $output .='<div class="wpmm-grid-post-content">';
  201.  
  202.                     if ( has_post_thumbnail() ) {
  203.                         $img = wp_get_attachment_image_src( get_post_thumbnail_id( get_the_ID() ), 'large-thumb' );
  204.                         $image ='style="background: url('.esc_url($img[0]).') no-repeat;background-size: cover;"';
  205.  
  206.                     }else {
  207.                         $image ='style="background: #333;"';
  208.                     }
  209.  
  210.                     $output .='<div class="wpmm-grid-post-img-wrap">';
  211.  
  212.                     if (has_post_thumbnail(get_the_ID())){
  213.                         $output .='<a href="'.get_permalink(get_the_ID()).'">';
  214.                         $output .='<div class="wpmm-grid-post-img" '.$image.'>';
  215.                         $output .= '</div>';
  216.                         $output .= '</a>';
  217.                     }
  218.  
  219.                     if( ! empty($instance['show_category']) && $instance['show_category'] == 'on' ){
  220.                         $output .= '<span class="post-in-image">';
  221.                         $var = get_the_term_list( get_the_ID(), 'product_cat' );
  222.                         if( !is_wp_error($var) ){
  223.                             $output .= $var;
  224.                         }
  225.                         $output .= '</span>';
  226.                     }
  227.  
  228.                     $output .= '</div>';//wpmm-grid-post-img-wrap
  229.  
  230.                     $output .= '<h4 class="grid-post-title"><a href="'.get_permalink().'">'. get_the_title() .'</a></h4>';
  231.  
  232.                     //Regular And Sales Price
  233.                     $output .= '<span class="post-in-price">';
  234.                     $price  = get_post_meta( get_the_ID(), '_regular_price', true);
  235.                     if( $price ){
  236.                         $output .= '<span class="post-regular-price">'.$price.' '.get_option('woocommerce_currency').'</span>';
  237.                     }
  238.                     $sale   = get_post_meta( get_the_ID(), '_sale_price', true);
  239.                     if( $sale ){
  240.                         $output .= '<span class="post-sales-price">'.$sale.get_option('woocommerce_currency').'</span>';
  241.                     }
  242.                     $output .= '</span>';
  243.                     $output .= '</div>'; //.wpmm-grid-post-content
  244.                     $output .= '</div>'; //.wpmm-grid-post
  245.                 }
  246.                 wp_reset_postdata();
  247.                 $output .= '</div>'; //wpmm-grid-post-addons
  248.  
  249.                 if( ! empty($instance['show_nav']) && $instance['show_nav'] == 'on' ){
  250.  
  251.                     $output .= '<span data-showcat="'.$instance['show_category'].'" data-type="woocommerce" data-category="'.implode(',',$instance['category']).'" data-current="1" data-oderby="'.$instance["order_by"].'" data-column="'.$no_column.'"  data-total="'.$data->max_num_pages.'" class="dashicons dashicons-arrow-left-alt2 wpmm-left wpmm-gridcontrol-left disablebtn"></span>';
  252.  
  253.                     $var = ($data->max_num_pages == 1)? 'disablebtn' : '';
  254.  
  255.                     $output .= '<span data-showcat="'.$instance['show_category'].'" data-type="woocommerce" data-category="'.implode(',',$instance['category']).'"  data-current="1" data-oderby="'.$instance["order_by"].'" data-column="'.$no_column.'"  data-total="'.$data->max_num_pages.'" class="dashicons dashicons-arrow-right-alt2 wpmm-right wpmm-gridcontrol-right '.$var.'"></span>';
  256.                 }
  257.             }
  258.         }
  259.  
  260.  
  261.         echo $output;
  262.         echo ! empty($args['after_widget']) ? $args['after_widget'] : '';
  263.     }
  264.  
  265.     function update( $new_instance, $old_instance )
  266.     {
  267.         $instance = $old_instance;
  268.  
  269.         $instance['title']          = strip_tags( $new_instance['title'] );
  270.         $instance['no_column']      = strip_tags( $new_instance['no_column'] );
  271.         $instance['order_by']       = strip_tags( $new_instance['order_by'] );
  272.         $instance['count']          = strip_tags( $new_instance['count'] );
  273.         $instance['category']       =  $new_instance['category'];
  274.         $instance['show_cat']       = strip_tags( $new_instance['show_cat'] );
  275.         $instance['show_nav']       = strip_tags( $new_instance['show_nav'] );
  276.         $instance['show_category']  = strip_tags( $new_instance['show_category'] );
  277.  
  278.  
  279.         return $instance;
  280.     }
  281.  
  282.  
  283.     function form($instance)
  284.     {
  285.         $defaults = array(
  286.             'title'         => 'Latest Posts',
  287.             'no_column'     => 'col4',
  288.             'order_by'      => 'latest',
  289.             'count'         => 4,
  290.             'category'      => 'allpost',
  291.             'show_cat'      => false,
  292.             'show_nav'      => false,
  293.             'show_category' => true
  294.         );
  295.         $instance = wp_parse_args( (array) $instance, $defaults );
  296.         ?>
  297.         <p>
  298.             <label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php esc_html_e('Widget Title', 'wp-megamenu'); ?></label>
  299.             <input id="<?php echo $this->get_field_id( 'title' ); ?>" name="<?php echo $this->get_field_name( 'title' ); ?>" value="<?php echo $instance['title']; ?>" style="width:100%;" />
  300.         </p>
  301.  
  302.         <p>
  303.             <label for="<?php echo $this->get_field_id( 'order_by' ); ?>"><?php esc_html_e('Ordered By', 'wp-megamenu'); ?></label>
  304.             <?php
  305.             $options = array(
  306.                 'popular'   => 'Popular',
  307.                 'latest'    => 'Latest',
  308.             );
  309.             if(isset($instance['order_by'])) $order_by = $instance['order_by'];
  310.             ?>
  311.             <select class="widefat" id="<?php echo $this->get_field_id( 'order_by' ); ?>" name="<?php echo $this->get_field_name( 'order_by' ); ?>">
  312.                 <?php
  313.                 $op = '<option value="%s"%s>%s</option>';
  314.  
  315.                 foreach ($options as $key=>$value ) {
  316.  
  317.                     if ($order_by === $key) {
  318.                         printf($op, $key, ' selected="selected"', $value);
  319.                     } else {
  320.                         printf($op, $key, '', $value);
  321.                     }
  322.                 }
  323.                 ?>
  324.             </select>
  325.         </p>
  326.  
  327.  
  328.         <p>
  329.             <label for="<?php echo $this->get_field_id( 'category' ); ?>"><?php esc_html_e('Select Product Category', 'wp-megamenu'); ?></label>
  330.             <?php
  331.             $options = array();
  332.             $options['allpost'] = 'All Category';
  333.             $query1 = get_terms( 'product_cat' );
  334.             if( $query1 ){
  335.                 foreach ( $query1 as $post ) {
  336.                     $options[ $post->slug ] = $post->name;
  337.                 }
  338.             }
  339.             if(!empty($instance['category'])){
  340.                 $category = $instance['category'];
  341.             } else {
  342.                 $category = array( 'allpost' );
  343.             }
  344.             ?>
  345.             <select multiple class="widefat" id="<?php echo $this->get_field_id( 'category' ); ?>" name="<?php echo $this->get_field_name( 'category' ); ?>[]">
  346.                 <?php
  347.                 $op = '<option value="%s"%s>%s</option>';
  348.                 foreach ($options as $key=>$value ) {
  349.                     if (in_array($key,$category)) {
  350.                         printf($op, $key, ' selected="selected"', $value);
  351.                     } else {
  352.                         printf($op, $key, '', $value);
  353.                     }
  354.                 }
  355.                 ?>
  356.             </select>
  357.         </p>
  358.  
  359.         <p>
  360.             <label for="<?php echo $this->get_field_id( 'no_column' ); ?>"><?php esc_html_e('Number of Column', 'wp-megamenu'); ?></label>
  361.             <?php
  362.             $options = array(
  363.                 'col1'  => 'Column 1',
  364.                 'col2'  => 'Column 2',
  365.                 'col3'  => 'Column 3',
  366.                 'col4'  => 'Column 4',
  367.                 'col5'  => 'Column 5',
  368.             );
  369.             if(isset($instance['no_column'])) $no_column = $instance['no_column'];
  370.             ?>
  371.             <select class="widefat" id="<?php echo $this->get_field_id( 'no_column' ); ?>" name="<?php echo $this->get_field_name( 'no_column' ); ?>">
  372.                 <?php
  373.                 $op = '<option value="%s"%s>%s</option>';
  374.  
  375.                 foreach ($options as $key=>$value ) {
  376.  
  377.                     if ($no_column === $key) {
  378.                         printf($op, $key, ' selected="selected"', $value);
  379.                     } else {
  380.                         printf($op, $key, '', $value);
  381.                     }
  382.                 }
  383.                 ?>
  384.             </select>
  385.         </p>
  386.         <p>
  387.             <label for="<?php echo $this->get_field_id( 'count' ); ?>"><?php esc_html_e('Post Count', 'wp-megamenu'); ?></label>
  388.             <input id="<?php echo $this->get_field_id( 'count' ); ?>" name="<?php echo $this->get_field_name( 'count' ); ?>" value="<?php echo $instance['count']; ?>" style="width:100%;" />
  389.         </p>
  390.  
  391.         <?php $show_category = isset( $instance['show_category'] ) ? (bool) $instance['show_category'] : false; ?>
  392.         <p>
  393.             <input class="checkbox" type="checkbox"<?php checked( $show_category ); ?> id="<?php echo $this->get_field_id( 'show_category' ); ?>" name="<?php echo $this->get_field_name( 'show_category' ); ?>" />
  394.             <label for="<?php echo $this->get_field_id( 'show_category' ); ?>"><?php _e( 'Show Category on the Product?' ); ?></label>
  395.         </p>
  396.  
  397.         <?php $show_nav = isset( $instance['show_nav'] ) ? (bool) $instance['show_nav'] : false; ?>
  398.         <p>
  399.             <input class="checkbox" type="checkbox"<?php checked( $show_nav ); ?> id="<?php echo $this->get_field_id( 'show_nav' ); ?>" name="<?php echo $this->get_field_name( 'show_nav' ); ?>" />
  400.             <label for="<?php echo $this->get_field_id( 'show_nav' ); ?>"><?php _e( 'Show Navigation on the Product?' ); ?></label>
  401.         </p>
  402.  
  403.         <?php $show_cat = isset( $instance['show_cat'] ) ? (bool) $instance['show_cat'] : false; ?>
  404.         <p>
  405.             <input class="checkbox" type="checkbox"<?php checked( $show_cat ); ?> id="<?php echo $this->get_field_id( 'show_cat' ); ?>" name="<?php echo $this->get_field_name( 'show_cat' ); ?>" />
  406.             <label for="<?php echo $this->get_field_id( 'show_cat' ); ?>"><?php _e( 'Show Left Category on the Widget?' ); ?></label>
  407.         </p>
  408.  
  409.         <?php
  410.     }
  411. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement