Advertisement
imranmodel32

wpb-wps-shortcodes.php

Jan 20th, 2016
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 9.54 KB | None | 0 0
  1. <?php
  2.  
  3. /*
  4.     WPB Woocommerce Product Slider pro
  5.     By WPBean
  6.    
  7. */
  8.  
  9. if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
  10.  
  11. // register shortcode for latest product
  12.  
  13. function wpb_wps_shortcode($atts){
  14.     extract(shortcode_atts(array(
  15.           'title'               => '', // Slider title
  16.           'theme'               => 'grid cs-style-3', // theme [ Options:  grid cs-style-3, ben-box, ]
  17.           'nav'                 => 'true', // show navigation
  18.           'prev'                => "<i class='fa fa-angle-left'></i>", // prev icon
  19.           'next'                => "<i class='fa fa-angle-right'></i>", // next icon
  20.           'autoplay'            => 'true', // slider auto play
  21.           'stop'                => 'true', // slider stop on mouse hover
  22.           'pagination'          => 'false', // slider pagination
  23.           'count'               => 'false', // slider pagination counting
  24.           'items'               => 4, // Number of product on default screen
  25.           'items_desktop'       => 3, // Number of product on screen size 1199px
  26.           'items_desktop_small' => 3, // Number of product on screen size 979px
  27.           'items_tablet'        => 2, // Number of product on screen size 768px
  28.           'items_mobile'        => 1, // Number of product on screen size 479px
  29.           'speed'               => 1000, // slider speed
  30.           'paginationSpeed'     => 1000, // paginationSpeed speed
  31.           'width'               => '320', // Product image width
  32.           'height'              => '300', // Product image height
  33.           'crop'                => 'true', // Product image hard crop
  34.           'product_type'        => 'latest', // latest, featured, category, id, skus, on_sell
  35.           'featured'            => '', // yes for featured products
  36.           'skus'                => '', // product skus comma separated
  37.           'category'            => '', // product category comma separated
  38.           'id'                  => '',
  39.           'orderby'             => 'none', // product orderby
  40.           'order'               => '', // product order
  41.           'posts'               => 10, // Number of products
  42.        ), $atts));
  43.    
  44.     $wpb_wps_slider_id = rand(); // random id for each slider
  45.     $products_id = ($id ? explode(',', $id) : null); // product id explode in array
  46.     $wpb_wps_has_title_class = ( $title && $title != '' ? 'wpb_slider_has_title' : 'wpb_slider_no_title' );
  47.  
  48.     $return_string = '<div class="wpb_slider_area wpb_fix_cart '.$wpb_wps_has_title_class.'">';
  49.     if( $title && !empty($title) ){
  50.         $return_string .= '<h2 class="wpb_area_title">'.$title.'</h2>';
  51.     }
  52.     $return_string .= '<div class="wpb_woo_slider wpb_woo_slider_'.$wpb_wps_slider_id.' owl-carousel '.$theme.'">';
  53.  
  54.     $args = array(
  55.         'post_type'         => 'product',
  56.         'posts_per_page'    => $posts,
  57.         'product_cat'       => $category,
  58.         'post__in'          => $products_id,
  59.         'orderby'           => $orderby,
  60.         'order'             => $order,
  61.     );
  62.    
  63.     // Woo meta featured post check
  64.     if ( $featured == 'yes' ) {
  65.         $args['meta_query'][] = array(
  66.             'key'       => '_featured',
  67.             'value'     => $featured,
  68.         );
  69.     }
  70.  
  71.     if($product_type == 'featured'){
  72.         $args['meta_query'][] = array(
  73.             'key'       => '_featured',
  74.             'value'     => 'yes',
  75.         );
  76.     }
  77.  
  78.    
  79.     // Woo meta sku check
  80.     if ( isset( $atts['skus'] ) ) {
  81.         $skus = explode( ',', $atts['skus'] );
  82.         $skus = array_map( 'trim', $skus );
  83.         $args['meta_query'][] = array(
  84.             'key'       => '_sku',
  85.             'value'     => $skus,
  86.             'compare'   => 'IN'
  87.         );
  88.     }
  89.  
  90.     // On Sell Products
  91.     if( $product_type == 'on_sell' ) {
  92.  
  93.         $args['meta_query'][] = array(
  94.             'relation' => 'OR',
  95.             array( // Simple products type
  96.                 'key'           => '_sale_price',
  97.                 'value'         => 0,
  98.                 'compare'       => '>',
  99.                 'type'          => 'numeric'
  100.             ),
  101.             array( // Variable products type
  102.                 'key'           => '_min_variation_sale_price',
  103.                 'value'         => 0,
  104.                 'compare'       => '>',
  105.                 'type'          => 'numeric'
  106.             )
  107.         );
  108.     }
  109.  
  110.     // Out of stock Products
  111.  
  112.     $wpb_wps_show_out_of_stock = wpb_wps_get_option( 'wpb_wps_show_out_of_stock', 'wpb_wps_general', 'no' );
  113.     if( $wpb_wps_show_out_of_stock == 'no' ){
  114.         $args['meta_query'][] = array(
  115.             'key'       => '_stock_status',
  116.             'value'     => 'outofstock',
  117.             'compare'   => 'NOT IN'
  118.         );
  119.     }
  120.  
  121.                    
  122.     $loop = new WP_Query( $args );
  123.         if ( $loop->have_posts() ) {
  124.             while ( $loop->have_posts() ) : $loop->the_post();
  125.  
  126.                 global $post, $woocommerce, $product;
  127.                 $price_html = $product->get_price_html();
  128.                 $wpb_wps_show_ribon = wpb_wps_get_option( 'wpb_wps_show_ribon', 'wpb_wps_general', 'yes' );
  129.  
  130.                 $return_string .= '<div class="item product_id_'.$product->id.' product_type_'.$product->product_type.' ' .implode( get_post_class()," " ). '">';
  131.  
  132.                 if ( $product->is_on_sale() &&  $wpb_wps_show_ribon == 'yes' ) {
  133.                     $return_string .= apply_filters( 'woocommerce_sale_flash', '<span class="onsale">' . __( 'Sale!', 'wpb-woocommerce-product-slider' ) . '</span>', $post, $product );
  134.                 }
  135.  
  136.                 $return_string .= '<figure>';
  137.                 $return_string .= '<a href="'.get_permalink().'" class="wpb_pro_img_url">';
  138.                 // Resize Image
  139.                 $pro_thumb = get_post_thumbnail_id();
  140.                 $pro_img_url = wp_get_attachment_url( $pro_thumb,'full' ); //get full URL
  141.                 $pro_image = aq_resize( $pro_img_url, $width, $height, $crop ); //resize & crop the image
  142.                 $pro_alt_text = get_post_meta($pro_thumb , '_wp_attachment_image_alt', true); // image alt text
  143.                 $wpb_wps_show_category = wpb_wps_get_option( 'wpb_wps_show_category', 'wpb_wps_general', 'yes' );
  144.                 $wpb_wps_show_price = wpb_wps_get_option( 'wpb_wps_show_price', 'wpb_wps_general', 'yes' );
  145.                 $wpb_wps_show_title = wpb_wps_get_option( 'wpb_wps_show_title', 'wpb_wps_general', 'yes' );
  146.                 $wpb_wps_show_cart = wpb_wps_get_option( 'wpb_wps_show_cart', 'wpb_wps_general', 'yes' );
  147.                 $wpb_wps_mouse_drag = wpb_wps_get_option( 'wpb_wps_mouse_drag', 'wpb_wps_slider_settings', 'yes' );
  148.                 $wpb_wps_touch_drag = wpb_wps_get_option( 'wpb_wps_touch_drag', 'wpb_wps_slider_settings', 'yes' );
  149.                 $wpb_wps_auto_crop_images = wpb_wps_get_option( 'wpb_wps_auto_crop_images', 'wpb_wps_slider_settings', 'yes' );
  150.  
  151.  
  152.  
  153.                 if( $wpb_wps_auto_crop_images == 'yes' ){
  154.                     if (has_post_thumbnail( $loop->post->ID )){
  155.                         $return_string .= '<img src="'.$pro_image.'" alt="'.$pro_alt_text.'" class="wpb_pro_img" />';
  156.                     }else{
  157.                         $return_string .= '<img class="wpb_pro_img" src="'. apply_filters( 'wpb_wps_placeholder_img_src', 'http://placehold.it/'.$width.'x'.$height.'' ) .'" alt="Placeholder" />';
  158.                     }
  159.                    
  160.                 }else {
  161.                     $return_string .= woocommerce_get_product_thumbnail();
  162.                 }
  163.  
  164.                
  165.                 $return_string .='</a>';
  166.                
  167.                 $return_string .='<figcaption>';
  168.  
  169.                 if( $wpb_wps_show_category && $wpb_wps_show_category == 'yes' && $theme == 'ben-box'){
  170.                     $return_string .='<span class="wpb_pro_cat">'.get_the_term_list(get_the_id(), 'product_cat', '', ',&nbsp;').'</span>';
  171.                 } // Showing Product Categories
  172.  
  173.  
  174.                 if( $price_html &&  $wpb_wps_show_price == 'yes' ){
  175.                     $return_string .='<span class="price wpb_wl_product_price">'. $price_html .'</span>';
  176.                 } // Showing Product Price
  177.  
  178.                 $return_string .='</figcaption>';
  179.                 $return_string .= '</figure>';
  180.  
  181.                 $return_string .='<div class="product-caption">';
  182.                     if( $wpb_wps_show_title && $wpb_wps_show_title == 'yes'){
  183.                         $return_string .='<h3 class="pro_title"><a href="'.get_permalink().'">';
  184.                         if (strlen($post->post_title) > 20) {
  185.                             $return_string .= substr(the_title($before = '', $after = '', FALSE), 0, wpb_wps_get_option( 'wpb_title_mx_ch', 'wpb_wps_general', 10 )) . '...';
  186.                         }else{
  187.                             $return_string .= get_the_title();
  188.                         }
  189.                         $return_string .='</a></h3>';
  190.                     } // Showing Product Title
  191.  
  192.                     if( $wpb_wps_show_cart && $wpb_wps_show_cart == 'yes' ){
  193.  
  194.                         $return_string .= '<div class="price_area_fix"><a href="'.esc_url( $product->add_to_cart_url() ).'" rel="nofollow" data-product_id="'.esc_attr( $product->id ).'" data-product_sku="'.esc_attr( $product->get_sku() ).'" data-quantity="'.esc_attr( isset( $quantity ) ? $quantity : 1 ).'" class="button '. ($product->is_purchasable() && $product->is_in_stock() ? 'add_to_cart_button' : '') .' product_type_'.esc_attr( $product->product_type ).'">'.esc_html( $product->add_to_cart_text()).'</a></div>';
  195.                     }
  196.                 $return_string .='</div>';
  197.  
  198.                 $return_string .= '</div>';
  199.          
  200.             endwhile;
  201.         } else {
  202.             echo __( 'No products found' );
  203.         }
  204.         wp_reset_postdata();
  205.            
  206.         $return_string .= '</div>';
  207.         $return_string .= '</div>';
  208.  
  209.  
  210.         if ( empty($autoplay) ) {
  211.             $autoplay = 'true';
  212.         }
  213.         if( empty($nav) ){
  214.             $nav = 'true';
  215.         }
  216.         if( empty($pagination) ){
  217.             $pagination = 'false';
  218.         }
  219.         if( empty($count) ){
  220.             $count = 'false';
  221.         }
  222.  
  223.         $wpb_wps_rtl_direction = '';
  224.         if( is_rtl() ){
  225.             $wpb_wps_rtl_direction = "direction:'rtl',";
  226.         }
  227.  
  228.         $return_string .= '<script type="text/javascript">
  229.             jQuery(".wpb_woo_slider_'.$wpb_wps_slider_id.'").owlCarousel({
  230.                 autoPlay: '.$autoplay.',
  231.                 stopOnHover: '.$stop.',
  232.                 navigation: '.$nav.',
  233.                 navigationText: ["'.$prev.'","'.$next.'"],
  234.                 slideSpeed: '.$speed.',
  235.                 paginationSpeed: '.$paginationSpeed.',
  236.                 pagination: '.$pagination.',
  237.                 paginationNumbers: '.$count.',
  238.                 items : '.$items.',
  239.                 itemsDesktop : [1199,'.$items_desktop.'],
  240.                 itemsDesktopSmall : [979,'.$items_desktop_small.'],
  241.                 itemsTablet : [768,'.$items_tablet.'],
  242.                 itemsMobile : [479,'.$items_mobile.'],
  243.                 scrollPerPage: true,
  244.                 mouseDrag: '.( $wpb_wps_mouse_drag == 'yes' ? 'true' : 'false' ).',
  245.                 touchDrag: '.( $wpb_wps_touch_drag == 'yes' ? 'true' : 'false' ).',
  246.                 '.$wpb_wps_rtl_direction.'
  247.             });
  248.         </script>';
  249.  
  250.         wp_reset_query();
  251.         return $return_string;
  252. }
  253.  
  254. add_shortcode('wpb-product-slider', 'wpb_wps_shortcode');
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement