";
}
}
//remove_action( 'woocommerce_product_thumbnails', 'woocommerce_show_product_thumbnails', 20 );
if(avia_woocommerce_version_check('3.0.0')) // in woocommerce 3.0.0
{
add_action('woocommerce_product_thumbnails', 'avia_product_gallery_thumbnail_opener', 19);
add_action('woocommerce_product_thumbnails', 'avia_close_div', 21);
}
if(!function_exists('avia_product_gallery_thumbnail_opener'))
{
function avia_product_gallery_thumbnail_opener()
{
echo "
";
}
}
#
# displays a front end interface for modifying the shoplist query parameters like sorting order, product count etc
#
if(!function_exists('avia_woocommerce_frontend_search_params'))
{
add_action( 'woocommerce_before_shop_loop', 'avia_woocommerce_frontend_search_params', 20);
function avia_woocommerce_frontend_search_params()
{
global $avia_config;
if(!empty($avia_config['woocommerce']['disable_sorting_options'])) return false;
$product_order['default'] = __("Default Order",'avia_framework');
$product_order['title'] = __("Name",'avia_framework');
$product_order['price'] = __("Price",'avia_framework');
$product_order['date'] = __("Date",'avia_framework');
$product_order['popularity'] = __("Popularity",'avia_framework');
$product_sort['asc'] = __("Click to order products ascending", 'avia_framework');
$product_sort['desc'] = __("Click to order products descending", 'avia_framework');
$per_page_string = __("Products per page",'avia_framework');
$per_page = get_option('avia_woocommerce_product_count');
if(!$per_page) $per_page = get_option('posts_per_page');
if(!empty($avia_config['woocommerce']['default_posts_per_page'])) $per_page = $avia_config['woocommerce']['default_posts_per_page'];
parse_str($_SERVER['QUERY_STRING'], $params);
$po_key = !empty($avia_config['woocommerce']['product_order']) ? $avia_config['woocommerce']['product_order'] : 'default';
$ps_key = !empty($avia_config['woocommerce']['product_sort']) ? $avia_config['woocommerce']['product_sort'] : 'asc';
$pc_key = !empty($avia_config['woocommerce']['product_count']) ? $avia_config['woocommerce']['product_count'] : $per_page;
$ps_key = strtolower($ps_key);
$nofollow = 'rel="nofollow"';
//generate markup
$output = "";
$output .= "
";
$output .= "
";
$output .= " - ".__("Sort by",'avia_framework')." ".$product_order[$po_key]."";
$output .= " ";
$output .= "
";
$output .= "
";
$output .= "
";
$output .= "
";
$output .= " - ".__("Display",'avia_framework')." ".$pc_key." ".$per_page_string."";
$output .= " ";
$output .= "
";
$output .= "
";
$output .= "
";
echo $output;
}
}
//helper function to create the active list class
if(!function_exists('avia_woo_active_class'))
{
function avia_woo_active_class($key1, $key2)
{
if($key1 == $key2) return " class='current-param'";
}
}
//helper function to build the query strings for the catalog ordering menu
if(!function_exists('avia_woo_build_query_string'))
{
function avia_woo_build_query_string($params = array(), $overwrite_key, $overwrite_value)
{
$params[$overwrite_key] = $overwrite_value;
$paged = (array_key_exists('product_count', $params)) ? 'paged=1&' : '';
return "?" . $paged . http_build_query($params);
}
}
//function that actually overwrites the query parameters
if(!function_exists('avia_woocommerce_overwrite_catalog_ordering'))
{
add_action( 'woocommerce_get_catalog_ordering_args', 'avia_woocommerce_overwrite_catalog_ordering', 20);
function avia_woocommerce_overwrite_catalog_ordering($args)
{
global $avia_config;
if(!empty($avia_config['woocommerce']['disable_sorting_options'])) return $args;
//check the folllowing get parameters and session vars. if they are set overwrite the defaults
$check = array('product_order', 'product_count', 'product_sort');
if(empty($avia_config['woocommerce'])) $avia_config['woocommerce'] = array();
foreach($check as $key)
{
if(isset($_GET[$key]) ) $_SESSION['avia_woocommerce'][$key] = esc_attr($_GET[$key]);
if(isset($_SESSION['avia_woocommerce'][$key]) ) $avia_config['woocommerce'][$key] = $_SESSION['avia_woocommerce'][$key];
}
// is user wants to use new product order remove the old sorting parameter
if(isset($_GET['product_order']) && !isset($_GET['product_sort']) && isset($_SESSION['avia_woocommerce']['product_sort']))
{
unset($_SESSION['avia_woocommerce']['product_sort'], $avia_config['woocommerce']['product_sort']);
}
extract($avia_config['woocommerce']);
// set the product order
if(!empty($product_order))
{
switch ( $product_order ) {
case 'date' : $orderby = 'date'; $order = 'desc'; $meta_key = ''; break;
case 'price' : $orderby = 'meta_value_num'; $order = 'asc'; $meta_key = '_price'; break;
case 'popularity' : $orderby = 'meta_value_num'; $order = 'desc'; $meta_key = 'total_sales'; break;
case 'title' : $orderby = 'title'; $order = 'asc'; $meta_key = ''; break;
case 'default':
default : $orderby = 'menu_order title'; $order = 'asc'; $meta_key = ''; break;
}
}
// set the product count
if(!empty($product_count) && is_numeric($product_count))
{
$avia_config['shop_overview_products_overwritten'] = true;
$avia_config['shop_overview_products'] = (int) $product_count;
}
//set the product sorting
if(!empty($product_sort))
{
switch ( $product_sort ) {
case 'desc' : $order = 'desc'; break;
case 'asc' : $order = 'asc'; break;
default : $order = 'asc'; break;
}
}
if(isset($orderby)) $args['orderby'] = $orderby;
if(isset($order)) $args['order'] = $order;
if (!empty($meta_key))
{
$args['meta_key'] = $meta_key;
}
$avia_config['woocommerce']['product_sort'] = $args['order'];
return $args;
}
}
//remove produt information on password protected products
if(!function_exists('avia_woocommerce_remove_hooks'))
{
add_action('woocommerce_before_single_product', 'avia_woocommerce_remove_hooks');
function avia_woocommerce_remove_hooks()
{
/*remove content from password protected products*/
if(post_password_required())
{
add_action( 'woocommerce_after_single_product_summary', 'avia_woocommerce_echo_password', 1 );
remove_action( 'woocommerce_after_single_product_summary', 'woocommerce_output_product_data_tabs', 1 );
remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_price', 10 );
remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_excerpt', 20 );
remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_meta', 40 );
remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_sharing', 50 );
remove_action( 'woocommerce_before_single_product_summary', 'woocommerce_show_product_images', 20 );
remove_action( 'woocommerce_product_thumbnails', 'woocommerce_show_product_thumbnails', 20 );
remove_action( 'woocommerce_after_single_product_summary', 'woocommerce_output_product_data_tabs', 10 );
remove_action( 'woocommerce_after_single_product_summary', 'woocommerce_upsell_display', 15 );
remove_action( 'woocommerce_after_single_product_summary', 'woocommerce_output_related_products', 20 );
remove_action( 'woocommerce_before_single_product_summary', 'woocommerce_show_product_sale_flash', 10 );
remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_add_to_cart', 30 );
remove_action( 'woocommerce_simple_add_to_cart', 'woocommerce_simple_add_to_cart', 30 );
remove_action( 'woocommerce_grouped_add_to_cart', 'woocommerce_grouped_add_to_cart', 30 );
remove_action( 'woocommerce_variable_add_to_cart', 'woocommerce_variable_add_to_cart', 30 );
remove_action( 'woocommerce_external_add_to_cart', 'woocommerce_external_add_to_cart', 30 );
remove_action( 'woocommerce_after_shop_loop_item', 'woocommerce_template_loop_add_to_cart', 10 );
remove_action( 'woocommerce_before_shop_loop_item_title', 'woocommerce_template_loop_product_thumbnail', 10 );
remove_action( 'woocommerce_after_shop_loop_item_title', 'woocommerce_template_loop_price', 10 );
remove_action( 'woocommerce_after_shop_loop_item_title', 'woocommerce_template_loop_rating', 5 );
remove_action( 'woocommerce_after_shop_loop_item_title', 'woocommerce_show_product_loop_sale_flash', 10 );
}
}
}
if(!function_exists('avia_woocommerce_echo_password'))
{
add_action('ava_woocomemrce_password_protection_remove_hooks', 'avia_woocommerce_remove_hooks');
function avia_woocommerce_echo_password()
{
/*remove content from password protected products*/
if(post_password_required())
{
echo get_the_password_form();
}
}
}
if( ! function_exists( 'avia_woocommerce_product_gallery_support' ) )
{
if ( did_action( 'woocommerce_init' ) )
{
avia_woocommerce_product_gallery_support();
}
else
{
add_action( 'woocommerce_init', 'avia_woocommerce_product_gallery_support', 50 );
}
function avia_woocommerce_product_gallery_support()
{
if( avia_woocommerce_version_check( '3.0.0' ) && current_theme_supports( 'avia-wc-30-product-gallery-feature' ) )
{
remove_action( 'woocommerce_product_thumbnails', 'avia_product_gallery_thumbnail_opener', 19 );
remove_action( 'woocommerce_product_thumbnails', 'avia_close_div', 21 );
}
else
{
add_filter( 'woocommerce_single_product_image_html','avia_woocommerce_post_thumbnail_description', 10, 2 );
add_filter( 'woocommerce_single_product_image_thumbnail_html','avia_woocommerce_gallery_thumbnail_description', 10, 4 );
}
}
}
/*
todo: check if this filter is deprecated. seems no longer in use
*/
if(!function_exists('avia_woocommerce_post_thumbnail_description'))
{
function avia_woocommerce_post_thumbnail_description($img, $post_id)
{
global $post, $woocommerce, $product;
if(has_post_thumbnail())
{
$image_title = esc_attr(get_post_field('post_content', get_post_thumbnail_id()));
$image_link = wp_get_attachment_url( get_post_thumbnail_id() );
$image = get_the_post_thumbnail( $post->ID, apply_filters( 'single_product_large_thumbnail_size', 'shop_single' ), array(
'title' => $image_title
) );
$attachment_count = count( $product->get_gallery_attachment_ids() );
if ( $attachment_count > 0 ) {
$gallery = '[product-gallery]';
} else {
$gallery = '';
}
return sprintf( '
%s', $image_link, $image_title, $image);
}
return $img;
}
}
/*
single page big image and thumbnails are using the same filter now. therefore we need to make sure that the images get the correct size by storing once the
woocommerce_product_thumbnails action has been called
*/
add_action('woocommerce_product_thumbnails', 'avia_woocommerce_set_single_page_image_size');
if(!function_exists('avia_woocommerce_set_single_page_image_size'))
{
function avia_woocommerce_set_single_page_image_size()
{
global $avia_config;
if(!isset($avia_config['avwc-single-page-size']))
{
$avia_config['avwc-single-page-size'] = "shop_thumbnail";
}
}
}
if(!function_exists('avia_woocommerce_gallery_thumbnail_description'))
{
function avia_woocommerce_gallery_thumbnail_description($img, $attachment_id, $post_id = "", $image_class = "" )
{
global $avia_config;
$image_size = isset($avia_config['avwc-single-page-size']) ? $avia_config['avwc-single-page-size'] : 'shop_single';
$image_link = wp_get_attachment_url( $attachment_id );
if(!$image_link) return $img;
$image = wp_get_attachment_image( $attachment_id, apply_filters( 'single_product_small_thumbnail_size', $image_size ) );
$image_title = esc_attr(get_post_field('post_content', $attachment_id));
$img = sprintf( '
%s', $image_link, $image_class, $image_title, $image );
return $img;
}
}
if(!function_exists('avia_title_args_woopage'))
{
add_filter('avf_title_args','avia_title_args_woopage', 10, 4);
function avia_title_args_woopage($args)
{
if(is_single() && is_product())
{
$args['heading'] = "strong";
}
return $args;
}
}
/*
Function that is able to overwrite the default "shop" page used by woocommerce so the template builder can be used
Will only be executed if the user has switched the "shop" page to advanced layout builder. Default products are no longer displayed
and the user needs to add a product grid element
Can be activated by adding
add_theme_support( 'avia_custom_shop_page' );
to your functions.php file
*/
if(!function_exists('avia_woocommerce_default_page'))
{
add_filter( 'pre_get_posts', 'avia_woocommerce_default_page' );
function avia_woocommerce_default_page($query)
{
if(current_theme_supports('avia_custom_shop_page'))
{
if(!$query->is_admin && $query->is_main_query() && !$query->is_tax && $query->is_archive && $query->is_post_type_archive)
{
$vars = $query->query_vars;
if(isset($vars['post_type']) && 'product' == $vars['post_type'] )
{
$shop_page_id = wc_get_page_id( 'shop' );
$builder_active = AviaHelper::builder_status($shop_page_id);
if($builder_active == "active")
{
$query->set( 'post_type', 'page' );
$query->set( 'p', $shop_page_id );
$query->set( 'meta_query', array() );
$query->is_singular = true;
$query->is_page = true;
$query->is_archive = false;
$query->is_post_type_archive = false;
$query->query = array('p'=>$shop_page_id, 'post_type' => 'page');
}
}
}
}
return $query;
}
}
if(!function_exists('avia_woocommerce_disable_editor'))
{
add_filter( 'avf_builder_button_params', 'avia_woocommerce_disable_editor' );
function avia_woocommerce_disable_editor($params)
{
if(!current_theme_supports('avia_custom_shop_page'))
{
global $post_ID;
$shop_page_id = wc_get_page_id( 'shop' );
if($post_ID == $shop_page_id)
{
$disabled = __('(disabled)', 'avia_framework');
$params['visual_label'] = $params['visual_label'] . " ".$disabled;
$params['default_label'] = $params['default_label'] . " ".$disabled;
$params['button_class'] = "av-builer-button-disabled";
$params['disabled'] = true;
$params['note'] = __('This page is set as the default WooCommerce Shop Overview and therefore does not support the Enfold advanced layout editor', 'avia_framework')."
(".__('Learn more').")";
}
}
if(avia_backend_get_post_type() == "product")
{
$params['noteclass'] = "av-notice av-only-active";
$params['note'] = __('Please note that the Advanced Layout Builder for products will not work with all WooCommerce Extensions', 'avia_framework');
}
return $params;
}
}
if(!function_exists('avia_woocommerce_disable_editor_option'))
{
add_filter( 'avf_builder_active', 'avia_woocommerce_disable_editor_option' , 10 , 2);
function avia_woocommerce_disable_editor_option($params, $post_id)
{
if(!current_theme_supports('avia_custom_shop_page'))
{
if($post_id == wc_get_page_id( 'shop' ))
{
$params = false;
}
}
return $params;
}
}
#
# place the cart button according to the header layout (top/sidebar)
#
if(!function_exists('avia_woocommerce_cart_placement'))
{
add_action( 'init', 'avia_woocommerce_cart_placement', 10);
function avia_woocommerce_cart_placement()
{
$position = avia_get_option('header_position', 'header_top') == "header_top" ? "ava_main_header" : "ava_inside_main_menu";
if(avia_get_option('cart_icon') == "always_display_menu")
{
$position = "ava_inside_main_menu";
if( strpos( avia_get_option('header_layout'), 'bottom_nav_header') !== false && avia_get_option('header_position') == 'header_top')
{
$position = "ava_before_bottom_main_menu";
}
}
add_action( $position, 'avia_woocommerce_cart_dropdown', 10);
}
}
#
# permanent display of cart button
#
if(!function_exists('avia_woocommerce_cart_pos'))
{
add_filter( 'avf_header_classes', 'avia_woocommerce_cart_pos' , 10 , 3);
function avia_woocommerce_cart_pos($class, $necessary, $prefix)
{
if($prefix == 'html_') // only for the html tag
{
global $woocommerce;
$cart = $woocommerce->cart->get_cart();
$cart_pos = avia_get_option('cart_icon');
if($cart_pos == "always_display" || (!empty($cart) && !avia_active_caching()))
{
$class[] = "visible_cart";
}
if($cart_pos == "always_display_menu")
{
$class[] = "cart_at_menu";
}
}
return $class;
}
}
function avia_woocommerce_cart_dropdown()
{
global $woocommerce, $avia_config;
$cart_subtotal = $woocommerce->cart->get_cart_subtotal();
$link = $woocommerce->cart->get_cart_url();
$id = "";
$added = wc_get_notices('success');
$trigger = !empty($added) ? "av-display-cart-on-load" : "";
if(avia_get_option('cart_icon') == "always_display_menu")
{
$id = 'id="menu-item-shop"';
}
$output = "";
$output .= "
";
echo $output;
}
/*
after importing demo pages make sure that if we got multiple shop/my account/etc pages (happens if the user used default woocommerce setup)
to remove the duplicates and set the theme options properly
*/
add_action('avia_after_import_hook', 'avia_woocommerce_set_pages');
// add_action('ava_after_main_container', 'avia_woocommerce_set_pages');
function avia_woocommerce_set_pages()
{
global $wpdb;
$pages = array(
'shop' => array(
'title' => 'Shop',
'slug' => 'shop',
),
'cart' => array(
'title' => 'Cart',
'slug' => 'cart',
),
'checkout' => array(
'title' => 'Checkout',
'slug' => 'checkout',
),
'myaccount' => array(
'title' => 'My Account',
'slug' => 'my-account',
)
);
/*query string to get multiple posts with the same name*/
$pagequery = "SELECT ID FROM $wpdb->posts WHERE post_title = %s AND post_type='page'";
foreach ($pages as $page)
{
$entries = $wpdb->get_results( $wpdb->prepare( $pagequery , $page['title'] ));
if(!empty($entries))
{
$keep = 0;
$delete = array();
//we got one post of that name. the user did not yet activate woocommerce setup or no page with that name was imported
if(count($entries) === 1)
{
$keep = $entries[0]->ID;
}
else //we got 2 or more entries. keep the one with the highest id as woocommerce setting and delete the other ones
{
foreach($entries as $entry)
{
if($entry->ID > $keep)
{
if($keep) $delete[] = $keep;
$keep = $entry->ID;
}
else
{
$delete[] = $entry->ID;
}
}
}
//delete the not required posts
foreach($delete as $delete_id)
{
wp_delete_post( $delete_id, true );
}
if($keep > 0)
{
//store the value of the $keep as the default woo setting
$setting = str_replace("-", "", $page['slug']);
update_option('woocommerce_' . $setting . '_page_id' , $keep);
//modify the page slug and remove any numbers if necessary
$update_post = array(
'ID' => $keep,
'post_name' => $page['slug']
);
wp_update_post( $update_post );
}
}
}
}
/**
* Helper functions for template builder elements - Product grids, slideshows, ......
* ==================================================================================
*
*/
if( ! function_exists( 'avia_wc_set_out_of_stock_query_params' ) )
{
/**
* Returns the query parameters for the "product out of stock" feature for selecting the products
*
* @param array $meta_query
* @param array $tax_query
* @param string $products_visibility 'show'|'hide'|'' for WC default
*/
function avia_wc_set_out_of_stock_query_params( array &$meta_query, array &$tax_query, $products_visibility = '' )
{
/**
* Backwards compatibility WC < 3.0.0
*/
if( ! avia_woocommerce_version_check( '3.0.0') )
{
$meta_query[] = WC()->query->visibility_meta_query();
$meta_query[] = WC()->query->stock_status_meta_query();
$meta_query = array_filter( $meta_query );
}
else
{
switch( $products_visibility )
{
case 'show':
$hide = 'no';
break;
case 'hide':
$hide = 'yes';
break;
default:
$hide = get_option( 'woocommerce_hide_out_of_stock_items', 'no' );
}
if( 'yes' == $hide )
{
$outofstock_term = get_term_by( 'name', 'outofstock', 'product_visibility' );
if( $outofstock_term instanceof WP_Term )
{
$tax_query[] = array(
'taxonomy' => 'product_visibility',
'field' => 'term_taxonomy_id',
'terms' => array( $outofstock_term->term_taxonomy_id ),
'operator' => 'NOT IN'
);
}
}
}
}
}
if( ! function_exists( 'avia_wc_get_product_query_order_args' ) )
{
/**
* Returns the ordering args, either the default catalog settings or the user selected
*
* @param string $order_by
* @param string $order
* @return array
*/
function avia_wc_get_product_query_order_args( $order_by = '', $order = '' )
{
$def_orderby = avia_wc_get_default_catalog_order_by();
$order_by = empty( $order_by ) ? $def_orderby['orderby'] : $order_by;
$order = empty( $order ) ? $def_orderby['order'] : $order;
// sets filter hooks !!
$ordering_args = WC()->query->get_catalog_ordering_args( $order_by, $order );
$ordering_args['orderby'] = $order_by;
$ordering_args['order'] = $order;
return $ordering_args;
}
}
if( ! function_exists( 'avia_wc_get_default_catalog_order_by' ) )
{
/**
* Returns the default settings for catalog order by and clears any set filter hook by this function
*
* @return array
*/
function avia_wc_get_default_catalog_order_by()
{
// does not always return correct values !!!
// $args = WC()->query->get_catalog_ordering_args();
$orderby_value = apply_filters( 'woocommerce_default_catalog_orderby', get_option( 'woocommerce_default_catalog_orderby' ) );
// Get order + orderby args from string
$orderby_value = explode( '-', $orderby_value );
$orderby = esc_attr( $orderby_value[0] );
$order = ! empty( $orderby_value[1] ) ? $orderby_value[1] : 'ASC';
$args = array();
$args['orderby'] = strtolower( $orderby );
$args['order'] = ( 'DESC' === strtoupper( $order ) ) ? 'DESC' : 'ASC';
$args['meta_key'] = '';
return $args;
}
}
if( ! function_exists( 'avia_wc_clear_catalog_ordering_args_filters' ) )
{
/**
* Remove all filters set by a call to WC()->query->get_catalog_ordering_args();
*/
function avia_wc_clear_catalog_ordering_args_filters()
{
remove_filter( 'posts_clauses', array( WC()->query, 'order_by_price_desc_post_clauses' ) );
remove_filter( 'posts_clauses', array( WC()->query, 'order_by_price_asc_post_clauses' ) );
remove_filter( 'posts_clauses', array( WC()->query, 'order_by_popularity_post_clauses' ) );
remove_filter( 'posts_clauses', array( WC()->query, 'order_by_rating_post_clauses' ) );
}
}
add_filter( 'woocommerce_product_is_visible', 'avia_wc_product_is_visible', 10, 2 );
if( ! function_exists( 'avia_wc_product_is_visible' ) )
{
/**
* Allows to change the default visibility for products in catalog.
*
* WC checks this in the loop when showing products on a catalog page - as we allow user to show/hide products out of stock in various
* builder elements we have to force the display even if visibility is false
*
* @param boolean $visible
* @param int $product_id
* @return boolean
*/
function avia_wc_product_is_visible( $visible, $product_id )
{
global $avia_config;
if( ! isset( $avia_config['woocommerce']['catalog_product_visibility'] ) )
{
return $visible;
}
switch( $avia_config['woocommerce']['catalog_product_visibility'] )
{
case 'show_all':
return true;
case 'hide_out_of_stock':
$product = wc_get_product( $product_id );
if( ! $product instanceof WC_Product )
{
return $visible;
}
return $product->is_in_stock();
case 'use_default':
default:
return $visible;
}
}
}