Skip to content

Instantly share code, notes, and snippets.

@juanfra
Created April 25, 2019 15:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save juanfra/5a0ba6d95103321daf94238390703241 to your computer and use it in GitHub Desktop.
Save juanfra/5a0ba6d95103321daf94238390703241 to your computer and use it in GitHub Desktop.
<?php
/**
* Smart by NiceThemes.
*
* This file contains functions to manage WooCommerce layout.
*
* @package Smart
* @author NiceThemes <hello@nicethemes.com>
* @license GPL-2.0+
* @link http://nicethemes.com/product/smart
* @copyright 2017 NiceThemes
* @since 1.0.0
*/
// If this file is called directly, abort.
if ( ! defined( 'WPINC' ) ) {
die;
}
if ( ! function_exists( 'nice_top_bar_login_shortcodes' ) ) :
add_filter( 'nice_top_bar_shortcodes', 'nice_woocommerce_integration_top_bar_shortcodes' );
/**
* Add shortcodes to top bar.
*
* @since 1.0.0
*
* @param array $shortcodes
*
* @return array
*/
function nice_woocommerce_integration_top_bar_shortcodes( array $shortcodes = array() ) {
$shortcodes['login'] = 'nice_woocommerce_integration_top_bar_login_shortcode';
$shortcodes['logout'] = 'nice_woocommerce_integration_top_bar_logout_shortcode';
$shortcodes['signup'] = 'nice_woocommerce_integration_top_bar_signup_shortcode';
$shortcodes['my_account'] = 'nice_woocommerce_integration_top_bar_my_account_shortcode';
return $shortcodes;
}
endif;
if ( ! function_exists( 'nice_woocommerce_integration_top_bar_login_shortcode' ) ) :
/**
* Process top bar login shortcode.
*
* @since 1.0.0
*
* @param array $atts
*
* @return string
*/
function nice_woocommerce_integration_top_bar_login_shortcode( array $atts = array() ) {
$content = '';
if ( ! is_user_logged_in() ) {
$text = empty( $atts['label'] ) ? esc_html__( 'Login', 'smart' ) : $atts['label'];
$icon_class = empty( $atts['icon_class'] ) ? 'fa-lock' : $atts['icon_class'];
ob_start();
?>
<div id="top-bar-login" class="top-bar-link">
<a href="<?php echo esc_url( get_permalink( wc_get_page_id( 'myaccount' ) ) ); ?>" title="<?php esc_html( $text ); ?>">
<?php if ( isset( $atts['icon'] ) && nice_bool( $atts['icon'] ) ) : ?>
<i class="fa <?php echo esc_attr( $icon_class ); ?>"></i>
<?php endif; ?>
<?php echo esc_html( $text ); ?>
</a>
</div>
<?php
$content = ob_get_contents();
ob_end_clean();
}
return $content;
}
endif;
if ( ! function_exists( 'nice_woocommerce_integration_top_bar_signup_shortcode' ) ) :
/**
* Process top bar signup shortcode.
*
* @since 1.0.0
*
* @param array $atts
*
* @return string
*/
function nice_woocommerce_integration_top_bar_signup_shortcode( array $atts = array() ) {
$content = '';
if ( ! is_user_logged_in() ) {
$text = empty( $atts['label'] ) ? esc_html__( 'Signup', 'smart' ) : $atts['label'];
$icon_class = empty( $atts['icon_class'] ) ? 'fa-user-plus' : $atts['icon_class'];
ob_start();
?>
<div id="top-bar-signup" class="top-bar-link">
<a href="<?php echo esc_url( get_permalink( wc_get_page_id( 'myaccount' ) ) ); ?>" title="<?php esc_html( $text ); ?>">
<?php if ( isset( $atts['icon'] ) && nice_bool( $atts['icon'] ) ) : ?>
<i class="fa <?php echo esc_attr( $icon_class ); ?>"></i>
<?php endif; ?>
<?php echo esc_html( $text ); ?>
</a>
</div>
<?php
$content = ob_get_contents();
ob_end_clean();
}
return $content;
}
endif;
if ( ! function_exists( 'nice_woocommerce_integration_top_bar_my_account_shortcode' ) ) :
/**
* Process top bar "My account" shortcode.
*
* @since 1.0.0
*
* @param array $atts
*
* @return string
*/
function nice_woocommerce_integration_top_bar_my_account_shortcode( array $atts = array() ) {
$content = '';
if ( is_user_logged_in() ) {
$text = empty( $atts['label'] ) ? esc_html__( 'My Account', 'smart' ) : $atts['label'];
$icon_class = empty( $atts['icon_class'] ) ? 'fa-user' : $atts['icon_class'];
ob_start();
?>
<div id="top-bar-my-account" class="top-bar-link">
<a href="<?php echo esc_url( get_permalink( wc_get_page_id( 'myaccount' ) ) ); ?>" title="<?php echo esc_html( $text ); ?>">
<?php if ( isset( $atts['icon'] ) && nice_bool( $atts['icon'] ) ) : ?>
<i class="fa <?php echo esc_attr( $icon_class ); ?>"></i>
<?php endif; ?>
<?php echo esc_html( $text ); ?>
</a>
</div>
<?php
$content = ob_get_contents();
ob_end_clean();
}
return $content;
}
endif;
if ( ! function_exists( 'nice_woocommerce_integration_top_bar_logout_shortcode' ) ) :
/**
* Process top bar logout shortcode.
*
* @since 1.0.0
*
* @param array $atts
*
* @return string
*/
function nice_woocommerce_integration_top_bar_logout_shortcode( array $atts = array() ) {
$content = '';
if ( is_user_logged_in() ) {
$text = empty( $atts['label'] ) ? esc_html__( 'Logout', 'smart' ) : $atts['label'];
$icon_class = empty( $atts['icon_class'] ) ? 'fa-power-off' : $atts['icon_class'];
ob_start();
?>
<div id="top-bar-logout" class="top-bar-link">
<a href="<?php echo esc_url( wp_logout_url( wc_get_page_id( 'myaccount' ) ) ); ?>" title="<?php esc_html( $text ); ?>">
<?php if ( isset( $atts['icon'] ) && nice_bool( $atts['icon'] ) ) : ?>
<i class="fa <?php echo esc_attr( $icon_class ); ?>"></i>
<?php endif; ?>
<?php echo esc_html( $text ); ?>
</a>
</div>
<?php
$content = ob_get_contents();
ob_end_clean();
}
return $content;
}
endif;
if ( ! function_exists( 'nice_woocommerce_integration_shop_title' ) ) :
add_filter( 'nice_page_title_args', 'nice_woocommerce_integration_shop_title' );
/**
* Use custom title for shop pages.
*
* @since 1.0.0
*
* @param array $args
*
* @return array
*/
function nice_woocommerce_integration_shop_title( array $args = array() ) {
if ( is_shop() ) {
$args['title'] = apply_filters( 'nice_woocommerce_integration_shop_title', woocommerce_page_title( false ) );
}
return $args;
}
endif;
if ( ! function_exists( 'nice_woocommerce_integration_page_title' ) ) :
add_action( 'template_redirect', 'nice_woocommerce_integration_page_title' );
/**
* Remove page heading and set a link back to shop
*
* @since 1.0.0
*/
function nice_woocommerce_integration_page_title() {
if ( ! is_product() ) {
return;
}
remove_action( 'nice_heading_title', 'nice_page_title' );
add_action( 'nice_heading_title', 'nice_woocommerce_integration_product_title' );
}
endif;
if ( ! function_exists( 'nice_woocommerce_integration_product_title' ) ) :
/**
* add a link back to the store page on the single product template
*
* @since 1.0.0
*/
function nice_woocommerce_integration_product_title() {
?>
<section class="back-to-store">
<a href="<?php echo esc_url( get_permalink( wc_get_page_id( 'shop' ) ) ); ?>" title="<?php esc_attr_e( 'Back to store', 'smart' ); ?>">
<i class="fa fa-th"></i> <span><?php esc_attr_e( 'Back to store', 'smart' ); ?></span>
</a>
</section>
<?php
}
endif;
if ( ! function_exists( 'nice_woocommerce_integration_remove_page_title' ) ) :
add_filter( 'woocommerce_show_page_title', 'nice_woocommerce_integration_remove_page_title' );
/**
* Remove page title from WooCommerce templates.
*
* @since 1.0.0
*
* @return bool
*/
function nice_woocommerce_integration_remove_page_title() {
return false;
}
endif;
if ( ! function_exists( 'nice_woocommerce_integration_theme_wrapper_start' ) ) :
remove_action( 'woocommerce_before_main_content', 'woocommerce_output_content_wrapper', 10 );
add_action( 'woocommerce_before_main_content', 'nice_woocommerce_integration_theme_wrapper_start', 10 );
/**
* Adds start of nice theme wrapper instead of the default one.
*
* @since 1.0.0
*
*/
function nice_woocommerce_integration_theme_wrapper_start() {
if ( ( is_shop() || is_product_category() || is_product_tag() )
&& $columns_number = nice_get_option( 'nice_woocommerce_columns' )
) {
$columns = 'columns-' . $columns_number;
} elseif ( is_product() ) {
$columns = '';
}
$full_width = nice_bool( nice_get_option( 'nice_woocommerce_products_fullwidth' ) );
if ( $full_width && is_product() ) {
$columns .= ' ';
} elseif ( $full_width && is_archive() ) {
$columns .= ' ';
} elseif ( is_page() ) {
$columns .= ' ';
} elseif ( is_cart() || is_checkout() ) {
$columns .= ' full-width';
} else {
$columns .= ' full-width';
}
?>
<section id="content" <?php nice_content_class( 'woocommerce-page ' . $columns, true ); ?>>
<?php
}
endif;
if ( ! function_exists( 'nice_woocommerce_integration_theme_wrapper_end' ) ) :
remove_action( 'woocommerce_after_main_content', 'woocommerce_output_content_wrapper_end', 10 );
add_action( 'woocommerce_after_main_content', 'nice_woocommerce_integration_theme_wrapper_end', 10 );
/**
* Adds end of nice theme wrapper instead of the default one.
*
* @since 1.0.0
*
*/
function nice_woocommerce_integration_theme_wrapper_end() {
?>
</section>
<?php
}
endif;
/**
* Disable default WooCommerce lightbox.
*
* @since 1.0.0
*/
add_action( 'option_woocommerce_enable_lightbox', '__return_false' );
if ( ! function_exists( 'nice_woocommerce_integration_search_form' ) ) :
add_filter( 'get_product_search_form', 'nice_woocommerce_integration_search_form' );
/**
* Replace the default search form.
*
* @since 1.0.0
*
* @return string Form HTML.
*/
function nice_woocommerce_integration_search_form() {
$default_view_mode = nice_get_option( '_woocommerce_shop_layout', 'grid' );
$view_mode = nice_woocommerce_integration_products_loop_view_mode();
$view_mode_field = $default_view_mode !== $view_mode ? '<input type="hidden" name="view_mode" value="' . esc_attr( $view_mode ) . '" />' : '';
$form = '<form role="search" method="get" action="' . esc_url( home_url( '/' ) ) . '" >
<label class="screen-reader-text" for="s">' . esc_html__( 'Search Products:', 'smart' ) . '</label>
<input type="search" results="5" autosave="' . esc_url( home_url( '/' ) ) . '" class="input-text" placeholder="' . esc_attr__( 'Search Products', 'smart' ) . '" name="s" />
' . $view_mode_field . '
<input type="submit" class="button" id="searchsubmit" value="' . esc_attr__( 'Search', 'smart' ) . '" />
<input type="hidden" name="post_type" value="product" />
</form>';
$form = apply_filters( 'nice_woocommerce_integration_search_form', $form );
return $form;
}
endif;
if ( ! function_exists( 'nice_woocommerce_integration_get_sidebar' ) ) :
remove_action( 'woocommerce_sidebar', 'woocommerce_get_sidebar', 10 );
add_action( 'woocommerce_after_main_content', 'nice_woocommerce_integration_get_sidebar', 10 );
/**
* Use custom WooCommerce sidebar instead of default.
*
* @since 1.0.0
*/
function nice_woocommerce_integration_get_sidebar() {
if ( is_product() || is_product_category() || is_product_tag() || is_shop() ) {
add_filter( 'nice_sidebar_id', 'nice_woocommerce_integration_shop_sidebar', 20 );
}
// Display the sidebar if full width option is disabled on archives
if ( nice_bool( nice_get_option( 'nice_woocommerce_archives_sidebar' ) ) && ( is_product_category() || is_product_tag() || is_shop() ) ) {
nice_sidebar_do();
return;
}
// Display the sidebar if full width option is disabled on product pages
if ( nice_bool( nice_get_option( 'nice_woocommerce_products_sidebar' ) ) && ( is_product() ) ) {
nice_sidebar_do();
return;
}
remove_filter( 'nice_sidebar_id', 'nice_woocommerce_integration_shop_sidebar', 20 );
}
endif;
if ( ! function_exists( 'nice_woocommerce_integration_shop_sidebar' ) ) :
/**
* Set ID for WooCommerce shop sidebar.
*
* @see nice_sidebar_do()
*
* @return string
*/
function nice_woocommerce_integration_shop_sidebar() {
return 'shop';
}
endif;
if ( ! function_exists( 'nice_woocommerce_integration_account_sidebar' ) ) :
add_filter( 'nice_sidebar_id', 'nice_woocommerce_integration_account_sidebar', 10 );
/**
* Filter the account sidebar for the WooCommerce account pages.
*
* @since 1.0
*
* @param string $sidebar_id Unfiltered sidebar ID.
* @return string Filtered sidebar ID.
*/
function nice_woocommerce_integration_account_sidebar( $sidebar_id ) {
if ( is_account_page() ) :
$sidebar_id = 'account';
endif;
return $sidebar_id;
}
endif;
if ( ! function_exists( 'nice_woocommerce_integration_loop_shop_columns' ) ) :
add_filter( 'loop_shop_columns', 'nice_woocommerce_integration_loop_shop_columns' );
/**
* Change number of products per row.
*
* @since 1.0.0
*
* @return int
*/
function nice_woocommerce_integration_loop_shop_columns() {
return nice_get_option( 'nice_woocommerce_columns', 4 );
}
endif;
if ( ! function_exists( 'nice_woocommerce_integration_layout_body_class' ) ) :
add_filter( 'body_class', 'nice_woocommerce_integration_layout_body_class', 10 );
/**
* Add current layout class to body class.
*
* @since 1.0.0
*
* @param array $wc_classes
*
* @return array
*/
function nice_woocommerce_integration_layout_body_class( $wc_classes ) {
$layout = '';
$page = '';
// Add woocommerce-fullwidth class if full width option is enabled
if ( is_product_category() || is_product_tag() || is_shop() ) {
$page = 'woocommerce-shop';
if ( nice_bool( nice_get_option( 'nice_woocommerce_archives_sidebar' ) ) ) {
$layout = 'has-sidebar';
} else {
$layout = 'layout-full';
}
}
if ( is_product() ) {
$page = 'woocommerce-product';
if ( nice_bool( nice_get_option( 'nice_woocommerce_products_sidebar' ) ) ) {
$layout = 'has-sidebar';
} else {
$layout = 'layout-full';
}
}
if ( is_cart() || is_checkout() ) {
$layout = 'layout-full';
if ( is_cart() ) {
$page = 'woocommerce-cart';
}
if ( is_checkout() ) {
$page = 'woocommerce-checkout';
}
}
// Remove sidebar if we're using a full-width layout.
if ( 'layout-full' === $layout ) {
remove_action( 'nice_sidebar', 'nice_sidebar_do' );
}
if ( is_order_received_page() ) {
$page = 'woocommerce-order-received';
}
// Add classes to body_class() output
$wc_classes[] = $layout;
$wc_classes[] = $page;
return $wc_classes;
}
endif;
if ( ! function_exists( 'nice_woocommerce_integration_layout_body_data' ) ) :
add_filter( 'nice_body_data', 'nice_woocommerce_integration_layout_body_data' );
/**
* Add data attributes to body.
*
* @since 1.0.0
*
* @param array $data
*
* @return array
*/
function nice_woocommerce_integration_layout_body_data( array $data = array() ) {
if ( 'yes' === get_option( 'woocommerce_enable_ajax_add_to_cart' ) ) {
$data['wc-ajax-cart'] = true;
}
return $data;
}
endif;
if ( ! function_exists( 'nice_woocommerce_integration_full_content_class' ) ) :
/**
* Replace default content class with a full-width class.
*
* Normally, the default content class is three quarters of the maximum number of columns in a grid.
* This function replaces it with the maximum number.
*
* @since 1.0.0
*/
function nice_woocommerce_integration_full_content_class() {
return apply_filters( 'nice_woocommerce_integration_full_content_class', 'full-width' );
}
endif;
if ( ! function_exists( 'nice_woocommerce_integration_breadcrumb' ) ) :
add_action( 'wp', 'nice_woocommerce_integration_breadcrumb' );
/**
* Remove default WooCommerce breadcrumbs.
*
* @since 1.0.0
*/
function nice_woocommerce_integration_breadcrumb() {
$show_breadcrumbs_in_product = nice_bool( nice_get_option( 'nice_woocommerce_products_breadcrumb' ) );
$show_breadcrumbs_in_archive = nice_bool( nice_get_option( 'nice_woocommerce_archives_breadcrumb' ) );
if ( ( ! $show_breadcrumbs_in_product && ( is_product() ) )
|| ( ! $show_breadcrumbs_in_archive && ( is_shop() || is_product_category() ) )
) {
remove_action( 'woocommerce_before_main_content', 'woocommerce_breadcrumb', 20 );
}
}
endif;
if ( ! function_exists( 'nice_woocommerce_integration_custom_breadcrumbs_args' ) ) :
add_filter( 'woo_breadcrumbs_args', 'nice_woocommerce_integration_custom_breadcrumbs_args', 10 );
/**
* Customize WooCommerce breadcrumbs.
*
* @since 1.0.0
*
* @return array
*/
function nice_woocommerce_integration_custom_breadcrumbs_args() {
$args = array(
'separator' => '/',
'before' => '',
'show_home' => esc_html__( 'Home', 'smart' ),
);
return $args;
}
endif;
if ( ! function_exists( 'nice_woocommerce_integration_products_per_page' ) ) :
add_filter( 'loop_shop_per_page', 'nice_woocommerce_integration_products_per_page', 20 );
/**
* Change number of products on shop page.
*
* @since 1.0.0
*/
function nice_woocommerce_integration_products_per_page() {
$products_per_page = nice_get_option( 'nice_woocommerce_products_per_page' );
$products_per_page = $products_per_page ? $products_per_page : 12;
return $products_per_page;
}
endif;
if ( ! function_exists( 'nice_woocommerce_integration_image_sizes' ) ) :
add_action( 'init', 'nice_woocommerce_integration_image_sizes', 1 );
/**
* Define image sizes.
*
* @since 1.0.0
*/
function nice_woocommerce_integration_image_sizes() {
/**
* @hook nice_woocommerce_integration_catalog_image_size
*
* Image sizes for WC catalog can be redefined by hooking in here.
*
* @since 1.0.0
*/
$catalog = apply_filters( 'nice_woocommerce_integration_catalog_image_size', array(
'width' => 300,
'height' => 378,
'crop' => 1,
)
);
/**
* @hook nice_woocommerce_integration_single_image_size
*
* Image sizes for single WC products can be redefined by hooking in here.
*
* @since 1.0.0
*/
$single = apply_filters( 'nice_woocommerce_integration_single_image_size', array(
'width' => 580,
'height' => 730,
'crop' => 1,
)
);
/**
* @hook nice_woocommerce_integration_thumbnail_image_size
*
* Image sizes for single WC products can be redefined by hooking in here.
*
* @since 1.0.0
*/
$thumbnail = apply_filters( 'nice_woocommerce_integration_thumbnail_image_size', array(
'width' => 110,
'height' => 110,
'crop' => 1,
)
);
// Update image sizes
if ( get_option( 'shop_catalog_image_size' ) !== $catalog ) {
update_option( 'shop_catalog_image_size', $catalog ); // Product category thumbs
}
if ( get_option( 'shop_single_image_size' ) !== $single ) {
update_option( 'shop_single_image_size', $single );
}
if ( get_option( 'shop_thumbnail_image_size' ) !== $thumbnail ) {
update_option( 'shop_thumbnail_image_size', $thumbnail ); // Image gallery thumbs
}
}
endif;
if ( ! function_exists( 'nice_woocommerce_integration_post_class' ) ) :
add_filter( 'post_class', 'nice_woocommerce_integration_post_class', 10, 3 );
add_filter( 'product_cat_class', 'nice_woocommerce_integration_post_class', 10, 3 );
/** @noinspection PhpUnusedParameterInspection */
/**
* Add custom post classes for products.
*
* @since 1.0.0
*
* @param array $classes
* @param array $class
* @param int $post_id
*
* @return array
*/
// @codingStandardsIgnoreStart
function nice_woocommerce_integration_post_class( array $classes = array(), $class = array(), $post_id ) {
// @codingStandardsIgnoreEnd
static $original_classes = array();
static $triggered = false;
// Define static array key.
$key = intval( $post_id );
// Check if we're seeing products.
$is_product = ( 'product' === get_post_type() );
// Check if we need to add classes.
// We need to add classes for all lists of products, and avoid applying
// them to the main product in single product pages. Since `post_class`
// will run first for the main product, we're not adding classes during
// the first time this function runs for singular product pages.
$add_classes = ! is_singular( 'product' ) || ( is_singular( 'product' ) && $triggered );
// Save original classes to restore them later if needed.
if ( empty( $original_classes[ $key ] ) ) {
$original_classes[ $key ] = $classes;
// Set flag to true, so we can save the status of this filter.
$triggered = true;
}
if ( $is_product && $add_classes ) {
global $woocommerce_loop;
$view_mode = nice_woocommerce_integration_products_loop_view_mode();
$colors = nice_theme_colors();
// Add our custom classes.
$classes = array_merge( $classes, array(
'item',
'type-product',
'thumb',
'item-tall',
'multiple-click',
'content-padding-h-0x',
'content-padding-v-1x',
'overlay-color-' . $colors['nice_dark_color_1']['css_id'],
'overlay-animated',
'image-animated',
'text-' . ( is_rtl() ? 'right' : 'left' ),
)
);
$columns = isset( $woocommerce_loop['columns'] ) ? $woocommerce_loop['columns'] : nice_get_option( 'nice_woocommerce_columns', 4 );
$classes[] = 'columns-' . intval( $columns );
if ( 'list' === $view_mode ) {
$classes[] = 'thumb-' . ( is_rtl() ? 'right' : 'left' );
}
if ( 'grid' === $view_mode ) {
$classes[] = 'item-text-padding-reduced';
}
// Add class for text-based "add to cart" link.
if ( nice_bool_option( '_woocommerce_add_to_cart_text' ) ) {
$classes[] = 'has-add-to-cart-link';
}
} elseif ( ! empty( $original_classes[ $key ] ) ) {
// Restore original classes when the ones set here are not needed anymore.
$classes = $original_classes[ $key ];
}
return $classes;
}
endif;
if ( ! function_exists( 'nice_woocommerce_integration_loop_add_to_cart_args' ) ) :
add_filter( 'woocommerce_loop_add_to_cart_args', 'nice_woocommerce_integration_loop_add_to_cart_args' );
/**
* Modify arguments for "add to cart" button.
*
* @since 1.0.0
*
* @param array $args
*
* @return array
*/
function nice_woocommerce_integration_loop_add_to_cart_args( array $args = array() ) {
if ( ! is_single( 'product' ) ) {
$classes = 'btn btn-default btn-square btn-disable-hover allow-pointer-events full-width uppercase btn-cart';
if ( nice_bool_option( '_woocommerce_quick_view' ) && 'btn' === nice_get_option( '_woocommerce_quick_view_style' ) ) {
$args['class'] = str_replace( 'ajax_add_to_cart', '', $args['class'] );
$classes .= ' modal-open modal-open-quick-view-product';
}
$args['class'] .= ' ' . $classes;
}
return $args;
}
endif;
if ( ! function_exists( 'nice_woocommerce_integration_shop_view_mode' ) ) :
add_action( 'woocommerce_before_shop_loop', 'nice_woocommerce_integration_shop_view_mode', 30 );
/**
* Display "View Mode" toggle before the product list.
*
* @since 1.0.2
*/
function nice_woocommerce_integration_shop_view_mode() {
if ( ! nice_woocommerce_integration_products_loop_use_view_mode() ) {
return;
}
$shop_url = '';
// If we're in a search view, we need to reconstruct the base URL to allow switching the layout
// while keeping search results.
if ( is_search() ) {
if ( $search_query = get_search_query() ) {
$shop_url = get_search_link( $search_query );
$query_string = ! empty( $_SERVER['QUERY_STRING'] ) ? $_SERVER['QUERY_STRING'] : '';
parse_str( $query_string, $query );
if ( isset( $query['s'] ) ) {
unset( $query['s'] );
}
if ( isset( $query['view_mode'] ) ) {
unset( $query['view_mode'] );
}
if ( ! empty( $query ) ) {
$http_query = http_build_query( $query );
$shop_url .= stripos( $shop_url, '?' ) ? '&' . $http_query : '?' . $http_query;
}
}
if ( ! $shop_url ) {
$shop_url = get_search_link();
}
$shop_url = trim( urldecode( $shop_url ), '/' );
}
// Use shop URL in case we couldn't get a URL yet.
if ( ! $shop_url ) {
$shop_url = get_permalink( wc_get_page_id( 'shop' ) );
}
$separator = $shop_url && ( stripos( $shop_url, '?' ) ) ? '&' : '?';
?>
<div class="shop-view-mode">
<a href="<?php echo esc_url( $shop_url ) . esc_attr( $separator ); ?>view_mode=grid" title="<?php esc_html_e( 'Grid View', 'smart' ); ?>"><i class="fa fa-th" aria-hidden="true"></i> <span><?php esc_html_e( 'Grid', 'smart' ); ?></span></a>
<a href="<?php echo esc_url( $shop_url ) . esc_attr( $separator ); ?>view_mode=list" title="<?php esc_html_e( 'List View', 'smart' ); ?>"><i class="fa fa-th-list" aria-hidden="true"></i> <span><?php esc_html_e( 'List', 'smart' ); ?></span></a>
</div>
<?php
}
endif;
if ( ! function_exists( 'nice_woocommerce_integration_shop_loop_item' ) ) :
add_action( 'woocommerce_before_shop_loop_item_title', 'nice_woocommerce_integration_shop_loop_item' );
/**
* Define a new layout for the product within the loop.
*
* @since 1.0.0
*/
function nice_woocommerce_integration_shop_loop_item() {
$classes = array(
'item-inner',
'has-caption',
);
if ( nice_bool_option( '_woocommerce_quick_view' ) && 'default' === nice_get_option( '_woocommerce_quick_view_style' ) ) {
$classes[] = 'has-quick-view-icon-' . esc_attr( nice_get_option( '_woocommerce_quick_view_link_position' ) );
}
if ( nice_bool_option( '_yith_wcwl_shop_link' ) ) {
$classes[] = 'has-wishlist-icon-' . esc_attr( nice_get_option( '_yith_wcwl_shop_link_position' ) );
}
?>
<div <?php nice_css_classes( $classes ); ?>>
<?php
/**
* @hook nice_woocommerce_integration_shop_loop_item
*
* All markup for looped products should be processed here,
* removing all the default actions.
*
* @since 1.0.0
*
* Hooked here:
* @see nice_woocommerce_integration_loop_item_permalink() - 0
* @see nice_woocommerce_integration_item_caption() - 10
* @see nice_woocommerce_integration_loop_item_content() - 20
*/
do_action( 'nice_woocommerce_integration_shop_loop_item' );
?>
</div>
<?php
}
endif;
if ( ! function_exists( 'nice_woocommerce_integration_loop_item_filler' ) ) :
add_action( 'woocommerce_before_shop_loop_item_title', 'nice_woocommerce_integration_loop_item_filler' );
/**
* Print out the item filler for the list view.
*
* @since 1.0.3
*/
function nice_woocommerce_integration_loop_item_filler() {
if ( 'list' !== nice_woocommerce_integration_products_loop_view_mode() ) {
return;
}
?>
<div class="item-filler">
<?php nice_woocommerce_integration_loop_item_content(); ?>
</div>
<?php
}
endif;
/**
* Remove default wrapper link.
*
* @since 1.0.0
*/
remove_action( 'woocommerce_before_shop_loop_item', 'woocommerce_template_loop_product_link_open', 10 );
remove_action( 'woocommerce_after_shop_loop_item', 'woocommerce_template_loop_product_link_close', 5 );
if ( ! function_exists( 'nice_woocommerce_integration_loop_item_permalink' ) ) :
add_action( 'nice_woocommerce_integration_shop_loop_item', 'nice_woocommerce_integration_loop_item_permalink', 0 );
/**
* Open item permalink container.
*
* @since 1.0.0
*/
function nice_woocommerce_integration_loop_item_permalink() {
?>
<section class="item-permalink item-overlay-add-to-cart">
<?php
/**
* @hook nice_woocommerce_integration_loop_item_permalink
*
* Actions for looped products should be printed here, after
* removing all related default actions.
*
* @since 1.0.0
*
* Hooked here:
* @see nice_woocommerce_integration_template_loop_product_link() - 10
* @see woocommerce_template_loop_add_to_cart() - 20
*/
do_action( 'nice_woocommerce_integration_loop_item_permalink' );
?>
</section>
<?php
}
endif;
if ( ! function_exists( 'nice_woocommerce_integration_template_loop_product_link' ) ) :
add_action( 'nice_woocommerce_integration_loop_item_permalink', 'nice_woocommerce_integration_template_loop_product_link', 10 );
/**
* Use our custom opening anchor tag for products in the loop.
*
* @since 1.0.0
*/
function nice_woocommerce_integration_template_loop_product_link() {
echo '<a class="permalink" href="' . esc_url( get_the_permalink() ) . '"></a>';
}
endif;
if ( ! function_exists( 'nice_woocommerce_integration_template_loop_add_to_cart' ) ) :
remove_action( 'woocommerce_after_shop_loop_item', 'woocommerce_template_loop_add_to_cart', 10 );
add_action( 'nice_woocommerce_integration_loop_item_permalink', 'nice_woocommerce_integration_template_loop_add_to_cart' );
/**
* Relocate "add to cart" button.
*
* @since 1.0.0
*/
function nice_woocommerce_integration_template_loop_add_to_cart() {
// Return early if we need to show the text-based "add to cart" link.
if ( nice_bool_option( '_woocommerce_add_to_cart_text' ) ) {
return;
}
woocommerce_template_loop_add_to_cart();
}
endif;
if ( ! function_exists( 'nice_woocommerce_integration_loop_added_to_cart_notification' ) ) :
add_action( 'nice_woocommerce_integration_loop_item_permalink', 'nice_woocommerce_integration_loop_added_to_cart_notification', 30 );
/**
* Show a notification for products that were added to cart.
*
* @since 1.0.0
*/
function nice_woocommerce_integration_loop_added_to_cart_notification() {
?>
<div class="nice-cart-notification">
<div class="nice-cart-notification-icon loading">
<i class="fa fa-circle-o-notch fa-spin"></i>
</div>
<div class="nice-cart-notification-icon added">
<i class="fa fa-check"></i>
</div>
</div>
<?php
}
endif;
if ( ! function_exists( 'nice_woocommerce_integration_item_caption' ) ) :
remove_action( 'woocommerce_before_shop_loop_item_title', 'woocommerce_template_loop_product_thumbnail', 10 ); // Remove default thumbnail.
add_action( 'nice_woocommerce_integration_shop_loop_item', 'nice_woocommerce_integration_item_caption', 10 );
/**
* Print item caption.
*
* @since 1.0.0
*/
function nice_woocommerce_integration_item_caption() {
global $product;
$classes = array(
'item-caption',
);
$attachment_ids = method_exists( $product, 'get_gallery_image_ids' ) ? $product->get_gallery_image_ids() : $product->get_gallery_attachment_ids();
if ( nice_bool_option( '_woocommerce_image_flip' ) && ! empty( $attachment_ids ) ) {
$classes[] = 'item-caption-flip';
}
?>
<div <?php nice_css_classes( $classes ); ?>>
<div class="item-caption-inner">
<?php
/**
* @hook nice_woocommerce_integration_item_caption
*
* Hook in here to print out the contents of the current item's caption.
*
* @since 1.0.2
*
* Hooked here:
* @see nice_woocommerce_integration_item_caption_permalink() - 10
* @see nice_woocommerce_integration_item_grid_caption_sale_flash() - 20
*/
do_action( 'nice_woocommerce_integration_item_caption' );
?>
</div>
</div>
<?php
}
endif;
if ( ! function_exists( 'nice_woocommerce_integration_item_caption_permalink' ) ) :
add_action( 'nice_woocommerce_integration_item_caption', 'nice_woocommerce_integration_item_caption_permalink', 10 );
/**
* Print out the current item's permalink.
*
* @since 1.0.2
*/
function nice_woocommerce_integration_item_caption_permalink() {
global $product;
$image_flip = nice_bool_option( '_woocommerce_image_flip' );
/**
* Generate markup Image Flip feature.
*/
if ( $image_flip ) {
$attachment_ids = method_exists( $product, 'get_gallery_image_ids' ) ? $product->get_gallery_image_ids() : $product->get_gallery_attachment_ids();
if ( ! empty( $attachment_ids ) ) {
foreach ( $attachment_ids as $attachment_id ) {
$thumbnail_size = apply_filters( 'single_product_shop_thumbnail_size', 'shop_catalog' );
$secondary_image = wp_get_attachment_image( $attachment_id, $thumbnail_size );
break;
}
}
}
?>
<a class="item-caption-permalink" href="<?php the_permalink(); ?>">
<?php woocommerce_template_loop_product_thumbnail(); ?>
<?php if ( ! empty( $secondary_image ) ) :
echo $secondary_image; // WPCS: XSS ok.
endif; ?>
<div class="item-caption-overlay <?php nice_theme_color_background_class( 'dark-color-1', true ); ?> animated" data-opacity="10"></div>
</a>
<?php
}
endif;
if ( ! function_exists( 'nice_woocommerce_integration_item_list_caption_sale_flash' ) ) :
add_action( 'nice_woocommerce_integration_item_caption', 'nice_woocommerce_integration_item_list_caption_sale_flash', 20 );
/**
* Print out sale flash for the current item in list mode.
*
* @since 1.0.2
*/
function nice_woocommerce_integration_item_list_caption_sale_flash() {
global $product;
if ( $product->is_on_sale() && 'list' === nice_woocommerce_integration_products_loop_view_mode() ) {
echo apply_filters( 'woocommerce_sale_flash', '' ); // WPCS: XSS Ok.
}
}
endif;
if ( ! function_exists( 'nice_woocommerce_integration_loop_grid_item_content' ) ) :
add_action( 'nice_woocommerce_integration_shop_loop_item', 'nice_woocommerce_integration_loop_grid_item_content', 20 );
/**
* Print out item content for grid view-mode.
*
* @since 1.0.0
*/
function nice_woocommerce_integration_loop_grid_item_content() {
if ( 'grid' !== nice_woocommerce_integration_products_loop_view_mode() ) {
return;
}
nice_woocommerce_integration_loop_item_content();
}
endif;
if ( ! function_exists( 'nice_woocommerce_integration_loop_item_content' ) ) :
/**
* Open content section wrapper.
*
* @since 1.0.0
*/
function nice_woocommerce_integration_loop_item_content() {
?>
<div class="item-content">
<div class="item-content-inner">
<?php
/**
* @hook nice_woocommerce_integration_loop_item_content
*
* All internal content for looped products should be printed
* here, after removing all related default actions.
*
* @since 1.0.0
*
* Hooked here:
* @see nice_woocommerce_integration_loop_product_title() - 10
* @see woocommerce_template_loop_price() - 20
* @see woocommerce_show_product_loop_sale_flash() - 30
* @see woocommerce_template_loop_rating() - 40
*/
do_action( 'nice_woocommerce_integration_loop_item_content' );
?>
</div>
</div>
<?php
}
endif;
if ( ! function_exists( 'nice_woocommerce_integration_add_product_categories' ) ) :
add_action( 'nice_woocommerce_integration_loop_item_content', 'nice_woocommerce_integration_loop_product_categories', 0 );
/**
* For shop list view, add product categories to the product's content.
*
* @since 1.0.2
*/
function nice_woocommerce_integration_loop_product_categories() {
if ( 'list' === nice_woocommerce_integration_products_loop_view_mode() ) {
nice_woocommerce_integration_product_categories();
}
}
endif;
if ( ! function_exists( 'nice_woocommerce_integration_product_categories' ) ) :
/**
* Print out a list of a product categories.
*
* @since 1.0.2
*/
function nice_woocommerce_integration_product_categories() {
$categories_array = wp_get_post_terms( get_the_ID(), 'product_cat' );
$category_links = array();
$category_classes = array(
'entry-categories',
'entry-meta',
);
if ( ! empty( $categories_array ) ) {
foreach ( $categories_array as $category ) {
$category_links[] = '<a href="' . esc_url( get_term_link( $category->term_id, $category->taxonomy ) ) . '">' . esc_attr( $category->name ) . '</a>';
}
}
if ( ! empty( $category_links ) ) {
$output = '<div ' . nice_css_classes( $category_classes, false ) . '>';
$output .= '<span>' . join( ', ', $category_links ) . '</span>';
$output .= '</div>';
echo $output; // WPCS: XSS Ok.
}
}
endif;
if ( ! function_exists( 'nice_woocommerce_integration_loop_product_title' ) ) :
remove_action( 'woocommerce_shop_loop_item_title', 'woocommerce_template_loop_product_title', 10 ); // Remove default product title.
add_action( 'nice_woocommerce_integration_loop_item_content', 'nice_woocommerce_integration_loop_product_title', 10 );
/**
* Show the product title in the product loop using our own markup.
*
* @since 1.0.0
*/
function nice_woocommerce_integration_loop_product_title() {
echo '<h3 class="entry-title"><a href="' . esc_url( get_the_permalink() ) . '">' . get_the_title() . '</a></h3>';
}
endif;
remove_action( 'woocommerce_after_shop_loop_item_title', 'woocommerce_template_loop_price', 10 );
add_action( 'nice_woocommerce_integration_loop_item_content', 'woocommerce_template_loop_price', 20 );
if ( ! function_exists( 'nice_woocommerce_integration_loop_grid_item_sale_flash' ) ) :
remove_action( 'woocommerce_before_shop_loop_item_title', 'woocommerce_show_product_loop_sale_flash', 10 );
add_action( 'nice_woocommerce_integration_loop_item_content', 'nice_woocommerce_integration_loop_grid_item_sale_flash', 30 );
/**
* Print out sale flash for grid view.
*
* @since 1.0.2
*/
function nice_woocommerce_integration_loop_grid_item_sale_flash() {
if ( 'grid' !== nice_woocommerce_integration_products_loop_view_mode() ) {
return;
}
woocommerce_show_product_loop_sale_flash();
}
endif;
if ( ! function_exists( 'nice_woocommerce_integration_loop_list_item_excerpt' ) ) :
add_action( 'nice_woocommerce_integration_loop_item_content', 'nice_woocommerce_integration_loop_list_item_excerpt', 40 );
/**
* For shop list view, add excerpt to product content.
*
* @since 1.0.2
*/
function nice_woocommerce_integration_loop_list_item_excerpt() {
if ( 'list' === nice_woocommerce_integration_products_loop_view_mode() ) {
nice_woocommerce_integration_loop_item_excerpt();
}
}
endif;
if ( ! function_exists( 'nice_woocommerce_integration_loop_item_excerpt' ) ) :
/**
* Print out product excerpt.
*
* @since 1.0.2
*/
function nice_woocommerce_integration_loop_item_excerpt() {
?>
<div class="entry-excerpt">
<?php the_excerpt(); ?>
</div>
<?php
}
endif;
if ( ! function_exists( 'nice_woocommerce_integration_loop_sale_flash_wrapper' ) ) :
add_filter( 'woocommerce_sale_flash', 'nice_woocommerce_integration_loop_sale_flash_wrapper', 99 );
/**
* Wrap sale flash label with our custom markup inside the loop.
*
* @since 1.0.0
*
* @param string $html
*
* @return string
*/
function nice_woocommerce_integration_loop_sale_flash_wrapper( $html = '' ) {
if ( ! is_single( 'product' ) ) {
$position = 'left' === nice_get_option( '_woocommerce_sale_flash_text_position' ) ? 'top-left' : 'top-right';
$html = '<div class="entry-sale position-' . esc_attr( $position ) . '">' . $html . '</div>';
}
return $html;
}
endif;
if ( ! function_exists( 'nice_woocommerce_integration_loop_rating' ) ) :
add_action( 'after_setup_theme', 'nice_woocommerce_integration_loop_rating' );
/**
* Remove default location of product rating stars and relocate them if
* required.
*
* @since 1.0.0
*/
function nice_woocommerce_integration_loop_rating() {
remove_action( 'woocommerce_after_shop_loop_item_title', 'woocommerce_template_loop_rating', 5 );
// Relocate if rating is enabled from user settings.
if ( nice_bool( nice_get_option( 'nice_woocommerce_archives_rating' ) ) ) {
add_action( 'nice_woocommerce_integration_loop_item_content', 'woocommerce_template_loop_rating', 40 );
}
}
endif;
if ( ! function_exists( 'nice_woocommerce_integration_loop_rating_wrapper' ) ) :
add_filter( 'woocommerce_product_get_rating_html', 'nice_woocommerce_integration_loop_rating_wrapper', 99 );
/**
* Wrap rating stars with our custom markup inside the loop.
*
* @since 1.0.0
*
* @param string $html
*
* @return string
*/
function nice_woocommerce_integration_loop_rating_wrapper( $html = '' ) {
if ( ! is_single( 'product' ) ) {
$position = is_rtl() ? 'bottom-right' : 'bottom-left';
$html = '<div class="entry-rating position-' . esc_attr( $position ) . '">' . $html . '</div>';
}
return $html;
}
endif;
if ( ! function_exists( 'nice_woocommerce_integration_product_add_to_cart_text' ) ) :
add_filter( 'woocommerce_product_add_to_cart_text', 'nice_woocommerce_integration_product_add_to_cart_text' );
/**
* Modify "add to cart" text.
*
* @since 1.0.0
*
* @param string $text
*
* @return string
*/
function nice_woocommerce_integration_product_add_to_cart_text( $text = '' ) {
if ( nice_bool_option( '_woocommerce_quick_view' ) && 'btn' === nice_get_option( '_woocommerce_quick_view_style' ) ) {
$text = esc_html__( 'Quick view', 'smart' );
}
return $text;
}
endif;
if ( ! function_exists( 'nice_woocommerce_integration_modal_quick_view_product' ) ) :
add_action( 'wp_footer', 'nice_woocommerce_integration_modal_quick_view_product' );
/**
* Print out an invisible modal box for product quick view.
*
* @since 1.0.0
*/
function nice_woocommerce_integration_modal_quick_view_product() {
if ( ! nice_woocommerce_integration_modal_display() ) {
return;
}
wc_get_template( 'quick-view/modal.php' );
}
endif;
if ( ! function_exists( 'nice_woocommerce_integration_modal_display' ) ) :
/**
* Check if the modal window should be displayed.
*
* @since 1.0.0
*
* @return bool
*/
function nice_woocommerce_integration_modal_display() {
$display = nice_bool_option( '_woocommerce_quick_view' );
if ( ! is_shop() && ! is_product() && ! is_product_category() && ! is_product_tag() ) {
$display = false;
}
return apply_filters( 'nice_woocommerce_integration_modal_display', $display );
}
endif;
if ( ! function_exists( 'nice_woocommerce_integration_modal_quick_view_link' ) ) :
add_action( 'nice_woocommerce_integration_loop_item_content', 'nice_woocommerce_integration_modal_quick_view_link', 20 );
/**
* Display default link to product quick view.
*
* @since 1.0.0
*/
function nice_woocommerce_integration_modal_quick_view_link() {
if ( ! nice_bool_option( '_woocommerce_quick_view' ) ) {
return;
}
$style = nice_get_option( '_woocommerce_quick_view_style' );
if ( 'text' === $style ) {
wc_get_template( 'quick-view/link.php' );
} elseif ( 'default' === $style ) {
wc_get_template( 'quick-view/icon.php' );
}
}
endif;
if ( ! function_exists( 'nice_woocommerce_integration_heading_display' ) ) :
add_filter( 'nice_heading_display', 'nice_woocommerce_integration_heading_display' );
/**
* Make sure the heading is always displayed in product pages.
*
* @since 1.1
*
* @param boolean $display
*
* @return mixed
*/
function nice_woocommerce_integration_heading_display( $display ) {
return is_product() ?: $display;
}
endif;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment