Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /*
- WPB Woocommerce Product Slider pro
- By WPBean
- */
- if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
- // register shortcode for latest product
- function wpb_wps_shortcode($atts){
- extract(shortcode_atts(array(
- 'title' => '', // Slider title
- 'theme' => 'grid cs-style-3', // theme [ Options: grid cs-style-3, ben-box, ]
- 'nav' => 'true', // show navigation
- 'prev' => "<i class='fa fa-angle-left'></i>", // prev icon
- 'next' => "<i class='fa fa-angle-right'></i>", // next icon
- 'autoplay' => 'true', // slider auto play
- 'stop' => 'true', // slider stop on mouse hover
- 'pagination' => 'false', // slider pagination
- 'count' => 'false', // slider pagination counting
- 'items' => 4, // Number of product on default screen
- 'items_desktop' => 3, // Number of product on screen size 1199px
- 'items_desktop_small' => 3, // Number of product on screen size 979px
- 'items_tablet' => 2, // Number of product on screen size 768px
- 'items_mobile' => 1, // Number of product on screen size 479px
- 'speed' => 1000, // slider speed
- 'paginationSpeed' => 1000, // paginationSpeed speed
- 'width' => '320', // Product image width
- 'height' => '300', // Product image height
- 'crop' => 'true', // Product image hard crop
- 'product_type' => 'latest', // latest, featured, category, id, skus, on_sell
- 'featured' => '', // yes for featured products
- 'skus' => '', // product skus comma separated
- 'category' => '', // product category comma separated
- 'id' => '',
- 'orderby' => 'none', // product orderby
- 'order' => '', // product order
- 'posts' => 10, // Number of products
- ), $atts));
- $wpb_wps_slider_id = rand(); // random id for each slider
- $products_id = ($id ? explode(',', $id) : null); // product id explode in array
- $wpb_wps_has_title_class = ( $title && $title != '' ? 'wpb_slider_has_title' : 'wpb_slider_no_title' );
- $return_string = '<div class="wpb_slider_area wpb_fix_cart '.$wpb_wps_has_title_class.'">';
- if( $title && !empty($title) ){
- $return_string .= '<h2 class="wpb_area_title">'.$title.'</h2>';
- }
- $return_string .= '<div class="wpb_woo_slider wpb_woo_slider_'.$wpb_wps_slider_id.' owl-carousel '.$theme.'">';
- $args = array(
- 'post_type' => 'product',
- 'posts_per_page' => $posts,
- 'product_cat' => $category,
- 'post__in' => $products_id,
- 'orderby' => $orderby,
- 'order' => $order,
- );
- // Woo meta featured post check
- if ( $featured == 'yes' ) {
- $args['meta_query'][] = array(
- 'key' => '_featured',
- 'value' => $featured,
- );
- }
- if($product_type == 'featured'){
- $args['meta_query'][] = array(
- 'key' => '_featured',
- 'value' => 'yes',
- );
- }
- // Woo meta sku check
- if ( isset( $atts['skus'] ) ) {
- $skus = explode( ',', $atts['skus'] );
- $skus = array_map( 'trim', $skus );
- $args['meta_query'][] = array(
- 'key' => '_sku',
- 'value' => $skus,
- 'compare' => 'IN'
- );
- }
- // On Sell Products
- if( $product_type == 'on_sell' ) {
- $args['meta_query'][] = array(
- 'relation' => 'OR',
- array( // Simple products type
- 'key' => '_sale_price',
- 'value' => 0,
- 'compare' => '>',
- 'type' => 'numeric'
- ),
- array( // Variable products type
- 'key' => '_min_variation_sale_price',
- 'value' => 0,
- 'compare' => '>',
- 'type' => 'numeric'
- )
- );
- }
- // Out of stock Products
- $wpb_wps_show_out_of_stock = wpb_wps_get_option( 'wpb_wps_show_out_of_stock', 'wpb_wps_general', 'no' );
- if( $wpb_wps_show_out_of_stock == 'no' ){
- $args['meta_query'][] = array(
- 'key' => '_stock_status',
- 'value' => 'outofstock',
- 'compare' => 'NOT IN'
- );
- }
- $loop = new WP_Query( $args );
- if ( $loop->have_posts() ) {
- while ( $loop->have_posts() ) : $loop->the_post();
- global $post, $woocommerce, $product;
- $price_html = $product->get_price_html();
- $wpb_wps_show_ribon = wpb_wps_get_option( 'wpb_wps_show_ribon', 'wpb_wps_general', 'yes' );
- $return_string .= '<div class="item product_id_'.$product->id.' product_type_'.$product->product_type.' ' .implode( get_post_class()," " ). '">';
- if ( $product->is_on_sale() && $wpb_wps_show_ribon == 'yes' ) {
- $return_string .= apply_filters( 'woocommerce_sale_flash', '<span class="onsale">' . __( 'Sale!', 'wpb-woocommerce-product-slider' ) . '</span>', $post, $product );
- }
- $return_string .= '<figure>';
- $return_string .= '<a href="'.get_permalink().'" class="wpb_pro_img_url">';
- // Resize Image
- $pro_thumb = get_post_thumbnail_id();
- $pro_img_url = wp_get_attachment_url( $pro_thumb,'full' ); //get full URL
- $pro_image = aq_resize( $pro_img_url, $width, $height, $crop ); //resize & crop the image
- $pro_alt_text = get_post_meta($pro_thumb , '_wp_attachment_image_alt', true); // image alt text
- $wpb_wps_show_category = wpb_wps_get_option( 'wpb_wps_show_category', 'wpb_wps_general', 'yes' );
- $wpb_wps_show_price = wpb_wps_get_option( 'wpb_wps_show_price', 'wpb_wps_general', 'yes' );
- $wpb_wps_show_title = wpb_wps_get_option( 'wpb_wps_show_title', 'wpb_wps_general', 'yes' );
- $wpb_wps_show_cart = wpb_wps_get_option( 'wpb_wps_show_cart', 'wpb_wps_general', 'yes' );
- $wpb_wps_mouse_drag = wpb_wps_get_option( 'wpb_wps_mouse_drag', 'wpb_wps_slider_settings', 'yes' );
- $wpb_wps_touch_drag = wpb_wps_get_option( 'wpb_wps_touch_drag', 'wpb_wps_slider_settings', 'yes' );
- $wpb_wps_auto_crop_images = wpb_wps_get_option( 'wpb_wps_auto_crop_images', 'wpb_wps_slider_settings', 'yes' );
- if( $wpb_wps_auto_crop_images == 'yes' ){
- if (has_post_thumbnail( $loop->post->ID )){
- $return_string .= '<img src="'.$pro_image.'" alt="'.$pro_alt_text.'" class="wpb_pro_img" />';
- }else{
- $return_string .= '<img class="wpb_pro_img" src="'. apply_filters( 'wpb_wps_placeholder_img_src', 'http://placehold.it/'.$width.'x'.$height.'' ) .'" alt="Placeholder" />';
- }
- }else {
- $return_string .= woocommerce_get_product_thumbnail();
- }
- $return_string .='</a>';
- $return_string .='<figcaption>';
- if( $wpb_wps_show_category && $wpb_wps_show_category == 'yes' && $theme == 'ben-box'){
- $return_string .='<span class="wpb_pro_cat">'.get_the_term_list(get_the_id(), 'product_cat', '', ', ').'</span>';
- } // Showing Product Categories
- if( $price_html && $wpb_wps_show_price == 'yes' ){
- $return_string .='<span class="price wpb_wl_product_price">'. $price_html .'</span>';
- } // Showing Product Price
- $return_string .='</figcaption>';
- $return_string .= '</figure>';
- $return_string .='<div class="product-caption">';
- if( $wpb_wps_show_title && $wpb_wps_show_title == 'yes'){
- $return_string .='<h3 class="pro_title"><a href="'.get_permalink().'">';
- if (strlen($post->post_title) > 20) {
- $return_string .= substr(the_title($before = '', $after = '', FALSE), 0, wpb_wps_get_option( 'wpb_title_mx_ch', 'wpb_wps_general', 10 )) . '...';
- }else{
- $return_string .= get_the_title();
- }
- $return_string .='</a></h3>';
- } // Showing Product Title
- if( $wpb_wps_show_cart && $wpb_wps_show_cart == 'yes' ){
- $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>';
- }
- $return_string .='</div>';
- $return_string .= '</div>';
- endwhile;
- } else {
- echo __( 'No products found' );
- }
- wp_reset_postdata();
- $return_string .= '</div>';
- $return_string .= '</div>';
- if ( empty($autoplay) ) {
- $autoplay = 'true';
- }
- if( empty($nav) ){
- $nav = 'true';
- }
- if( empty($pagination) ){
- $pagination = 'false';
- }
- if( empty($count) ){
- $count = 'false';
- }
- $wpb_wps_rtl_direction = '';
- if( is_rtl() ){
- $wpb_wps_rtl_direction = "direction:'rtl',";
- }
- $return_string .= '<script type="text/javascript">
- jQuery(".wpb_woo_slider_'.$wpb_wps_slider_id.'").owlCarousel({
- autoPlay: '.$autoplay.',
- stopOnHover: '.$stop.',
- navigation: '.$nav.',
- navigationText: ["'.$prev.'","'.$next.'"],
- slideSpeed: '.$speed.',
- paginationSpeed: '.$paginationSpeed.',
- pagination: '.$pagination.',
- paginationNumbers: '.$count.',
- items : '.$items.',
- itemsDesktop : [1199,'.$items_desktop.'],
- itemsDesktopSmall : [979,'.$items_desktop_small.'],
- itemsTablet : [768,'.$items_tablet.'],
- itemsMobile : [479,'.$items_mobile.'],
- scrollPerPage: true,
- mouseDrag: '.( $wpb_wps_mouse_drag == 'yes' ? 'true' : 'false' ).',
- touchDrag: '.( $wpb_wps_touch_drag == 'yes' ? 'true' : 'false' ).',
- '.$wpb_wps_rtl_direction.'
- });
- </script>';
- wp_reset_query();
- return $return_string;
- }
- add_shortcode('wpb-product-slider', 'wpb_wps_shortcode');
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement