Advertisement
cipher87

config.php

Sep 5th, 2020
335
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 84.95 KB | None | 0 0
  1. <?php
  2. /**
  3.  * WooCommerce Integration
  4.  * =======================
  5.  *
  6.  * @since < 4.0
  7.  * @since 4.5.6     modifications for sorting integrations with WC 3.5.7 (backwards comp. with config-356.php)
  8.  *
  9.  * ToDo:    global $woocommerce can be replaced by WC()->
  10.  * ======================================================
  11.  *
  12.  */
  13. if ( ! defined( 'ABSPATH' ) ) {  exit;  }    // Exit if accessed directly
  14.  
  15.  
  16. function avia_woocommerce_enabled()
  17. {
  18.     // if( !function_exists( 'wc_get_template_part' ) && class_exists( 'woocommerce' )) return "deprecated";
  19.    
  20.     return class_exists( 'WooCommerce' );
  21. }
  22.  
  23. global $avia_config;
  24.  
  25. //product thumbnails
  26. $avia_config['imgSize']['shop_thumbnail']   = array( 'width' => 120, 'height' => 120 );
  27. $avia_config['imgSize']['shop_catalog']     = array( 'width' => 450, 'height' => 450 );
  28. $avia_config['imgSize']['shop_single']      = array( 'width' => 450, 'height' => 999, 'crop' => false );
  29.  
  30. avia_backend_add_thumbnail_size( $avia_config );
  31.  
  32. include( 'admin-options.php' );
  33. include( 'admin-import.php' );
  34. include( 'woocommerce-mod-css-dynamic.php' );
  35.  
  36. add_theme_support( 'woocommerce' );
  37.  
  38.  
  39. function av_add_deprecated_notice()
  40. {
  41.     echo '<div class="notice notice-error">';
  42.     echo    '<p>' .  __( 'Attention! Please update WooCommerce to the latest version to properly display your products', 'avia_framework' ) . '</p>';
  43.     echo '</div>';
  44. }
  45.  
  46.  
  47.  
  48. //check if the plugin is enabled, otherwise stop the script
  49. if( avia_woocommerce_enabled() !== true )
  50. {
  51.     if( avia_woocommerce_enabled() == "deprecated" )
  52.     {
  53.         add_action( 'admin_notices', 'av_add_deprecated_notice' );
  54.     }
  55.    
  56.     return false;
  57. }
  58.  
  59.  
  60. /**
  61.  * Checks if WooCommerce version is >= $version
  62.  *
  63.  * @since < 4.0
  64.  * @param string $version
  65.  * @return boolean
  66.  */
  67. function avia_woocommerce_version_check( $version  )
  68. {
  69.     if( version_compare( WC()->version, $version, ">=" ) )
  70.     {
  71.         return true;
  72.     }
  73.    
  74.     return false;
  75. }
  76.  
  77.  
  78.  
  79. //register my own styles, remove wootheme stylesheet
  80. if( ! is_admin() )
  81. {
  82.     add_action( 'init', 'avia_woocommerce_register_assets' );
  83. }
  84.  
  85. if( ! function_exists( 'avia_woocommerce_add_body_classes' ) )
  86. {
  87.     /**
  88.      * Add info about WC version to body
  89.      *
  90.      * @since 4.7.6.4
  91.      * @param array $classes
  92.      * @param array $class
  93.      * @return array
  94.      */
  95.     function avia_woocommerce_add_body_classes( $classes, $class )
  96.     {
  97.         if( avia_woocommerce_version_check( '3.0' ) )
  98.         {
  99.             $classes[] = 'avia-woocommerce-30';
  100.         }
  101.        
  102.         return $classes;
  103.     }
  104.    
  105.     add_filter( 'body_class', 'avia_woocommerce_add_body_classes', 10, 2 );
  106. }
  107.  
  108.  
  109.  
  110.  
  111. /**
  112.  * Wrapper function as WC deprecated function get_woocommerce_term_meta with 3.6
  113.  *
  114.  * @since 4.5.6.1
  115.  * @param int $term_id
  116.  * @param string $key
  117.  * @param bool $single
  118.  * @return mixed
  119.  */
  120. function avia_get_woocommerce_term_meta( $term_id, $key, $single = true )
  121. {
  122.     if( ! avia_woocommerce_version_check( '3.6' ) )
  123.     {
  124.         return get_woocommerce_term_meta( $term_id, $key, $single );
  125.     }
  126.    
  127.     return function_exists( 'get_term_meta' ) ? get_term_meta( $term_id, $key, $single ) : get_metadata( 'woocommerce_term', $term_id, $key, $single );
  128. }
  129.  
  130.  
  131. function avia_woocommerce_register_assets()
  132. {
  133.     wp_enqueue_style( 'avia-woocommerce-css', AVIA_BASE_URL . 'config-woocommerce/woocommerce-mod.css' );
  134.    
  135.     if( version_compare( WC()->version, '2.7.0', '<' ) )
  136.     {
  137.         wp_enqueue_script( 'avia-woocommerce-js', AVIA_BASE_URL . 'config-woocommerce/woocommerce-mod-v26.js', array( 'jquery' ), 1, true );
  138.     }
  139.     else
  140.     {
  141.         wp_enqueue_script( 'avia-woocommerce-js', AVIA_BASE_URL . 'config-woocommerce/woocommerce-mod.js', array( 'jquery' ), 1, true );
  142.     }
  143.    
  144. }
  145.  
  146.  
  147.  
  148.  
  149.  
  150. global $woocommerce;
  151.  
  152. if(version_compare($woocommerce->version, "2.1", "<"))
  153. {
  154.     define('WOOCOMMERCE_USE_CSS', false);
  155. }
  156. else
  157. {
  158.     add_filter( 'woocommerce_enqueue_styles', 'avia_woocommerce_enqueue_styles' );
  159.     function avia_woocommerce_enqueue_styles($styles)
  160.     {
  161.         $styles = array();
  162.         return $styles;
  163.     }
  164. }
  165.  
  166.  
  167. if ( class_exists( 'WC_Bookings' ) )
  168. {
  169.     require_once( 'config-woocommerce-bookings/config.php' ); //compatibility with woocommerce plugin
  170. }
  171.  
  172.  
  173.  
  174.  
  175.  
  176.  
  177.  
  178.  
  179. ######################################################################
  180. # config
  181. ######################################################################
  182.  
  183. //add avia_framework config defaults
  184.  
  185. $avia_config['shop_overview_column']  = get_option('avia_woocommerce_column_count');  // columns for the overview page
  186. $avia_config['shop_overview_products']= get_option('avia_woocommerce_product_count'); // products for the overview page
  187.  
  188. $avia_config['shop_single_column']       = 4;           // columns for related products and upsells
  189. $avia_config['shop_single_column_items'] = 4;   // number of items for related products and upsells
  190. $avia_config['shop_overview_excerpt'] = false;      // display excerpt
  191.  
  192. if(!$avia_config['shop_overview_column']) $avia_config['shop_overview_column'] = 3;
  193.  
  194. /**
  195.  * Setup product gallery support depending on user settings and available WooCommerce galleries
  196.  */
  197. if( ! function_exists( 'avia_woocommerce_product_gallery_support_setup' ) )
  198. {
  199.     if ( did_action( 'woocommerce_init' ) )
  200.     {
  201.         avia_woocommerce_product_gallery_support_setup();
  202.     }
  203.     else
  204.     {
  205.         add_action( 'woocommerce_init', 'avia_woocommerce_product_gallery_support_setup', 10);
  206.     }
  207.    
  208.     function avia_woocommerce_product_gallery_support_setup()
  209.     {
  210.         if( ! avia_woocommerce_version_check( '3.0.0' ) )
  211.         {
  212.             return;
  213.         }
  214.        
  215.         $options = avia_get_option();
  216.        
  217.         //  Fallback, if options have not been saved
  218.         if( ! array_key_exists( 'product_gallery', $options ) || ( 'wc_30_gallery' != $options['product_gallery'] ) )
  219.         {
  220.             $options['product_gallery'] = '';
  221.         }
  222.        
  223.         if( 'wc_30_gallery' == $options['product_gallery'] )
  224.         {
  225.             add_theme_support( 'wc-product-gallery-zoom' );
  226.                 //  uncomment the following line if you want default WooCommerce lightbox - else Enfold lightbox will be used
  227. //          add_theme_support( 'wc-product-gallery-lightbox' );
  228.             add_theme_support( 'wc-product-gallery-slider' );
  229.             add_theme_support( 'avia-wc-30-product-gallery-feature' );
  230.         }
  231.        
  232.         return;
  233.     }
  234. }
  235.  
  236. ######################################################################
  237. # Allow to add WC structured data on template builder page
  238. ######################################################################
  239. #
  240.  
  241. add_action( 'get_footer', 'avia_activate_wc_structured_data', 10, 1 );
  242.  
  243.  
  244. if( ! function_exists( 'avia_activate_wc_structured_data' ) )
  245. {
  246.     /**
  247.      *
  248.      * @param type $name
  249.      */
  250.     function avia_activate_wc_structured_data( $name )
  251.     {
  252.         global $product;
  253.        
  254.         if( ! avia_woocommerce_version_check( '3.0.0') )
  255.         {
  256.             return;
  257.         }
  258.        
  259.         //  Currently only on single product page with template builder required
  260.         if( ! is_product() ||  ! $product instanceof WC_Product )
  261.         {
  262.             return;
  263.         }
  264.        
  265.         /**
  266.          * Check necessary data in \woocommerce\includes\class-wc-structured-data.php    
  267.          */
  268.         if( ! did_action( 'woocommerce_before_main_content' ) )
  269.         {
  270.             WC()->structured_data->generate_website_data();
  271.         }
  272.        
  273.         if( ! ( did_action( 'woocommerce_shop_loop' ) || did_action( 'woocommerce_single_product_summary' ) ) )
  274.         {
  275.             WC()->structured_data->generate_product_data();
  276.         }
  277.        
  278.             //  not needed on single product page
  279.         if( ! did_action( 'woocommerce_breadcrumb' ) )
  280.         {
  281. //          WC()->structured_data->generate_breadcrumblist_data();
  282.         }
  283.         if( ! did_action( 'woocommerce_review_meta' ) )
  284.         {
  285. //          WC()->structured_data->generate_review_data();
  286.         }
  287.         if( ! did_action( 'woocommerce_email_order_details' ) )
  288.         {
  289. //          WC()->structured_data->generate_order_data();
  290.         }
  291.     }
  292. }
  293.  
  294. ######################################################################
  295. # Create the correct template html structure
  296. ######################################################################
  297.  
  298. //remove woo defaults
  299. remove_action( 'woocommerce_sidebar', 'woocommerce_get_sidebar', 10);
  300. remove_action( 'woocommerce_before_main_content', 'woocommerce_output_content_wrapper', 10);
  301. remove_action( 'woocommerce_after_main_content', 'woocommerce_output_content_wrapper_end', 10);
  302. remove_action( 'woocommerce_after_shop_loop_item', 'woocommerce_template_loop_add_to_cart', 10);
  303. remove_action( 'woocommerce_before_main_content', 'woocommerce_breadcrumb', 20, 0);
  304. remove_action( 'woocommerce_pagination', 'woocommerce_catalog_ordering', 20 );
  305. remove_action( 'woocommerce_pagination', 'woocommerce_pagination', 10 );
  306. remove_action( 'woocommerce_before_single_product', array($woocommerce, 'show_messages'), 10);
  307.  
  308.  
  309.  
  310. //add theme actions && filter
  311. add_action( 'woocommerce_after_shop_loop_item_title', 'avia_woocommerce_overview_excerpt', 10);
  312. add_filter( 'loop_shop_columns', 'avia_woocommerce_loop_columns');
  313. add_filter( 'loop_shop_per_page', 'avia_woocommerce_product_count' );
  314.  
  315. //single page adds
  316. add_action( 'avia_add_to_cart', 'woocommerce_template_single_add_to_cart', 30, 2 );
  317.  
  318.  
  319.  
  320. /*update woocommerce v2*/
  321.  
  322. remove_action( 'woocommerce_before_shop_loop', 'woocommerce_result_count', 20 ); /*remove result count above products*/
  323. remove_action( 'woocommerce_before_shop_loop', 'woocommerce_catalog_ordering', 30 ); /*remove woocommerce ordering dropdown*/
  324. remove_action( 'woocommerce_after_shop_loop_item_title', 'woocommerce_template_loop_rating', 5 ); //remove rating
  325. remove_action( 'woocommerce_after_shop_loop', 'woocommerce_pagination', 10 ); //remove woo pagination
  326.  
  327.  
  328.  
  329. ######################################################################
  330. # FUNCTIONS
  331. ######################################################################
  332.  
  333. #
  334. # set the shop page id, otherwise avia_get_the_ID() can return a wrong id on the shop page
  335. #
  336. add_filter( 'avf_avia_get_the_ID', 'avia_set_shop_page_id', 10, 1 );
  337.  
  338. function avia_set_shop_page_id( $id )
  339. {
  340.     if( is_shop() )
  341.     {
  342.         $id = function_exists( 'wc_get_page_id' ) ? wc_get_page_id( 'shop' ) : woocommerce_get_page_id( 'shop' );
  343.     }
  344.    
  345.     return $id;
  346. }
  347.  
  348. if( ! function_exists( 'avia_woocommerce_thumbnail' ) )
  349. {
  350.     /**
  351.      * removes the default post image from shop overview pages and replaces it with this image
  352.      */
  353.     add_action( 'woocommerce_before_shop_loop_item_title', 'avia_woocommerce_thumbnail', 10 );
  354.     remove_action( 'woocommerce_before_shop_loop_item_title', 'woocommerce_template_loop_product_thumbnail', 10 );
  355.  
  356.     function avia_woocommerce_thumbnail()
  357.     {
  358.         global $product;
  359.  
  360.         if( function_exists( 'wc_get_rating_html' ) )
  361.         {
  362.             $rating = wc_get_rating_html( $product->get_average_rating() );
  363.         }
  364.         else
  365.         {
  366.             $rating = $product->get_rating_html(); //get rating
  367.         }
  368.  
  369.         $id = get_the_ID();
  370.         $size = 'shop_catalog';
  371.  
  372.         $image = get_the_post_thumbnail( $id , $size );
  373.        
  374.         //  try to get fallback image
  375.         if( empty( $image ) )
  376.         {
  377.             $image_url = wc_placeholder_img_src( $size );
  378.             if( ! empty( $image_url ) )
  379.             {
  380.                 $image = '<img src="' . $image_url . '" height="450" width="450" loading="lazy" alt="' . __( 'Placeholder image', 'avia_framework' ) . '">';
  381.             }
  382.         }
  383.        
  384.         $html  = "<div class='thumbnail_container'>";
  385.         $html .=    avia_woocommerce_gallery_first_thumbnail( $id , $size );
  386.         $html .=    $image;
  387.            
  388.         if( ! empty( $rating ) )
  389.         {
  390.             $html .= "<span class='rating_container'>{$rating}</span>";
  391.         }
  392.         if( $product->get_type() == 'simple' )
  393.         {
  394.             $html .= "<span class='cart-loading'></span>";
  395.         }
  396.            
  397.         $html .= '</div>';
  398.        
  399.         echo $html;
  400.     }
  401. }
  402.  
  403. if( ! function_exists( 'avia_woocommerce_gallery_first_thumbnail' ) )
  404. {
  405.     /**
  406.      *
  407.      * @param int $id
  408.      * @param string $size
  409.      * @param boolean $id_only
  410.      * @return string|int
  411.      */
  412.     function avia_woocommerce_gallery_first_thumbnail( $id, $size, $id_only = false )
  413.     {
  414.         $image = '';
  415.         $active_hover = get_post_meta( $id, '_product_hover', true );
  416.  
  417.         if( ! empty( $active_hover ) )
  418.         {
  419.             $product_gallery = get_post_meta( $id, '_product_image_gallery', true );
  420.  
  421.             if( ! empty( $product_gallery ) )
  422.             {
  423.                 $gallery = explode( ',',$product_gallery );
  424.                 $image_id = $gallery[0];
  425.  
  426.                 //return id only
  427.                 if( ! empty( $id_only ) )
  428.                 {
  429.                     return $image_id;
  430.                 }
  431.  
  432.                 $image = wp_get_attachment_image( $image_id, $size, false, array( 'class' => "attachment-$size avia-product-hover" ) );
  433.             }
  434.  
  435.             return $image;
  436.         }
  437.     }
  438. }
  439.  
  440.  
  441.  
  442.  
  443. #
  444. # add ajax cart / options buttons to the product
  445. #
  446.  
  447. add_action( 'woocommerce_after_shop_loop_item', 'avia_add_cart_button', 16);
  448. function avia_add_cart_button()
  449. {
  450.     global $product, $avia_config;
  451.  
  452.     if ($product->get_type() == 'bundle' ){
  453.         $product = new WC_Product_Bundle($product->get_id());
  454.     }
  455.  
  456.     $extraClass  = "";
  457.  
  458.     ob_start();
  459.     woocommerce_template_loop_add_to_cart();
  460.     $output = ob_get_clean();
  461.  
  462.     if(!empty($output))
  463.     {
  464.         $pos = strpos($output, ">");
  465.  
  466.         if ($pos !== false) {
  467.             $output = substr_replace($output,"><span ".av_icon_string('cart')."></span> ", $pos , strlen(1));
  468.         }
  469.     }
  470.  
  471.  
  472.     if($product->get_type() == 'variable' && empty($output))
  473.     {
  474.         $output = '<a class="add_to_cart_button button product_type_variable" href="'.get_permalink($product->get_id()).'"><span '.av_icon_string("details").'></span> '.__("Select options","avia_framework").'</a>';
  475.     }
  476.  
  477.     if(in_array($product->get_type(), array('subscription', 'simple', 'bundle')))
  478.     {
  479.         $output .= '<a class="button show_details_button" href="'.get_permalink($product->get_id()).'"><span '.av_icon_string("details").'></span>  '.__("Show Details","avia_framework").'</a>';
  480.     }
  481.     else
  482.     {
  483.         $extraClass  = "single_button";
  484.     }
  485.  
  486.     if(empty($extraClass)) $output .= " <span class='button-mini-delimiter'></span>";
  487.  
  488.  
  489.     if($output && !post_password_required() && '' == avia_get_option('product_layout',''))
  490.     {
  491.         echo "<div class='avia_cart_buttons $extraClass'>$output</div>";
  492.     }
  493. }
  494.  
  495.  
  496.  
  497.  
  498.  
  499. #
  500. # wrap products on overview pages into an extra div for improved styling options. adds "product_on_sale" class if prodct is on sale
  501. #
  502.  
  503. add_action( 'woocommerce_before_shop_loop_item', 'avia_shop_overview_extra_div', 5);
  504. function avia_shop_overview_extra_div()
  505. {
  506.     global $product;
  507.     $product_class = $product->is_on_sale() ? "product_on_sale" : "";
  508.     $product_class.= " av-product-class-".avia_get_option('product_layout');
  509.  
  510.     echo "<div class='inner_product main_color wrapped_style noLightbox $product_class'>";
  511. }
  512.  
  513. add_action( 'woocommerce_after_shop_loop_item',  'avia_close_div', 1000);
  514. function avia_close_div()
  515. {
  516.     echo "</div>";
  517. }
  518.  
  519.  
  520. #
  521. # wrap product titles and sale number on overview pages into an extra div for improved styling options
  522. #
  523.  
  524. add_action( 'woocommerce_before_shop_loop_item_title', 'avia_shop_overview_extra_header_div', 20);
  525. function avia_shop_overview_extra_header_div()
  526. {
  527.     echo "<div class='inner_product_header'><div class='avia-arrow'></div>";
  528.     echo    "<div class='inner_product_header_table'>";
  529.     echo        "<div class='inner_product_header_cell'>";
  530. }
  531.  
  532. add_action( 'woocommerce_after_shop_loop_item_title',  'avia_close_div', 1000);
  533. add_action( 'woocommerce_after_shop_loop_item_title',  'avia_close_div', 1001);
  534. add_action( 'woocommerce_after_shop_loop_item_title',  'avia_close_div', 1002);
  535.  
  536.  
  537. #
  538. # remove on sale badge from usual location and add it to the bottom of the product
  539. #
  540. remove_action( 'woocommerce_before_shop_loop_item_title', 'woocommerce_show_product_loop_sale_flash', 10);
  541. add_action( 'woocommerce_after_shop_loop_item_title', 'woocommerce_show_product_loop_sale_flash', 10);
  542.  
  543.  
  544. #
  545. # create the shop navigation with account links, as well as cart and checkout, called as fallback function by the wp_nav_menu function in header.php
  546. #
  547. function avia_shop_nav($args)
  548. {
  549.     $output = "";
  550.     $url = avia_collect_shop_urls();
  551.  
  552.     $output .= "<ul>";
  553.  
  554.     if( is_user_logged_in() )
  555.     {
  556.         $current = $sub1 = $sub2 = $sub3 = "";
  557.         if(is_account_page()) $current = "current-menu-item";
  558.         if(is_page(get_option('woocommerce_change_password_page_id'))) $sub1 = "current-menu-item";
  559.         if(is_page(get_option('woocommerce_edit_address_page_id'))) $sub2 = "current-menu-item";
  560.         if(is_page(get_option('woocommerce_view_order_page_id'))) $sub3 = "current-menu-item";
  561.  
  562.  
  563.         $output .= "<li class='$current account_overview_link'><a href='".$url['account_overview']."'>".__('My Account', 'avia_framework')."</a>";
  564.             $output .= "<ul>";
  565.             $output .= "<li class='$sub1 account_change_pw_link'><a href='".$url['account_change_pw']."'>".__('Change Password', 'avia_framework')."</a></li>";
  566.             $output .= "<li class='$sub2 account_edit_adress_link'><a href='".$url['account_edit_adress']."'>".__('Edit Address', 'avia_framework')."</a></li>";
  567.             $output .= "<li class='$sub3 account_view_order_link'><a href='".$url['account_view_order']."'>".__('View Order', 'avia_framework')."</a></li>";
  568.             $output .= "</ul>";
  569.         $output .= "</li>";
  570.         $output .= "<li class='account_logout_link'><a href='".$url['logout']."'>".__('Log Out', 'avia_framework')."</a></li>";
  571.     }
  572.     else
  573.     {
  574.         $sub1 = $sub2 = "";
  575.         if(is_page(get_option('woocommerce_myaccount_page_id')))
  576.         {
  577.             if(isset($_GET['account_visible']) && $_GET['account_visible'] == 'register') $sub1 = "current-menu-item";
  578.             if(isset($_GET['account_visible']) && $_GET['account_visible'] == 'login') $sub2 = "current-menu-item";
  579.         }
  580.  
  581.         $url_param = strpos($url['account_overview'], '?') === false ? "?" : "&";
  582.  
  583.         if (get_option('woocommerce_enable_myaccount_registration') =='yes')
  584.         {
  585.             $output .= "<li class='register_link $sub1'><a href='".$url['account_overview'].$url_param."account_visible=register'>".__('Register', 'avia_framework')."</a></li>";
  586.         }
  587.  
  588.         $output .= "<li class='login_link $sub2'><a href='".$url['account_overview'].$url_param."account_visible=login'>".__('Log In', 'avia_framework')."</a></li>";
  589.     }
  590.  
  591.     $output .= "</ul>";
  592.  
  593.     if($args['echo'] == true)
  594.     {
  595.         echo $output;
  596.     }
  597.     else
  598.     {
  599.         return $output;
  600.     }
  601. }
  602.  
  603.  
  604. #
  605. # helper function that collects all the necessary urls for the shop navigation
  606. #
  607.  
  608. function avia_collect_shop_urls()
  609. {
  610.     global $woocommerce;
  611.  
  612.     $url['cart']                = $woocommerce->cart->get_cart_url();
  613.     $url['checkout']            = $woocommerce->cart->get_checkout_url();
  614.     $url['account_overview']    = get_permalink(get_option('woocommerce_myaccount_page_id'));
  615.     $url['account_edit_adress'] = get_permalink(get_option('woocommerce_edit_address_page_id'));
  616.     $url['account_view_order']  = get_permalink(get_option('woocommerce_view_order_page_id'));
  617.     $url['account_change_pw']   = get_permalink(get_option('woocommerce_change_password_page_id'));
  618.     $url['logout']              = wp_logout_url(home_url('/'));
  619.  
  620.     return $url;
  621. }
  622.  
  623.  
  624.  
  625.  
  626. #
  627. # check which page is displayed and if the automatic sidebar menu for subpages should be prevented
  628. #
  629. add_filter( 'avf_sidebar_menu_filter', 'avia_woocommerce_sidebar_filter');
  630.  
  631. function avia_woocommerce_sidebar_filter($menu)
  632. {
  633.     $id = avia_get_the_ID();
  634.     if(is_cart() || is_checkout() || get_option('woocommerce_thanks_page_id') == $id){$menu = "";}
  635.     return $menu;
  636. }
  637.  
  638.  
  639. #
  640. # check if a single product is displayed and always set the sidebar styling to that of a right sidebar
  641. #
  642. add_filter( 'avf_sidebar_position', 'avia_woocommerce_sidebar_pos');
  643.  
  644. function avia_woocommerce_sidebar_pos($sidebar)
  645. {
  646.     if(is_product())
  647.     {
  648.         $sidebar = "sidebar_right";
  649.     }
  650.  
  651.     return $sidebar;
  652. }
  653.  
  654.  
  655.  
  656. function avia_add_to_cart($post, $product )
  657. {
  658.     echo "<div class='avia_cart avia_cart_".$product->get_type()."'>";
  659.     do_action( 'avia_add_to_cart', $post, $product );
  660.     echo "</div>";
  661. }
  662.  
  663.  
  664.  
  665. #
  666. # replace thumbnail image size with full size image on single pages
  667. #
  668. /*
  669.  
  670. add_filter( 'single_product_small_thumbnail_size', 'avia_woocommerce_thumb_size');
  671.  
  672. function avia_woocommerce_thumb_size()
  673. {
  674.     return 'shop_single';
  675. }
  676. */
  677.  
  678.  
  679.  
  680.  
  681. #
  682. # if we are viewing a woocommerce page modify the breadcrumb nav
  683. #
  684.  
  685. if(!function_exists('avia_woocommerce_breadcrumb'))
  686. {
  687.     add_filter('avia_breadcrumbs_trail','avia_woocommerce_breadcrumb', 10, 2 );
  688.  
  689.     function avia_woocommerce_breadcrumb( $trail, $args )
  690.     {
  691.         global $avia_config;
  692.  
  693.         if(is_woocommerce())
  694.         {
  695.             $front_id   = avia_get_option('frontpage');
  696.             $home       = isset( $trail[0] ) ? $trail[0] : '';
  697.             $last       = array_pop($trail);
  698.             $shop_id    = function_exists( 'wc_get_page_id' ) ? wc_get_page_id( 'shop' ) : woocommerce_get_page_id( 'shop' );
  699.             $taxonomy   = "product_cat";
  700.            
  701.             // on the shop frontpage simply display the shop name, rather than shop name + "All Products"
  702.             if(is_shop())
  703.             {
  704.                 if(!empty($shop_id) && $shop_id  != -1)  $trail = array_merge( $trail, avia_breadcrumbs_get_parents( $shop_id ) );
  705.                 $last = "";
  706.  
  707.                 if(is_search())
  708.                 {
  709.                     $last = __('Search results for:','avia_framework').' '.esc_attr($_GET['s']);
  710.                 }
  711.             }
  712.  
  713.             // on the product page single page modify the breadcrumb to read [home] [if available:parent shop pages] [shop] [if available:parent categories] [category] [title]
  714.             if(is_product())
  715.             {
  716.                 //fetch all product categories and search for the ones with parents. if none are avalaible use the first category found
  717.                 $product_category = $parent_cat = array();
  718.                 $temp_cats = get_the_terms(get_the_ID(), $taxonomy);
  719.  
  720.                 if( is_array( $temp_cats ) && ! empty( $temp_cats ) )
  721.                 {
  722.                     foreach( $temp_cats as $key => $cat )
  723.                     {
  724.                         if($cat->parent != 0 && !in_array($cat->term_taxonomy_id, $parent_cat))
  725.                         {
  726.                             $product_category[] = $cat;
  727.                             $parent_cat[] = $cat->parent;
  728.                         }
  729.                     }
  730.  
  731.                     //if no categories with parents use the first one
  732.                     if(empty($product_category)) $product_category[] = reset($temp_cats);
  733.  
  734.                 }
  735.                 //unset the trail and build our own
  736.                 unset($trail);
  737.  
  738.                 $trail = ( empty( $home ) ) ? array() : array( 0 => $home );
  739.                 if(!empty($shop_id) && $shop_id  != -1)    $trail = array_merge( $trail, avia_breadcrumbs_get_parents( $shop_id ) );
  740.                 if(!empty($parent_cat)) $trail = array_merge( $trail, avia_breadcrumbs_get_term_parents( $parent_cat[0] , $taxonomy ) );
  741.                 if(!empty($product_category)) $trail[] = '<a href="' . get_term_link( $product_category[0]->slug, $taxonomy ) . '" title="' . esc_attr( $product_category[0]->name ) . '">' . $product_category[0]->name . '</a>';
  742.  
  743.             }
  744.  
  745.  
  746.             // add the [shop] trail to category/tag pages: [home] [if available:parent shop pages] [shop] [if available:parent categories] [category/tag]
  747.             if(is_product_category() || is_product_tag())
  748.             {
  749.                 if(!empty($shop_id) && $shop_id  != -1)
  750.                 {
  751.                     $shop_trail = avia_breadcrumbs_get_parents( $shop_id ) ;
  752.                     array_splice($trail, 1, 0, $shop_trail);
  753.                 }
  754.             }
  755.  
  756.             if(is_product_tag())
  757.             {
  758.                 $last = __("Tag",'avia_framework').": ".$last;
  759.             }
  760.  
  761.             if( ! empty( $last ) )
  762.             {
  763.                 $trail['trail_end'] = $last;
  764.             }
  765.            
  766.             /**
  767.              * Allow to remove "Shop" in breadcrumb when shop page is frontpage
  768.              *
  769.              * @since 4.2.7
  770.              */
  771.             $trail_count = count( $trail );
  772.             if( ( $front_id == $shop_id ) && ! empty( $home ) && ( $trail_count > 1 ) )
  773.             {
  774.                 $hide = apply_filters( 'avf_woocommerce_breadcrumb_hide_shop', 'hide', $trail, $args );
  775.                 if( 'hide' == $hide )
  776.                 {
  777.                     $title = get_the_title( $shop_id );
  778.                    
  779.                     for( $i = 1; $i < $trail_count; $i++ )
  780.                     {
  781.                         if( false !== strpos( $trail[ $i ], $title ) )
  782.                         {
  783.                             unset( $trail[ $i ] );
  784.                             break;
  785.                         }
  786.                     }
  787.                    
  788.                     $trail = array_merge( $trail );
  789.                 }
  790.             }
  791.         }
  792.  
  793.         return $trail;
  794.     }
  795.  
  796. }
  797.  
  798.  
  799.  
  800. #
  801. # creates the avia framework container arround the shop pages
  802. #
  803. add_action( 'woocommerce_before_main_content', 'avia_woocommerce_before_main_content', 10);
  804.  
  805.  
  806. function avia_woocommerce_before_main_content()
  807. {
  808.     global $avia_config;
  809.  
  810.     if(!isset($avia_config['shop_overview_column'])) $avia_config['shop_overview_column'] = "auto";
  811.    
  812.     $id = get_option('woocommerce_shop_page_id');
  813.     $layout = get_post_meta($id, 'layout', true);
  814.    
  815.     if(!empty($layout))
  816.     {
  817.             $avia_config['layout']['current'] = $avia_config['layout'][$layout];
  818.             $avia_config['layout']['current']['main'] = $layout;
  819.     }
  820.  
  821.         $avia_config['layout'] = apply_filters('avia_layout_filter', $avia_config['layout'], $id);
  822.  
  823.     $title_args = array();
  824.  
  825.     if(is_woocommerce())
  826.     {
  827.         $t_link = "";
  828.  
  829.         if(is_shop()) $title  = get_option('woocommerce_shop_page_title');
  830.  
  831.         $shop_id = function_exists( 'wc_get_page_id' ) ? wc_get_page_id( 'shop' ) : woocommerce_get_page_id( 'shop' );
  832.         if($shop_id && $shop_id != -1)
  833.         {
  834.             if(empty($title)) $title = get_the_title($shop_id);
  835.             $t_link = get_permalink($shop_id);
  836.         }
  837.  
  838.         if(empty($title)) $title  = __("Shop",'avia_framework');
  839.  
  840.         if(is_product_category() || is_product_tag())
  841.         {
  842.             global $wp_query;
  843.             $tax = $wp_query->get_queried_object();
  844.             $title = $tax->name;
  845.             $t_link = '';
  846.         }
  847.  
  848.         $title_args = array('title' => $title, 'link' => $t_link);
  849.     }
  850.  
  851.     if( get_post_meta(get_the_ID(), 'header', true) != 'no') echo avia_title($title_args);
  852.    
  853.    
  854.     if(is_singular()) {
  855.        
  856.         $result = 'sidebar_right';
  857.         $avia_config['layout']['current'] = $avia_config['layout'][$result];
  858.         $avia_config['layout']['current']['main'] = $result;
  859.        
  860.     }
  861.     $sidebar_setting = avia_layout_class( 'main' , false );
  862.     echo "<div class='container_wrap container_wrap_first main_color {$sidebar_setting} template-shop shop_columns_".$avia_config['shop_overview_column']."'>";
  863.  
  864.         echo "<div class='container'>";
  865.  
  866.         if(!is_singular()) { $avia_config['overview'] = true; }
  867. }
  868.  
  869. #
  870. # closes the avia framework container arround the shop pages
  871. #
  872.  
  873. add_action( 'woocommerce_after_main_content', 'avia_woocommerce_after_main_content', 10);
  874. function avia_woocommerce_after_main_content()
  875. {
  876.  
  877.     global $avia_config;
  878.     $avia_config['currently_viewing'] = "shop";
  879.  
  880.             //reset all previous queries
  881.             wp_reset_query();
  882.  
  883.             //get the sidebar
  884.             if(!is_singular())
  885.             get_sidebar();
  886.  
  887.     //  echo "</div>"; // end container - gets already closed at the top of footer.php
  888.  
  889.         echo "</div>"; // end tempate-shop content
  890.         echo "</div>"; // close default .container_wrap element
  891. }
  892.  
  893.  
  894. add_action( 'avf_custom_sidebar', 'avia_woocommerce_custom_sidebar', 10);
  895. function avia_woocommerce_custom_sidebar($sidebar)
  896. {
  897.     if(is_shop())
  898.     {
  899.         $the_id = function_exists( 'wc_get_page_id' ) ? wc_get_page_id( 'shop' ) : woocommerce_get_page_id( 'shop' );
  900.         $sidebar = get_post_meta($the_id, 'sidebar', true);
  901.     }
  902.    
  903.     return $sidebar;
  904. }
  905.  
  906.  
  907.  
  908.  
  909. #
  910. # wrap an empty product search into extra div
  911. #
  912. add_action( 'woocommerce_before_main_content', 'avia_woocommerce_404_search', 9111);
  913. function avia_woocommerce_404_search()
  914. {
  915.     global $wp_query;
  916.  
  917.     if( (is_search() || is_archive()) && empty($wp_query->found_posts) )
  918.     {
  919.         echo "<div class='template-page template-search template-search-none content ".avia_layout_class( 'content', false )." units'>";
  920.         echo "<div class='entry entry-content-wrapper' id='search-fail'>";
  921.     }
  922. }
  923.  
  924. add_action( 'woocommerce_after_main_content', 'avia_woocommerce_404_search_close', 1);
  925. function avia_woocommerce_404_search_close()
  926. {
  927.     global $wp_query;
  928.  
  929.     if( (is_search() || is_shop() || is_archive()) && empty($wp_query->found_posts) )
  930.     {
  931.         get_template_part('includes/error404');
  932.         echo "</div>";
  933.         echo "</div>"; // close default .container_wrap element
  934.     }
  935. }
  936.  
  937.  
  938.  
  939.  
  940. #
  941. # modifies the class of a page so we can display single login and single register
  942. #
  943. add_filter( 'avia_layout_class_filter_main', 'avia_register_login_class');
  944.  
  945. function avia_register_login_class($layout)
  946. {
  947.     if(isset($_GET['account_visible']))
  948.     {
  949.         if($_GET['account_visible'] == 'register') $layout .= " template-register";
  950.         if($_GET['account_visible'] == 'login') $layout .= " template-login";
  951.     }
  952.  
  953.     return $layout;
  954. }
  955.  
  956.  
  957.  
  958.  
  959.  
  960.  
  961.  
  962. #
  963. # creates the avia framework content container arround the shop loop
  964. #
  965. add_action( 'woocommerce_before_shop_loop', 'avia_woocommerce_before_shop_loop', 1);
  966.  
  967. function avia_woocommerce_before_shop_loop()
  968. {
  969.  
  970.     global $avia_config;
  971.     if(isset($avia_config['dynamic_template'])) return;
  972.     $markup = avia_markup_helper(array('context' => 'content','echo'=>false,'post_type'=>'products'));
  973.     echo "<main class='template-shop content ".avia_layout_class( 'content' , false)." units' $markup><div class='entry-content-wrapper'>";
  974. }
  975.  
  976. #
  977. # closes the avia framework content container around the shop loop
  978. #
  979. add_action( 'woocommerce_after_shop_loop', 'avia_woocommerce_after_shop_loop', 10);
  980.  
  981. function avia_woocommerce_after_shop_loop()
  982. {
  983.             global $avia_config;
  984.             if(isset($avia_config['dynamic_template'])) return;
  985.             if(isset($avia_config['overview'] )) echo avia_pagination('', 'nav');
  986.             echo "</div></main>"; //end content
  987. }
  988.  
  989.  
  990.  
  991. #
  992. # echo the excerpt
  993. #
  994. function avia_woocommerce_overview_excerpt()
  995. {
  996.     global $avia_config;
  997.  
  998.     if(!empty($avia_config['shop_overview_excerpt']))
  999.     {
  1000.         echo "<div class='product_excerpt'>";
  1001.         the_excerpt();
  1002.         echo "</div>";
  1003.     }
  1004. }
  1005.  
  1006.  
  1007.  
  1008. #
  1009. # creates the preview images based on page/category image
  1010. #
  1011. remove_action( 'woocommerce_archive_description', 'woocommerce_taxonomy_archive_description', 10 );
  1012. remove_action( 'woocommerce_product_archive_description', 'woocommerce_product_archive_description', 10 );
  1013.  
  1014. add_action( 'woocommerce_before_shop_loop', 'avia_woocommerce_overview_banner_image', 10);
  1015. add_action( 'woocommerce_before_shop_loop', 'woocommerce_taxonomy_archive_description', 11 );
  1016. //add_action( 'woocommerce_before_shop_loop', 'woocommerce_product_archive_description', 12 ); //causes warning
  1017.  
  1018.  
  1019. function avia_woocommerce_overview_banner_image()
  1020. {
  1021.     global $avia_config;
  1022.     if(avia_is_dynamic_template() || is_paged() || is_search() ) return false;
  1023.  
  1024.     $image_size = "entry_with_sidebar";
  1025.     $layout = avia_layout_class( 'main' , false );
  1026.     if($layout == 'fullsize') $image_size = 'entry_without_sidebar';
  1027.  
  1028.     if(is_shop())
  1029.     {
  1030.         $shop_id = function_exists( 'wc_get_page_id' ) ? wc_get_page_id( 'shop' ) : woocommerce_get_page_id( 'shop' );
  1031.         if($shop_id != -1)
  1032.         {
  1033.             $image = get_the_post_thumbnail($shop_id, $image_size);
  1034.             if($image) echo "<div class='page-thumb'>{$image}</div>";
  1035.         }
  1036.     }
  1037.    
  1038.    
  1039.  
  1040.     if(is_product_category())
  1041.     {
  1042.         global $wp_query;
  1043.         $image  = "";
  1044.         if(isset($wp_query->query_vars['taxonomy']))
  1045.         {
  1046.             $term = get_term_by( 'slug', get_query_var($wp_query->query_vars['taxonomy']), $wp_query->query_vars['taxonomy']);
  1047.  
  1048.             if(!empty($term->term_id))
  1049.             {
  1050.                 $attachment_id  = avia_get_woocommerce_term_meta($term->term_id, 'thumbnail_id');
  1051.                 $style          = avia_get_woocommerce_term_meta($term->term_id, 'av_cat_styling');
  1052.                
  1053.                 if(!empty($attachment_id) && empty($style))
  1054.                 {
  1055.                     $image = wp_get_attachment_image( $attachment_id, $image_size, false, array('class'=>'category_thumb'));
  1056.                     if($image) echo "<div class='page-thumb'>{$image}</div>";
  1057.                 }
  1058.                
  1059.             }
  1060.         }
  1061.     }
  1062.  
  1063. }
  1064.  
  1065.  
  1066.  
  1067. add_action( 'ava_after_main_container', 'avia_woocommerce_big_cat_banner', 11 );
  1068.  
  1069.  
  1070. function avia_woocommerce_big_cat_banner()
  1071. {
  1072.     if(is_product_category())
  1073.     {
  1074.         global $wp_query, $avia_config;
  1075.        
  1076.         if(isset($wp_query->query_vars['taxonomy']))
  1077.         {  
  1078.             $term = get_term_by( 'slug', get_query_var($wp_query->query_vars['taxonomy']), $wp_query->query_vars['taxonomy']);
  1079.             if( ! empty( $term->term_id ) )
  1080.             {
  1081.                 $description    = term_description() ;
  1082.                 $style      = avia_get_woocommerce_term_meta( $term->term_id, 'av_cat_styling' );
  1083.                 $attachment_id  = avia_get_woocommerce_term_meta( $term->term_id, 'thumbnail_id' );
  1084.                
  1085.                 $overlay    = avia_get_woocommerce_term_meta( $term->term_id, 'av-banner-overlay' );
  1086.                 $font       = avia_get_woocommerce_term_meta( $term->term_id, 'av-banner-font' );
  1087.                 $opacity    = avia_get_woocommerce_term_meta( $term->term_id, 'av-banner-overlay-opacity' );
  1088.                
  1089.                 if(!empty($style))
  1090.                 {
  1091.                     remove_action( 'woocommerce_before_shop_loop', 'woocommerce_taxonomy_archive_description', 11 );
  1092.                     echo avia_woocommerce_parallax_banner($attachment_id, $overlay, $opacity, $description, $font);
  1093.                     $avia_config['woo-banner'] = true;
  1094.                 }
  1095.             }
  1096.         }
  1097.     }
  1098. }
  1099.  
  1100.  
  1101.  
  1102. add_action( 'ava_after_main_container', 'avia_woocommerce_shop_banner', 11 );
  1103.  
  1104. function avia_woocommerce_shop_banner()
  1105. {
  1106.     global $avia_config;
  1107.    
  1108.     if(is_shop() || (is_product_category() && avia_get_option('shop_banner_global') == "shop_banner_global") && !isset($avia_config['woo-banner']))
  1109.     {
  1110.         $options = avia_get_option();
  1111.        
  1112.         if( isset( $options['shop_banner'] )  && ( $options['shop_banner'] == 'av-active-shop-banner' ) )
  1113.         {
  1114.             $bg         = $options['shop_banner_image'];
  1115.             $overlay    = $options['shop_banner_overlay_color'];
  1116.             $opacity    = $options['shop_banner_overlay_opacity'];
  1117.             $description= wpautop($options['shop_banner_message']);
  1118.             $font       = $options['shop_banner_message_color'];
  1119.            
  1120.             echo avia_woocommerce_parallax_banner($bg, $overlay, $opacity, $description, $font);
  1121.         }
  1122.     }
  1123. }
  1124.  
  1125.  
  1126. function avia_woocommerce_parallax_banner( $bg, $overlay, $opacity, $description, $font )
  1127. {
  1128.    
  1129.     if( is_numeric( $bg ) )
  1130.     {
  1131.         $bg = wp_get_attachment_image_src($bg, 'extra_large');
  1132.         $bg = ( is_array( $bg ) && $bg[0] != '' ) ? $bg[0] : '';
  1133.     }
  1134.    
  1135.     if( $font )
  1136.     {
  1137.         $font = "style='color:{$font};'";
  1138.     }
  1139.    
  1140.     if( $bg )
  1141.     {
  1142.         $bg = "background-image: url(".$bg.");";
  1143.     }
  1144.    
  1145.     $output = '';
  1146.                    
  1147.     $output .='<div id="av_product_description" class="avia-section main_color avia-section-large avia-no-border-styling avia-full-stretch av-parallax-section av-section-color-overlay-active avia-bg-style-parallax container_wrap fullsize" data-section-bg-repeat="stretch" '.$font.'>';
  1148.     $output .='<div class="av-parallax avia-full-stretch" data-avia-parallax-ratio="0.3">';
  1149.     $output .='<div class="av-parallax-inner av-parallax-woo" style="'.$bg.' main_color background-attachment: scroll; background-position: 50% 50%; background-repeat: no-repeat;">';
  1150.     $output .='</div>';
  1151.     $output .='</div>';
  1152.    
  1153.    
  1154.     $output .='<div class="av-section-color-overlay-wrap">';
  1155.     if(!empty($overlay))
  1156.     {
  1157.         $output .='<div class="av-section-color-overlay" style="opacity: '.$opacity.'; background-color: '.$overlay.'; "></div>';
  1158.     }
  1159.    
  1160.     $output .='<div class="container">';
  1161.     $output .='<main class="template-page content av-content-full alpha units">';
  1162.     if($description) $output .= "<h1>".$description."</h1>";
  1163.     $output .='</main></div></div></div>';
  1164.    
  1165.     return $output;
  1166. }
  1167.  
  1168.  
  1169. #
  1170. # creates the title + description for overview pages
  1171. #
  1172. function avia_woocommerce_advanced_title()
  1173. {
  1174.  
  1175.     global $wp_query;
  1176.     $titleClass     = "";
  1177.     $image          = "";
  1178.  
  1179.  
  1180.     if(!empty($attachment_id))
  1181.     {
  1182.         $titleClass .= "title_container_image ";
  1183.         $image      = wp_get_attachment_image( $attachment_id, 'thumbnail', false, array('class'=>'category_thumb'));
  1184.     }
  1185.  
  1186.     echo "<div class='extralight-border title_container shop_title_container $titleClass'>";
  1187.     //echo avia_breadcrumbs();
  1188.     woocommerce_catalog_ordering();
  1189.     echo $image;
  1190. }
  1191.  
  1192.  
  1193.  
  1194.  
  1195.  
  1196.  
  1197.  
  1198.  
  1199.  
  1200. #
  1201. # modify shop overview column count
  1202. #
  1203. function avia_woocommerce_loop_columns()
  1204. {
  1205.     global $avia_config;
  1206.     return $avia_config['shop_overview_column'];
  1207. }
  1208.  
  1209.  
  1210. #
  1211. # modify shop overview product count
  1212. #
  1213.  
  1214. function avia_woocommerce_product_count()
  1215. {
  1216.     global $avia_config;
  1217.     return $avia_config['shop_overview_products'];
  1218. }
  1219.  
  1220.  
  1221. #
  1222. # filter cross sells on the cart page. display 4 on fullwidth pages and 3 on carts with sidebar
  1223. #
  1224.  
  1225. add_filter('woocommerce_cross_sells_total', 'avia_woocommerce_cross_sale_count');
  1226. add_filter('woocommerce_cross_sells_columns', 'avia_woocommerce_cross_sale_count');
  1227.  
  1228. function avia_woocommerce_cross_sale_count($count)
  1229. {
  1230.     return 4;
  1231. }
  1232.  
  1233. #
  1234. # move cross sells below the shipping
  1235. #
  1236.  
  1237. remove_action( 'woocommerce_cart_collaterals', 'woocommerce_cross_sell_display' );
  1238. add_action( 'woocommerce_after_cart', 'woocommerce_cross_sell_display' , 10);
  1239.  
  1240.  
  1241.  
  1242.  
  1243. #
  1244. # display tabs and related items within the summary wrapper
  1245. #
  1246. remove_action( 'woocommerce_after_single_product_summary', 'woocommerce_output_product_data_tabs', 10 );
  1247. add_action(    'woocommerce_after_single_product_summary', 'woocommerce_output_product_data_tabs', 1 );
  1248.  
  1249.  
  1250.  
  1251. #
  1252. # display upsells and related products within dedicated div with different column and number of products
  1253. #
  1254. remove_action( 'woocommerce_after_single_product_summary', 'woocommerce_output_related_products',20);
  1255. remove_action( 'woocommerce_after_single_product', 'woocommerce_output_related_products',10);
  1256. add_action( 'woocommerce_after_single_product_summary', 'avia_woocommerce_output_related_products', 20);
  1257.  
  1258. function avia_woocommerce_output_related_products($items = false, $columns = false)
  1259. {
  1260.     global $avia_config;
  1261.     $output = "";
  1262.    
  1263.     if(!$items)     $items   = $avia_config['shop_single_column_items'];
  1264.     if(!$columns)   $columns = $avia_config['shop_single_column'];
  1265.  
  1266.     ob_start();
  1267.     woocommerce_related_products(array('posts_per_page'=>$items, 'columns'=>$columns)); // X products, X columns
  1268.     $content = ob_get_clean();
  1269.     if($content)
  1270.     {
  1271.         $output .= "<div class='product_column product_column_".$columns."'>";
  1272.         //$output .= "<h3>".(__('Related Products', 'avia_framework'))."</h3>";
  1273.         $output .= $content;
  1274.         $output .= "</div>";
  1275.     }
  1276.  
  1277.     $avia_config['woo_related'] = $output;
  1278.     return $output;
  1279.  
  1280. }
  1281.  
  1282. remove_action( 'woocommerce_after_single_product_summary', 'woocommerce_upsell_display', 15 );
  1283. remove_action( 'woocommerce_after_single_product', 'woocommerce_upsell_display',10);
  1284. add_action( 'woocommerce_after_single_product_summary', 'avia_woocommerce_output_upsells', 21); // needs to be called after the "related product" function to inherit columns and product count
  1285.  
  1286. function avia_woocommerce_output_upsells($items = false, $columns = false)
  1287. {
  1288.     global $avia_config;
  1289.  
  1290.     $output = "";
  1291.    
  1292.     if(!$items)     $items   = $avia_config['shop_single_column_items'];
  1293.     if(!$columns)   $columns = $avia_config['shop_single_column'];
  1294.    
  1295.     ob_start();
  1296.     woocommerce_upsell_display($items,$columns); // 4 products, 4 columns
  1297.     $content = ob_get_clean();
  1298.     if($content)
  1299.     {
  1300.         $output .= "<div class='product_column product_column_".$columns."'>";
  1301.         //$output .= "<h3>".(__('You may also like', 'avia_framework'))."</h3>";
  1302.         $output .= $content;
  1303.         $output .= "</div>";
  1304.     }
  1305.  
  1306.     $avia_config['woo_upsells'] = $output;
  1307.     return $output;
  1308.  
  1309. }
  1310.  
  1311. add_action( 'woocommerce_after_single_product_summary', 'avia_woocommerce_display_output_upsells', 30); //display the related products and upsells
  1312.  
  1313. function avia_woocommerce_display_output_upsells()
  1314. {
  1315.     global $avia_config;
  1316.  
  1317.     $sells = isset( $avia_config['woo_upsells'] ) ? $avia_config['woo_upsells'] : '';
  1318.     $related = isset( $avia_config['woo_related'] ) ? $avia_config['woo_related'] : '';
  1319.  
  1320.     $products = $sells . $related;
  1321.  
  1322.     if( ! empty( $products ) )
  1323.     {
  1324.  
  1325.         $output  = "</div></div></div>";
  1326.         $output .= '<div id="av_section_1" class="avia-section alternate_color avia-section-small  container_wrap fullsize"><div class="container"><div class="template-page content  twelve alpha units">';
  1327.         $output .= $products;
  1328.  
  1329.         echo $output;
  1330.     }
  1331. }
  1332.  
  1333.  
  1334. if( ! function_exists( 'avia_before_get_sidebar_template_builder' ) && avia_woocommerce_enabled() )
  1335. {
  1336.     /**
  1337.      * Single Product page on ALB: we need to change sidebar - otherwise we have blog or page resulting in a wrong output
  1338.      *
  1339.      * @since 4.5.5
  1340.      */
  1341.     function avia_before_get_sidebar_template_builder()
  1342.     {
  1343.         global $avia_config;
  1344.        
  1345.         if( is_product() )
  1346.         {
  1347.             $avia_config['currently_viewing'] = 'shop_single';
  1348.         }
  1349.         else if( is_page ( wc_get_page_id( 'shop' ) ) )
  1350.         {
  1351.             $avia_config['currently_viewing'] = 'shop';
  1352.         }
  1353.     }
  1354.    
  1355.     add_action( 'ava_before_get_sidebar_template_builder', 'avia_before_get_sidebar_template_builder', 10 );
  1356. }
  1357.        
  1358.  
  1359. #
  1360. # wrap single product image in an extra div
  1361. #
  1362. add_action( 'woocommerce_before_single_product_summary', 'avia_add_image_div', 2);
  1363. add_action( 'woocommerce_before_single_product_summary',  'avia_close_image_div', 20);
  1364.  
  1365. if(!function_exists('avia_add_image_div'))
  1366. {
  1367.     function avia_add_image_div()
  1368.     {
  1369.         $nolightbox = '';
  1370.         $icon = '';
  1371.        
  1372.         if( avia_woocommerce_version_check( '3.0.0' ) )
  1373.         {
  1374.             if( current_theme_supports( 'wc-product-gallery-lightbox' ) )
  1375.             {
  1376.                 $nolightbox = 'noLightbox';
  1377.             }
  1378.             else if( current_theme_supports( 'avia-wc-30-product-gallery-feature' ) )
  1379.             {
  1380.                 $nolightbox = 'noHover';
  1381.                 $icon = '<div class="avia-wc-30-product-gallery-lightbox" '.av_icon_string('search').' ></div>';
  1382.             }
  1383.         }
  1384.        
  1385.         echo '<div class="' . $nolightbox . ' single-product-main-image alpha">' . $icon;
  1386.     }
  1387. }
  1388.  
  1389.  
  1390.  
  1391. if(!function_exists('avia_close_image_div'))
  1392. {
  1393.     function avia_close_image_div()
  1394.     {
  1395.         global $avia_config;
  1396.         if(is_product()) {
  1397.         $avia_config['currently_viewing'] = "shop_single";
  1398.         get_sidebar();
  1399.         }
  1400.         echo "</div>";
  1401.     }
  1402. }
  1403.  
  1404.  
  1405.  
  1406. #
  1407. # wrap single product summary in an extra div
  1408. #
  1409. add_action( 'woocommerce_before_single_product_summary', 'avia_add_summary_div', 25);
  1410. add_action( 'woocommerce_after_single_product_summary',  'avia_close_div', 3);
  1411.  
  1412. if(!function_exists('avia_add_summary_div'))
  1413. {
  1414.     function avia_add_summary_div()
  1415.     {
  1416.         echo "<div class='single-product-summary'>";
  1417.     }
  1418. }
  1419.  
  1420. //remove_action( 'woocommerce_product_thumbnails', 'woocommerce_show_product_thumbnails', 20 );
  1421.  
  1422. if(avia_woocommerce_version_check('3.0.0')) // in woocommerce 3.0.0
  1423. {
  1424.     add_action('woocommerce_product_thumbnails', 'avia_product_gallery_thumbnail_opener', 19);
  1425.     add_action('woocommerce_product_thumbnails',  'avia_close_div', 21);
  1426. }
  1427.  
  1428. if(!function_exists('avia_product_gallery_thumbnail_opener'))
  1429. {
  1430.     function avia_product_gallery_thumbnail_opener()
  1431.     {
  1432.         echo "<div class='thumbnails'>";
  1433.     }
  1434. }
  1435.  
  1436.  
  1437. if( ! function_exists( 'avia_woocommerce_frontend_search_params' ) )
  1438. {
  1439.     add_action( 'woocommerce_before_shop_loop', 'avia_woocommerce_frontend_search_params', 20 );
  1440.  
  1441.     /**
  1442.      * Displays a front end interface for modifying the shoplist query parameters like sorting order, product count etc
  1443.      *
  1444.      * @since < 4.0
  1445.      */
  1446.     function avia_woocommerce_frontend_search_params()
  1447.     {
  1448.         global $avia_config;
  1449.  
  1450.         if( ! empty( $avia_config['woocommerce']['disable_sorting_options'] ) )
  1451.         {
  1452.             return;
  1453.         }
  1454.  
  1455.         $product_order['default'] = __( 'Default', 'avia_framework' );
  1456.         $product_order['menu_order'] = __( 'Custom', 'avia_framework' );
  1457.         $product_order['title'] = __( 'Name', 'avia_framework' );
  1458.         $product_order['price'] = __( 'Price', 'avia_framework' );
  1459.         $product_order['date'] = __( 'Date', 'avia_framework' );
  1460.         $product_order['popularity'] = __( 'Popularity (sales)', 'avia_framework' );
  1461.         $product_order['rating'] = __( 'Average rating', 'avia_framework' );
  1462.         $product_order['relevance'] = __( 'Relevance', 'avia_framework' );
  1463.         $product_order['rand'] = __( 'Random', 'avia_framework' );
  1464.         $product_order['id'] = __( 'Product ID', 'avia_framework' );
  1465.        
  1466.         /**
  1467.          *
  1468.          * @since 4.5.6.2
  1469.          * @return array
  1470.          */
  1471.         $product_order = apply_filters( 'avf_wc_product_order_dropdown_frontend', $product_order );
  1472.  
  1473.         $product_sort['asc'] = __( 'Click to order products ascending', 'avia_framework' );
  1474.         $product_sort['desc'] = __( 'Click to order products descending', 'avia_framework' );
  1475.  
  1476.         $per_page_string = __( 'Products per page', 'avia_framework' );
  1477.        
  1478.  
  1479.         $per_page = get_option( 'avia_woocommerce_product_count' );
  1480.         if( ! $per_page )
  1481.         {
  1482.             $per_page = get_option( 'posts_per_page' );
  1483.         }
  1484.        
  1485.         /**
  1486.          * ALB elements can return all elements = -1
  1487.          */
  1488.         if( ! empty( $avia_config['woocommerce']['default_posts_per_page'] ) && is_numeric( $avia_config['woocommerce']['default_posts_per_page'] ) )
  1489.         {
  1490.             if( $avia_config['woocommerce']['default_posts_per_page'] > 0 )
  1491.             {
  1492.                 $per_page = $avia_config['woocommerce']['default_posts_per_page'];
  1493.             }
  1494.         }
  1495.        
  1496.         parse_str( $_SERVER['QUERY_STRING'], $params );
  1497.        
  1498.        
  1499.         if( ! isset( $params['product_order'] ) )
  1500.         {
  1501.             $po_key = 'default';
  1502.         }
  1503.         else
  1504.         {
  1505.             $po_key = $params['product_order'];
  1506.         }
  1507.        
  1508.         if( ! isset( $params['product_sort'] ) )
  1509.         {
  1510.             $ps_key = ! empty( $avia_config['woocommerce']['product_sort'] ) ? $avia_config['woocommerce']['product_sort'] : 'asc';
  1511.         }
  1512.         else
  1513.         {
  1514.             $ps_key = $params['product_sort'];
  1515.         }
  1516.        
  1517.         if( 'default' == $po_key )
  1518.         {
  1519.             unset( $params['product_sort'] );
  1520.         }
  1521.        
  1522.         $params['avia_extended_shop_select'] = 'yes';
  1523.  
  1524. //      $po_key = ! empty( $avia_config['woocommerce']['product_order'] ) ? $avia_config['woocommerce']['product_order'] : $params['product_order'];
  1525. //      $ps_key = ! empty( $avia_config['woocommerce']['product_sort'] ) ? $avia_config['woocommerce']['product_sort'] : $params['product_sort'];
  1526.         $pc_key = ! empty( $avia_config['woocommerce']['product_count'] ) ? $avia_config['woocommerce']['product_count'] : $per_page;
  1527.  
  1528.         $ps_key = strtolower( $ps_key );
  1529.        
  1530.         $show_sort = ! in_array( $po_key, array( 'rand', 'popularity', 'rating', 'default' ) );
  1531.        
  1532.         $nofollow = 'rel="nofollow"';
  1533.  
  1534.         //generate markup
  1535.         $output  =  '';
  1536.         $output .=  "<div class='product-sorting'>";
  1537.         $output .=      "<ul class='sort-param sort-param-order'>";
  1538.         $output .=          "<li><span class='currently-selected'>" . __( 'Sort by', 'avia_framework' ) . " <strong>{$product_order[$po_key]}</strong></span>";
  1539.         $output .=              "<ul>";
  1540.        
  1541.         foreach ( $product_order as $order_key => $order_text )
  1542.         {
  1543.             $query_string = 'default' == $order_key ? avia_woo_build_query_string( $params, 'product_order', $order_key, 'product_sort' ) : avia_woo_build_query_string( $params, 'product_order', $order_key );
  1544.                    
  1545.             $output .=              '<li' . avia_woo_active_class( $po_key, $order_key ) . '>';
  1546.             $output .=                  "<a href='{$query_string}' {$nofollow}>";
  1547.             $output .=                      "<span class='avia-bullet'></span>{$order_text}";
  1548.             $output .=                  '</a>';
  1549.             $output .=              '</li>';
  1550.         }
  1551.        
  1552.         $output .=              '</ul>';
  1553.         $output .=          '</li>';
  1554.         $output .=      '</ul>';
  1555.  
  1556.         if( $show_sort )
  1557.         {
  1558.             $output .=  "<ul class='sort-param sort-param-sort'>";
  1559.             $output .=      '<li>';
  1560.  
  1561.             if( $ps_key == 'desc' )
  1562.             {
  1563.             $output .=          "<a title='{$product_sort['asc']}' class='sort-param-asc'  href='" . avia_woo_build_query_string($params, 'product_sort', 'asc' ) . "' {$nofollow}>{$product_sort['desc']}</a>";
  1564.             }
  1565.             if( $ps_key == 'asc' )
  1566.             {
  1567.             $output .=          "<a title='{$product_sort['desc']}' class='sort-param-desc' href='" . avia_woo_build_query_string($params, 'product_sort', 'desc' ) . "' {$nofollow}>{$product_sort['asc']}</a>";
  1568.             }
  1569.        
  1570.             $output .=      '</li>';
  1571.             $output .=  '</ul>';
  1572.         }
  1573.        
  1574.         if( ! isset( $avia_config['woocommerce']['default_posts_per_page'] ) || ( $avia_config['woocommerce']['default_posts_per_page'] > 0 ) )
  1575.         {
  1576.             $output .=  "<ul class='sort-param sort-param-count'>";
  1577.             $output .=      "<li><span class='currently-selected'>".__("Display",'avia_framework')." <strong>".$pc_key." ".$per_page_string."</strong></span>";
  1578.             $output .=          '<ul>';
  1579.             $output .=              "<li" . avia_woo_active_class( $pc_key, $per_page ) . "><a href='" . avia_woo_build_query_string( $params, 'product_count', $per_page ) . "' {$nofollow}>       <span class='avia-bullet'></span>{$per_page} {$per_page_string}</a></li>";
  1580.             $output .=              "<li" . avia_woo_active_class( $pc_key, $per_page*2 ) . "><a href='" . avia_woo_build_query_string( $params, 'product_count', $per_page * 2 ) . "' {$nofollow}> <span class='avia-bullet'></span>" . ( $per_page * 2 ) . " {$per_page_string}</a></li>";
  1581.             $output .=              "<li" . avia_woo_active_class( $pc_key, $per_page*3 ) . "><a href='" . avia_woo_build_query_string( $params, 'product_count', $per_page * 3 ) . "' {$nofollow}> <span class='avia-bullet'></span>" . ( $per_page * 3 ) . " {$per_page_string}</a></li>";
  1582.             $output .=          '</ul>';
  1583.             $output .=      '</li>';
  1584.             $output .=  '</ul>';
  1585.         }
  1586.  
  1587.  
  1588.         $output .= '</div>';
  1589.         echo $output;
  1590.     }
  1591. }
  1592.  
  1593.  
  1594. if( ! function_exists( 'avia_woocommerce_ajax_search_params' ) )
  1595. {
  1596.     /**
  1597.      * Add support for WC product display settings
  1598.      *
  1599.      * @since 4.7.3.1
  1600.      * @param array|string $params
  1601.      * @return array
  1602.      */
  1603.     function avia_woocommerce_ajax_search_params( $params = array() )
  1604.     {
  1605.         if( ! avia_woocommerce_enabled() )
  1606.         {
  1607.             return $params;
  1608.         }
  1609.        
  1610.         if( ! avia_woocommerce_version_check( '3.0.0') )
  1611.         {
  1612.             return $params;
  1613.         }
  1614.        
  1615.         /**
  1616.          *
  1617.          * @since 4.7.3.1
  1618.          * @param string $visibility            'show'|'hide'|'' for WC default
  1619.          * @param string $context
  1620.          * @return string                       'show'|'hide'|'' for WC default
  1621.          */
  1622.         $products_visibility = apply_filters( 'avf_ajax_search_woocommerce_params', '', 'out_of_stock' );
  1623.        
  1624.         /**
  1625.          *
  1626.          * @since 4.7.3.1
  1627.          * @param string $visibility            'show'|'hide'|'' for WC default
  1628.          * @param string $context
  1629.          * @return string                       'show'|'hide'|'' for all
  1630.          */
  1631.         $prod_hidden = apply_filters( 'avf_ajax_search_woocommerce_params', '', 'hidden_products' );
  1632.        
  1633.         /**
  1634.          *
  1635.          * @since 4.7.3.1
  1636.          * @param string $visibility            'show'|'hide'|'' for WC default
  1637.          * @param string $context
  1638.          * @return string                       'show'|'hide'|'' for all
  1639.          */
  1640.         $prod_featured = apply_filters( 'avf_ajax_search_woocommerce_params', '', 'featured_products' );
  1641.        
  1642.         // Meta query - replaced by Tax query in WC 3.0.0
  1643.         $meta_query = array();
  1644.         $tax_query = array();
  1645.  
  1646.         avia_wc_set_out_of_stock_query_params( $meta_query, $tax_query, $products_visibility );
  1647.         avia_wc_set_hidden_prod_query_params( $meta_query, $tax_query, $prod_hidden );
  1648.         avia_wc_set_featured_prod_query_params( $meta_query, $tax_query, $prod_featured );
  1649.        
  1650.         if( empty( $tax_query ) || ! is_array( $tax_query ) )
  1651.         {
  1652.             return $params;
  1653.         }
  1654.        
  1655.         //  Plugins might render a query string -> transform to array
  1656.         $params = wp_parse_args( $params );
  1657.        
  1658.         if( ! isset( $params['tax_query'] ) || ! is_array( $params['tax_query'] ) )
  1659.         {
  1660.             $params['tax_query'] = array();
  1661.         }
  1662.        
  1663.         foreach( $tax_query as $value )
  1664.         {
  1665.             $params['tax_query'][] = $value;
  1666.         }
  1667.        
  1668.         return $params;
  1669.     }
  1670.    
  1671.     add_filter( 'avf_ajax_search_query', 'avia_woocommerce_ajax_search_params', 20, 1 );
  1672. }
  1673.  
  1674.  
  1675. if( ! function_exists( 'avia_woo_active_class' ) )
  1676. {
  1677.     /**
  1678.      * Helper function to create the active list class
  1679.      *
  1680.      * @param string $key1
  1681.      * @param string $key2
  1682.      * @return string
  1683.      */
  1684.     function avia_woo_active_class( $key1, $key2 )
  1685.     {
  1686.         return ( $key1 == $key2 ) ? " class='current-param'" : '';
  1687.     }
  1688. }
  1689.  
  1690.  
  1691. if( ! function_exists( 'avia_woo_build_query_string' ) )
  1692. {
  1693.     /**
  1694.      * helper function to build the query strings for the catalog ordering menu
  1695.      *
  1696.      * @since < 4.0
  1697.      * @param array $params
  1698.      * @param string $overwrite_key
  1699.      * @param string $overwrite_value
  1700.      * @param string $remove_key
  1701.      * @return string
  1702.      */
  1703.     function avia_woo_build_query_string( $params = array(), $overwrite_key = '', $overwrite_value = '', $remove_key = '' )
  1704.     {
  1705.         if( ! empty( $overwrite_key ) )
  1706.         {
  1707.             $params[ $overwrite_key ] = $overwrite_value;
  1708.         }
  1709.        
  1710.         if( ! empty( $remove_key ) )
  1711.         {
  1712.             unset( $params[ $remove_key ] );
  1713.         }
  1714.        
  1715.         $paged = ( array_key_exists( 'product_count', $params ) ) ? 'paged=1&' : '';
  1716.        
  1717.         return "?" . $paged . http_build_query( $params );
  1718.     }
  1719. }
  1720.  
  1721.  
  1722. if( ! function_exists( 'avia_woocommerce_overwrite_catalog_ordering' ) )
  1723. {
  1724.     add_action( 'woocommerce_get_catalog_ordering_args', 'avia_woocommerce_overwrite_catalog_ordering', 20, 1 );
  1725.  
  1726.     /**
  1727.      * Overwrite the query parameters from WooCommerce
  1728.      *
  1729.      * @since < 4.0
  1730.      * @param array $args
  1731.      * @return string
  1732.      */
  1733.     function avia_woocommerce_overwrite_catalog_ordering( $args )
  1734.     {
  1735.         global $avia_config;
  1736.  
  1737.         if( empty( $avia_config['woocommerce'] ) )
  1738.         {
  1739.             $avia_config['woocommerce'] = array();
  1740.         }
  1741.        
  1742.         if( ! empty( $avia_config['woocommerce']['disable_sorting_options'] ) )
  1743.         {
  1744.             return $args;
  1745.         }
  1746.        
  1747.         /**
  1748.          * WC added shortcodes that use this filter (e.g. products).
  1749.          * We only need to alter the query when we have our select boxes.
  1750.          *
  1751.          * LINITATION: It is not possible to mix shop overview (= shop) and other shortcodes because we cannot distinguish when this filter is called !!!
  1752.          */
  1753.         if( ! isset( $_REQUEST['avia_extended_shop_select'] ) || ( 'yes' != $_REQUEST['avia_extended_shop_select'] ) )
  1754.         {
  1755.             $avia_config['woocommerce']['product_sort'] = strtolower( $args['order'] );
  1756.             $avia_config['woocommerce']['product_order'] = strtolower( $args['orderby'] );
  1757.        
  1758.             return $args;
  1759.         }
  1760.        
  1761.         //check the folllowing get parameters and session vars. if they are set overwrite the defaults
  1762.         $check = array( 'product_order', 'product_count', 'product_sort' );
  1763.        
  1764.         foreach( $check as $key )
  1765.         {
  1766.             if( isset( $_GET[ $key ] ) )
  1767.             {
  1768.                 $_SESSION['avia_woocommerce'][ $key ] = esc_attr( $_GET[ $key ] );
  1769.             }
  1770.             if( isset( $_SESSION['avia_woocommerce'][ $key ] ) )
  1771.             {
  1772.                 $avia_config['woocommerce'][ $key ] = $_SESSION['avia_woocommerce'][ $key ];
  1773.             }
  1774.         }
  1775.  
  1776.         // if user wants to use new product order remove the old sorting parameter
  1777.         if( isset( $_GET['product_order'] ) && ! isset( $_GET['product_sort'] ) && isset( $_SESSION['avia_woocommerce']['product_sort'] ) )
  1778.         {
  1779.             unset( $_SESSION['avia_woocommerce']['product_sort'], $avia_config['woocommerce']['product_sort'] );
  1780.         }
  1781.        
  1782.         $orderby = '';
  1783.         $order = '';
  1784.        
  1785.         /**
  1786.          * Set the product sorting
  1787.          */
  1788.         $product_sort = '';
  1789.         if( isset( $avia_config['woocommerce']['product_sort'] ) )
  1790.         {
  1791.             $product_sort = strtoupper( $avia_config['woocommerce']['product_sort'] );
  1792.             switch ( $product_sort )
  1793.             {
  1794.                 case 'DESC':
  1795.                 case 'ASC':
  1796.                     break;
  1797.                 default:
  1798.                     $product_sort = 'ASC';
  1799.                     break;
  1800.             }
  1801.         }
  1802.        
  1803.         /**
  1804.          * Set the product order with default sortings
  1805.          */
  1806.         $product_order = isset( $avia_config['woocommerce']['product_order'] ) ? $avia_config['woocommerce']['product_order'] :'';
  1807.         switch ( $product_order )
  1808.         {
  1809.             case 'id':
  1810.             case 'relevance':
  1811.             case 'date':
  1812.                 $orderby = $product_order;
  1813.                 $order = ! empty( $product_sort ) ? $product_sort : 'DESC';
  1814.                 break;
  1815.             case 'menu_order':
  1816.             case 'title' :
  1817.             case 'price' :
  1818.                 $orderby = $product_order;  
  1819.                 $order = ! empty( $product_sort ) ? $product_sort : 'ASC';
  1820.                 break;
  1821.             case 'rand':
  1822.             case 'popularity':
  1823.             case 'rating':
  1824.                 $orderby = $product_order;
  1825.                 break;
  1826.             case 'default':
  1827.             default:
  1828.                 $orderby = '';
  1829.                 break;
  1830.         }
  1831.        
  1832.         WC()->query->remove_ordering_args();
  1833.        
  1834.         $old_disable_sorting_options = isset( $avia_config['woocommerce']['disable_sorting_options'] ) ? $avia_config['woocommerce']['disable_sorting_options'] : null;
  1835.         $avia_config['woocommerce']['disable_sorting_options'] = true;
  1836.        
  1837.         $new_args = WC()->query->get_catalog_ordering_args( $orderby, $order );
  1838.  
  1839.         if( ! is_null( $old_disable_sorting_options) )
  1840.         {
  1841.             $avia_config['woocommerce']['disable_sorting_options'] = $old_disable_sorting_options;
  1842.         }
  1843.         else
  1844.         {
  1845.             unset( $avia_config['woocommerce']['disable_sorting_options'] );
  1846.         }
  1847.        
  1848.        
  1849.         /**
  1850.          * set the product count
  1851.          */
  1852.         if( isset( $avia_config['woocommerce']['product_count'] ) && is_numeric( $avia_config['woocommerce']['product_count'] ) )
  1853.         {
  1854.             $avia_config['shop_overview_products_overwritten'] = true;
  1855.             $avia_config['shop_overview_products'] = (int) $avia_config['woocommerce']['product_count'];
  1856.         }
  1857.        
  1858.         $avia_config['woocommerce']['product_order'] = strtolower( $new_args['orderby'] );
  1859.         $avia_config['woocommerce']['product_sort'] = strtolower( $new_args['order'] );
  1860.        
  1861.         return $new_args;
  1862.     }
  1863.  
  1864.  
  1865. }
  1866.  
  1867. //remove produt information on password protected products
  1868. if(!function_exists('avia_woocommerce_remove_hooks'))
  1869. {
  1870.     add_action('woocommerce_before_single_product', 'avia_woocommerce_remove_hooks');
  1871.  
  1872.     function avia_woocommerce_remove_hooks()
  1873.     {
  1874.         /*remove content from password protected products*/
  1875.         if(post_password_required())
  1876.         {
  1877.             add_action( 'woocommerce_after_single_product_summary', 'avia_woocommerce_echo_password', 1 );
  1878.             remove_action( 'woocommerce_after_single_product_summary', 'woocommerce_output_product_data_tabs', 1 );
  1879.             remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_price', 10 );
  1880.             remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_excerpt', 20 );
  1881.             remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_meta', 40 );
  1882.             remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_sharing', 50 );
  1883.             remove_action( 'woocommerce_before_single_product_summary', 'woocommerce_show_product_images', 20 );
  1884.             remove_action( 'woocommerce_product_thumbnails', 'woocommerce_show_product_thumbnails', 20 );
  1885.             remove_action( 'woocommerce_after_single_product_summary', 'woocommerce_output_product_data_tabs', 10 );
  1886.             remove_action( 'woocommerce_after_single_product_summary', 'woocommerce_upsell_display', 15 );
  1887.             remove_action( 'woocommerce_after_single_product_summary', 'woocommerce_output_related_products', 20 );
  1888.             remove_action( 'woocommerce_before_single_product_summary', 'woocommerce_show_product_sale_flash', 10 );
  1889.             remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_add_to_cart', 30 );
  1890.             remove_action( 'woocommerce_simple_add_to_cart', 'woocommerce_simple_add_to_cart', 30 );
  1891.             remove_action( 'woocommerce_grouped_add_to_cart', 'woocommerce_grouped_add_to_cart', 30 );
  1892.             remove_action( 'woocommerce_variable_add_to_cart', 'woocommerce_variable_add_to_cart', 30 );
  1893.             remove_action( 'woocommerce_external_add_to_cart', 'woocommerce_external_add_to_cart', 30 );
  1894.             remove_action( 'woocommerce_after_shop_loop_item', 'woocommerce_template_loop_add_to_cart', 10 );
  1895.             remove_action( 'woocommerce_before_shop_loop_item_title', 'woocommerce_template_loop_product_thumbnail', 10 );
  1896.             remove_action( 'woocommerce_after_shop_loop_item_title', 'woocommerce_template_loop_price', 10 );
  1897.             remove_action( 'woocommerce_after_shop_loop_item_title', 'woocommerce_template_loop_rating', 5 );
  1898.             remove_action( 'woocommerce_after_shop_loop_item_title', 'woocommerce_show_product_loop_sale_flash', 10 );
  1899.         }
  1900.     }
  1901. }
  1902.  
  1903. if(!function_exists('avia_woocommerce_echo_password'))
  1904. {
  1905.     add_action('ava_woocomemrce_password_protection_remove_hooks', 'avia_woocommerce_remove_hooks');
  1906.  
  1907.     function avia_woocommerce_echo_password()
  1908.     {
  1909.         /*remove content from password protected products*/
  1910.         if(post_password_required())
  1911.         {
  1912.             echo get_the_password_form();
  1913.         }
  1914.     }
  1915. }
  1916.  
  1917. if( ! function_exists( 'avia_woocommerce_product_gallery_support' ) )
  1918. {
  1919.     if ( did_action( 'woocommerce_init' ) )
  1920.     {
  1921.         avia_woocommerce_product_gallery_support();
  1922.     }
  1923.     else
  1924.     {
  1925.         add_action( 'woocommerce_init', 'avia_woocommerce_product_gallery_support', 50 );
  1926.     }
  1927.    
  1928.     function avia_woocommerce_product_gallery_support()
  1929.     {
  1930.         if( avia_woocommerce_version_check( '3.0.0' ) && current_theme_supports( 'avia-wc-30-product-gallery-feature' ) )
  1931.         {
  1932.             remove_action( 'woocommerce_product_thumbnails', 'avia_product_gallery_thumbnail_opener', 19 );
  1933.             remove_action( 'woocommerce_product_thumbnails', 'avia_close_div', 21 );
  1934.         }
  1935.         else
  1936.         {
  1937.             add_filter( 'woocommerce_single_product_image_thumbnail_html','avia_woocommerce_gallery_thumbnail_description', 10, 4 );
  1938.         }
  1939.     }
  1940. }
  1941.  
  1942. /*
  1943.     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
  1944.     woocommerce_product_thumbnails action has been called
  1945. */
  1946.  
  1947. add_action('woocommerce_product_thumbnails', 'avia_woocommerce_set_single_page_image_size');
  1948.  
  1949. if(!function_exists('avia_woocommerce_set_single_page_image_size'))
  1950. {
  1951.     function avia_woocommerce_set_single_page_image_size()
  1952.     {
  1953.         global $avia_config;
  1954.        
  1955.         if(!isset($avia_config['avwc-single-page-size']))
  1956.         {
  1957.             $avia_config['avwc-single-page-size'] = "shop_thumbnail";
  1958.         }
  1959.     }
  1960. }
  1961.  
  1962.  
  1963. if(!function_exists('avia_woocommerce_gallery_thumbnail_description'))
  1964. {
  1965.     function avia_woocommerce_gallery_thumbnail_description($img, $attachment_id, $post_id = "", $image_class = "" )
  1966.     {
  1967.             global $avia_config;
  1968.        
  1969.             $image_size = isset($avia_config['avwc-single-page-size']) ? $avia_config['avwc-single-page-size'] : 'shop_single';
  1970.        
  1971.             $image_link = wp_get_attachment_url( $attachment_id );
  1972.  
  1973.             if(!$image_link) return $img;
  1974.  
  1975.             $image = wp_get_attachment_image( $attachment_id, apply_filters( 'single_product_small_thumbnail_size', $image_size ) );
  1976.             $image_title = esc_attr(get_post_field('post_content', $attachment_id));
  1977.             $img = sprintf( '<a href="%s" class="%s" title="%s"  rel="prettyPhoto[product-gallery]">%s</a>', $image_link, $image_class, $image_title, $image );
  1978.  
  1979.         return $img;
  1980.     }
  1981. }
  1982.  
  1983.  
  1984. if( ! function_exists( 'avia_title_args_woopage' ) )
  1985. {
  1986.     add_filter( 'avf_title_args', 'avia_title_args_woopage', 10, 2 );
  1987.    
  1988.     /**
  1989.      *
  1990.      * @param array $args
  1991.      * @param int $id
  1992.      * @return array
  1993.      */
  1994.     function avia_title_args_woopage( $args, $id )
  1995.     {
  1996.         if( is_single() && is_product() )
  1997.         {
  1998.             $args['heading'] = "strong";
  1999.         }
  2000.  
  2001.         return $args;
  2002.     }
  2003. }
  2004.  
  2005.  
  2006. /*
  2007. Function that is able to overwrite the default "shop" page used by woocommerce so the template builder can be used
  2008. Will only be executed if the user has switched the "shop" page to advanced layout builder. Default products are no longer displayed
  2009. and the user needs to add a product grid element
  2010.  
  2011. Can be activated by adding
  2012.  
  2013. add_theme_support( 'avia_custom_shop_page' );
  2014.  
  2015. to your functions.php file
  2016. */
  2017. if(!function_exists('avia_woocommerce_default_page'))
  2018. {
  2019.     add_filter( 'pre_get_posts', 'avia_woocommerce_default_page' );
  2020.        
  2021.     function avia_woocommerce_default_page($query)
  2022.     {
  2023.         if(current_theme_supports('avia_custom_shop_page'))
  2024.         {
  2025.             if( isset( $_REQUEST['s'] ) )
  2026.             {
  2027.                 return $query;
  2028.             }
  2029.            
  2030.             if(!$query->is_admin && $query->is_main_query() && !$query->is_tax && $query->is_archive && $query->is_post_type_archive)
  2031.             {
  2032.                 $vars = $query->query_vars;
  2033.            
  2034.                 if(isset($vars['post_type']) && 'product' == $vars['post_type'] )
  2035.                 {
  2036.                     $shop_page_id   = wc_get_page_id( 'shop' );
  2037.                     $builder_active = Avia_Builder()->get_alb_builder_status($shop_page_id);
  2038.                    
  2039.                     if($builder_active == "active")
  2040.                     {
  2041.                         $query->set( 'post_type', 'page' );
  2042.                         $query->set( 'p', $shop_page_id  );
  2043.                         $query->set( 'meta_query', array() );
  2044.                        
  2045.                         $query->is_singular = true;
  2046.                         $query->is_page     = true;
  2047.                         $query->is_archive  = false;
  2048.                         $query->is_post_type_archive  = false;
  2049.                         $query->query = array('p'=>$shop_page_id, 'post_type' => 'page');
  2050.                     }
  2051.                 }
  2052.             }
  2053.         }  
  2054.        
  2055.     return $query;
  2056.        
  2057.     }
  2058. }
  2059.  
  2060.  
  2061.  
  2062. if(!function_exists('avia_woocommerce_disable_editor'))
  2063. {
  2064.     add_filter( 'avf_builder_button_params', 'avia_woocommerce_disable_editor' );
  2065.    
  2066.    
  2067.     function avia_woocommerce_disable_editor($params)
  2068.     {
  2069.         if(!current_theme_supports('avia_custom_shop_page'))
  2070.         {
  2071.             global $post_ID;
  2072.             $shop_page_id = wc_get_page_id( 'shop' );
  2073.            
  2074.             if($post_ID == $shop_page_id)
  2075.             {
  2076.                 $disabled = __('(disabled)', 'avia_framework');
  2077.            
  2078.                 $params['visual_label']     = $params['visual_label']  . " ".$disabled;
  2079.                 $params['default_label']    = $params['default_label'] . " ".$disabled;
  2080.                 $params['button_class']     = "av-builer-button-disabled";
  2081.                 $params['disabled']         = true;
  2082.                 $params['note']             = __('This page is set as the default WooCommerce Shop Overview and therefore does not support the Enfold advanced layout editor', 'avia_framework')." <br/><a href='https://kriesi.at/documentation/enfold/custom-woocommerce-shop-overview/' target='_blank' rel='noopener noreferrer'>(".__('Learn more').")</a>";
  2083.                
  2084.             }
  2085.         }
  2086.        
  2087.        
  2088.         if(avia_backend_get_post_type() == "product")
  2089.         {
  2090.             $params['noteclass'] = "av-notice av-only-active"; 
  2091.             $params['note'] = __('Please note that the Advanced Layout Builder for products will not work with all WooCommerce Extensions', 'avia_framework');
  2092.         }
  2093.        
  2094.        
  2095.        
  2096.        
  2097.         return $params;
  2098.     }
  2099.  
  2100. }
  2101.  
  2102. if(!function_exists('avia_woocommerce_disable_editor_option'))
  2103. {
  2104.     add_filter( 'avf_builder_active', 'avia_woocommerce_disable_editor_option' , 10 , 2);
  2105.    
  2106.     function avia_woocommerce_disable_editor_option($params, $post_id)
  2107.     {
  2108.         if(!current_theme_supports('avia_custom_shop_page'))
  2109.         {
  2110.             if($post_id == wc_get_page_id( 'shop' ))
  2111.             {
  2112.                 $params = false;
  2113.             }
  2114.         }
  2115.        
  2116.         return $params;
  2117.     }
  2118.  
  2119. }
  2120.  
  2121.  
  2122. if( ! function_exists( 'avia_woocommerce_cart_placement' ) )
  2123. {
  2124.     /**
  2125.      * Place the cart button according to the header layout (top/sidebar)
  2126.      */
  2127.     function avia_woocommerce_cart_placement()
  2128.     {
  2129.         $cart_pos = avia_get_option( 'cart_icon', '' );
  2130.        
  2131.         if( 'no_cart' == $cart_pos )
  2132.         {
  2133.             return;
  2134.         }
  2135.        
  2136.         $position = avia_get_option( 'header_position',  'header_top' ) == 'header_top' ? 'ava_main_header' : 'ava_inside_main_menu';
  2137.        
  2138.         if( $cart_pos == 'always_display_menu' )
  2139.         {
  2140.             $position = 'ava_inside_main_menu';
  2141.             if( strpos( avia_get_option( 'header_layout' ), 'bottom_nav_header' ) !== false && avia_get_option( 'header_position' ) == 'header_top' )
  2142.             {
  2143.                 $position = 'ava_before_bottom_main_menu';
  2144.             }
  2145.         }
  2146.        
  2147.         add_action( $position, 'avia_woocommerce_cart_dropdown', 10 );
  2148.     }
  2149.    
  2150.     add_action( 'init', 'avia_woocommerce_cart_placement', 10 );
  2151. }
  2152.  
  2153. if( ! function_exists( 'avia_woocommerce_cart_pos' ) )
  2154. {
  2155.     /**
  2156.      * Permanent display of cart button
  2157.      *
  2158.      * @param array $class
  2159.      * @param array $necessary
  2160.      * @param string $prefix
  2161.      * @return array
  2162.      */
  2163.     function avia_woocommerce_cart_pos( $class, $necessary, $prefix )
  2164.     {
  2165.         $cart_pos = avia_get_option( 'cart_icon', '' );
  2166.        
  2167.         if( 'no_cart' == $cart_pos )
  2168.         {
  2169.             return $class;
  2170.         }
  2171.        
  2172.         if( $prefix == 'html_' ) // only for the html tag
  2173.         {
  2174.             $cart = WC()->cart->get_cart();
  2175.            
  2176.             if( $cart_pos == 'always_display' || ( ! empty( $cart ) ) )
  2177.             {
  2178.                 $class[] = 'visible_cart';
  2179.             }
  2180.            
  2181.             if( $cart_pos == 'always_display_menu' )
  2182.             {
  2183.                 $class[] = 'cart_at_menu';
  2184.             }
  2185.         }
  2186.        
  2187.         return $class;
  2188.     }
  2189.    
  2190.     add_filter( 'avf_header_classes', 'avia_woocommerce_cart_pos', 10, 3 );
  2191. }
  2192.  
  2193. if( ! function_exists( 'avia_woocommerce_cart_dropdown' ) )
  2194. {
  2195.  
  2196.     function avia_woocommerce_cart_dropdown()
  2197.     {
  2198.         $cart_items = WC()->cart->get_cart_contents_count();
  2199.         $cart_subtotal =  WC()->cart->get_cart_subtotal();
  2200.         $link = function_exists( 'wc_get_cart_url' ) ? wc_get_cart_url() :  WC()->cart->get_cart_url();
  2201.        
  2202.         $id = '';
  2203.         $added = wc_get_notices( 'success' );
  2204.         $trigger = ! empty( $added ) ? 'av-display-cart-on-load' : '';
  2205.         $active = $cart_items > 0 ? 'av-active-counter' : '';
  2206.  
  2207.         if( avia_get_option( 'cart_icon' ) == 'always_display_menu' )
  2208.         {
  2209.             $id = 'id="menu-item-shop"';
  2210.         }
  2211.        
  2212.  
  2213.         $output  = '';
  2214.         $output .= "<ul {$id} class = 'menu-item cart_dropdown {$trigger}' data-success='" . __( 'was added to the cart', 'avia_framework' ). "'>";
  2215.         $output .=      "<li class='cart_dropdown_first'>";
  2216.         $output .=          "<a class='cart_dropdown_link' href='" . $link . "'>";
  2217.         $output .=              '<span ' . av_icon_string( 'cart' ) . '></span>';
  2218.         $output .=              "<span class='av-cart-counter {$active}'>{$cart_items}</span>";
  2219.         $output .=              "<span class='avia_hidden_link_text'>" . __( 'Shopping Cart', 'avia_framework' ) . '</span>';
  2220.         $output .=          '</a>';
  2221.         $output .=          "<!--<span class='cart_subtotal'>{$cart_subtotal}</span>-->";
  2222.         $output .=          "<div class='dropdown_widget dropdown_widget_cart'>";
  2223.         $output .=              "<div class='avia-arrow'></div>";
  2224.         $output .=              '<div class="widget_shopping_cart_content"></div>';
  2225.         $output .=          '</div>';
  2226.         $output .=      '</li>';
  2227.         $output .= '</ul>';
  2228.  
  2229.         echo $output;
  2230.     }
  2231.    
  2232. }
  2233.  
  2234. if( ! function_exists( 'avia_woocommerce_add_to_cart_fragments' ) )
  2235. {
  2236.     /**
  2237.      * @since 4.7.6.3
  2238.      * @param array $fragments
  2239.      * @return array
  2240.      */
  2241.     function avia_woocommerce_add_to_cart_fragments( $fragments )
  2242.     {
  2243.         $cart_items = WC()->cart->get_cart_contents_count();
  2244.         $active = $cart_items > 0 ? 'av-active-counter' : '';
  2245.        
  2246.         $fragments['span.av-cart-counter'] = "<span class='av-cart-counter {$active}'>{$cart_items}</span>";
  2247.        
  2248.         return $fragments;
  2249.     }
  2250.    
  2251.     add_filter( 'woocommerce_add_to_cart_fragments', 'avia_woocommerce_add_to_cart_fragments');
  2252. }
  2253.  
  2254.  
  2255. /*
  2256.     after importing demo pages make sure that if we got multiple shop/my account/etc pages (happens if the user used default woocommerce setup)
  2257.     to remove the duplicates and set the theme options properly
  2258. */
  2259.  
  2260. add_action('avia_after_import_hook', 'avia_woocommerce_set_pages');
  2261. // add_action('ava_after_main_container', 'avia_woocommerce_set_pages');
  2262.  
  2263. function avia_woocommerce_set_pages()
  2264. {
  2265.     global $wpdb;
  2266.    
  2267.     $pages = array(
  2268.             'shop' => array(
  2269.                 'title'   => 'Shop',
  2270.                 'slug'    => 'shop',
  2271.             ),
  2272.             'cart' => array(
  2273.                 'title'   => 'Cart',
  2274.                 'slug'    => 'cart',
  2275.             ),
  2276.             'checkout' => array(
  2277.                 'title'   => 'Checkout',
  2278.                 'slug'    => 'checkout',
  2279.             ),
  2280.             'myaccount' => array(
  2281.                 'title'   => 'My Account',
  2282.                 'slug'    => 'my-account',
  2283.             )
  2284.         );
  2285.    
  2286.     /*query string to get multiple posts with the same name*/  
  2287.     $pagequery = "SELECT ID FROM $wpdb->posts WHERE post_title = %s AND post_type='page'";
  2288.    
  2289.    
  2290.     foreach ($pages as $page)
  2291.     {
  2292.         $entries = $wpdb->get_results( $wpdb->prepare( $pagequery , $page['title'] ));
  2293.    
  2294.         if(!empty($entries))
  2295.         {
  2296.             $keep   = 0;
  2297.             $delete = array();
  2298.            
  2299.             //we got one post of that name. the user did not yet activate woocommerce setup or no page with that name was imported
  2300.             if(count($entries) === 1)
  2301.             {
  2302.                 $keep = $entries[0]->ID;
  2303.             }
  2304.             else //we got 2 or more entries. keep the one with the highest id as woocommerce setting and delete the other ones
  2305.             {  
  2306.                 foreach($entries as $entry)
  2307.                 {
  2308.                     if($entry->ID > $keep)
  2309.                     {
  2310.                         if($keep) $delete[] = $keep;
  2311.                         $keep = $entry->ID;
  2312.                     }
  2313.                     else
  2314.                     {
  2315.                         $delete[] = $entry->ID;
  2316.                     }
  2317.                 }
  2318.             }
  2319.            
  2320.             //delete the not required posts
  2321.             foreach($delete as $delete_id)
  2322.             {
  2323.                 wp_delete_post( $delete_id, true );
  2324.             }
  2325.            
  2326.             if($keep > 0)
  2327.             {
  2328.                 //store the value of the $keep as the default woo setting
  2329.                 $setting = str_replace("-", "", $page['slug']);
  2330.                 update_option('woocommerce_' . $setting . '_page_id' , $keep);
  2331.                
  2332.                 //modify the page slug and remove any numbers if necessary
  2333.                 $update_post = array(
  2334.                       'ID'          => $keep,
  2335.                       'post_name'   => $page['slug']
  2336.                   );
  2337.                
  2338.                 wp_update_post( $update_post );
  2339.             }
  2340.         }
  2341.     }      
  2342. }
  2343.  
  2344. /**
  2345.  * Helper functions for template builder elements - Product grids, slideshows, ......
  2346.  * ==================================================================================
  2347.  *
  2348.  */
  2349. if( ! function_exists( 'avia_wc_set_out_of_stock_query_params' ) )
  2350. {
  2351.    
  2352.     /**
  2353.      * Returns the query parameters for the "product out of stock" feature for selecting the products
  2354.      *
  2355.      * @param array $meta_query
  2356.      * @param array $tax_query
  2357.      * @param string $products_visibility                   'show'|'hide'|'' for WC default
  2358.      */
  2359.     function avia_wc_set_out_of_stock_query_params( array &$meta_query, array &$tax_query, $products_visibility = '' )
  2360.     {
  2361.         /**
  2362.          * Backwards compatibility WC < 3.0.0
  2363.          */
  2364.         if( ! avia_woocommerce_version_check( '3.0.0') )
  2365.         {
  2366.             $meta_query[] = WC()->query->visibility_meta_query();
  2367.             $meta_query[] = WC()->query->stock_status_meta_query();
  2368.             $meta_query   = array_filter( $meta_query );
  2369.         }
  2370.         else
  2371.         {
  2372.             switch( $products_visibility )
  2373.             {
  2374.                 case 'show':
  2375.                     $hide = 'no';
  2376.                     break;
  2377.                 case 'hide':
  2378.                     $hide = 'yes';
  2379.                     break;
  2380.                 default:
  2381.                     $hide = get_option( 'woocommerce_hide_out_of_stock_items', 'no' );
  2382.             }
  2383.  
  2384.             if( 'yes' == $hide )
  2385.             {
  2386.                 $outofstock_term = get_term_by( 'name', 'outofstock', 'product_visibility' );
  2387.                 if( $outofstock_term instanceof WP_Term )
  2388.                 {
  2389.                     $tax_query[] = array(
  2390.                                     'taxonomy'  =>  'product_visibility',
  2391.                                     'field'     =>  'term_taxonomy_id',
  2392.                                     'terms'     =>  array( $outofstock_term->term_taxonomy_id ),
  2393.                                     'operator'  =>  'NOT IN'
  2394.                                 );
  2395.                 }
  2396.             }
  2397.         }
  2398.     }
  2399. }
  2400.  
  2401. if( ! function_exists( 'avia_wc_set_hidden_prod_query_params' ) )
  2402. {
  2403.     /**
  2404.      * Returns the query parameters for the catalog visibility "hidden" feature for selecting the products.
  2405.      *
  2406.      * @since 4.1.3
  2407.      * @param array $meta_query
  2408.      * @param array $tax_query
  2409.      * @param string $catalog_visibility                    'show'|'hide'|'' for all
  2410.      */
  2411.     function avia_wc_set_hidden_prod_query_params( array &$meta_query, array &$tax_query, $catalog_visibility = '' )
  2412.     {
  2413.         if( avia_woocommerce_version_check( '3.0.0') )
  2414.         {
  2415.             switch( $catalog_visibility )
  2416.             {
  2417.                 case 'show':
  2418.                     $operator = 'IN';
  2419.                     break;
  2420.                 case 'hide':
  2421.                     $operator = 'NOT IN';
  2422.                     break;
  2423.                 default:
  2424.                     $operator = '';
  2425.             }
  2426.  
  2427.             if( in_array( $operator, array( 'IN', 'NOT IN' ) ) )
  2428.             {
  2429.                 $hidden_term = get_term_by( 'name', 'exclude-from-catalog', 'product_visibility' );
  2430.                 if( $hidden_term instanceof WP_Term )
  2431.                 {
  2432.                     $tax_query[] = array(
  2433.                                     'taxonomy'  =>  'product_visibility',
  2434.                                     'field'     =>  'term_taxonomy_id',
  2435.                                     'terms'     =>  array( $hidden_term->term_taxonomy_id ),
  2436.                                     'operator'  =>  $operator
  2437.                                 );
  2438.                 }
  2439.             }
  2440.         }
  2441.     }
  2442. }
  2443.  
  2444. if( ! function_exists( 'avia_wc_set_featured_prod_query_params' ) )
  2445. {
  2446.     /**
  2447.      * Returns the query parameters for the catalog visibility "hidden" feature for selecting the products.
  2448.      *
  2449.      * @since 4.1.3
  2450.      * @param array $meta_query
  2451.      * @param array $tax_query
  2452.      * @param string $catalog_visibility                    'show'|'hide'|'' for all
  2453.      */
  2454.     function avia_wc_set_featured_prod_query_params( array &$meta_query, array &$tax_query, $catalog_visibility = '' )
  2455.     {
  2456.         if( avia_woocommerce_version_check( '3.0.0') )
  2457.         {
  2458.             switch( $catalog_visibility )
  2459.             {
  2460.                 case 'show':
  2461.                     $operator = 'IN';
  2462.                     break;
  2463.                 case 'hide':
  2464.                     $operator = 'NOT IN';
  2465.                     break;
  2466.                 default:
  2467.                     $operator = '';
  2468.             }
  2469.  
  2470.             if( in_array( $operator, array( 'IN', 'NOT IN' ) ) )
  2471.             {
  2472.                 $featured_term = get_term_by( 'name', 'featured', 'product_visibility' );
  2473.                 if( $featured_term instanceof WP_Term )
  2474.                 {
  2475.                     $tax_query[] = array(
  2476.                                     'taxonomy'  =>  'product_visibility',
  2477.                                     'field'     =>  'term_taxonomy_id',
  2478.                                     'terms'     =>  array( $featured_term->term_taxonomy_id ),
  2479.                                     'operator'  =>  $operator
  2480.                                 );
  2481.                 }
  2482.             }
  2483.         }
  2484.     }
  2485. }
  2486.  
  2487. if( ! function_exists( 'avia_wc_set_additional_filter_args' ) )
  2488. {
  2489.     /**
  2490.      * Add additional filters from user selections in widget like
  2491.      *      - minimum / maximum price filter
  2492.      *
  2493.      * @since 4.5.5
  2494.      * @param array $meta_query
  2495.      * @param array $tax_query
  2496.      */
  2497.     function avia_wc_set_additional_filter_args( array &$meta_query, array &$tax_query )
  2498.     {
  2499.         /**
  2500.          * Filter for Minimum / Maximum Price
  2501.          */
  2502.         $args = array();
  2503.         if( isset( $_REQUEST['min_price'] ) && is_numeric( $_REQUEST['min_price'] ) )
  2504.         {
  2505.             $args['min_price'] = $_REQUEST['min_price'];
  2506.         }
  2507.         if( isset( $_REQUEST['max_price'] ) && is_numeric( $_REQUEST['max_price'] ) )
  2508.         {
  2509.             $args['max_price'] = $_REQUEST['max_price'];
  2510.         }
  2511.        
  2512.         if( ! empty( $args ) )
  2513.         {
  2514.             $meta_query[] = wc_get_min_max_price_meta_query( $args );
  2515.         }
  2516.        
  2517.         /**
  2518.          * Additional filters - see woocommerce\includes\class-wc-query.php::get_tax_query()
  2519.          * ==================
  2520.          */
  2521.         $product_visibility_terms  = wc_get_product_visibility_term_ids();
  2522.         $product_visibility_not_in = array( is_search() && $main_query ? $product_visibility_terms['exclude-from-search'] : $product_visibility_terms['exclude-from-catalog'] );
  2523.  
  2524.         /**
  2525.          * Filter for rating
  2526.          */
  2527.         if ( isset( $_REQUEST['rating_filter'] ) )
  2528.         {
  2529.             $rating_filter = array_filter( array_map( 'absint', explode( ',', $_REQUEST['rating_filter'] ) ) );
  2530.             $rating_terms  = array();
  2531.             for ( $i = 1; $i <= 5; $i ++ )
  2532.             {
  2533.                 if ( in_array( $i, $rating_filter, true ) && isset( $product_visibility_terms[ 'rated-' . $i ] ) )
  2534.                 {
  2535.                     $rating_terms[] = $product_visibility_terms[ 'rated-' . $i ];
  2536.                 }
  2537.             }
  2538.             if ( ! empty( $rating_terms ) )
  2539.             {
  2540.                 $tax_query[] = array(
  2541.                     'taxonomy'      => 'product_visibility',
  2542.                     'field'         => 'term_taxonomy_id',
  2543.                     'terms'         => $rating_terms,
  2544.                     'operator'      => 'IN',
  2545.                     'rating_filter' => true,
  2546.                 );
  2547.             }
  2548.         }
  2549.        
  2550.         /**
  2551.          * Filter for additional attribute filters
  2552.          */
  2553.         $layered_nav_chosen_attributes = WC_Query::get_layered_nav_chosen_attributes();
  2554.         foreach ( $layered_nav_chosen_attributes as $taxonomy => $data )
  2555.         {
  2556.                 $tax_query[] = array(
  2557.                     'taxonomy'         => $taxonomy,
  2558.                     'field'            => 'slug',
  2559.                     'terms'            => $data['terms'],
  2560.                     'operator'         => 'and' === $data['query_type'] ? 'AND' : 'IN',
  2561.                     'include_children' => false,
  2562.                 );
  2563.             }
  2564.     }
  2565. }
  2566.  
  2567.  
  2568. if( ! function_exists( 'avia_wc_get_product_query_order_args' ) )
  2569. {
  2570.     /**
  2571.      * Returns the ordering args, either the default catalog settings or the user selected.
  2572.      * Calls standard WC function to set filter hooks for order by
  2573.      * and removes previously set filter hooks
  2574.      *  
  2575.      * @since < 4.0
  2576.      * @modified 4.5.6
  2577.      * @param string $order_by
  2578.      * @param string $order
  2579.      * @return array
  2580.      */
  2581.     function avia_wc_get_product_query_order_args( $order_by = '', $order = '' )
  2582.     {
  2583.         $def_orderby = avia_wc_get_default_catalog_order_by();
  2584.                    
  2585.         $order_by = empty( $order_by ) ? $def_orderby['orderby'] : $order_by;
  2586.         $order = empty( $order ) ? $def_orderby['order'] : $order;
  2587.                    
  2588.                 //  remove and set filter hooks !!
  2589.         WC()->query->remove_ordering_args();
  2590.         $ordering_args = WC()->query->get_catalog_ordering_args( $order_by, $order );
  2591.        
  2592.         return $ordering_args;
  2593.     }
  2594. }
  2595.  
  2596.  
  2597. if( ! function_exists( 'avia_wc_get_default_catalog_order_by' ) )
  2598. {
  2599.     /**
  2600.      * Returns the default settings for catalog order by and clears any set filter hook by this function
  2601.      *
  2602.      * With Enfold 4.7.6.4 default $order extended to be equal to WC()->query->get_catalog_ordering_args()
  2603.      *
  2604.      * @return array
  2605.      */
  2606.     function avia_wc_get_default_catalog_order_by()
  2607.     {
  2608.         //  does not always return correct values !!!
  2609. //      $args = WC()->query->get_catalog_ordering_args();
  2610.        
  2611.         $orderby_value = apply_filters( 'woocommerce_default_catalog_orderby', get_option( 'woocommerce_default_catalog_orderby' ) );
  2612.  
  2613.             // Get order + orderby args from string
  2614.         $orderby_value = explode( '-', $orderby_value );
  2615.         $orderby = esc_attr( $orderby_value[0] );
  2616.        
  2617.         if( ! empty( $orderby_value[1] ) )
  2618.         {
  2619.             $order = $orderby_value[1];
  2620.         }
  2621.         else
  2622.         {
  2623.             switch( $orderby )
  2624.             {
  2625.                 case 'relevance':
  2626.                 case 'date':
  2627.                     $order = 'DESC';
  2628.                     break;
  2629.                 default:
  2630.                     $order = 'ASC';
  2631.                     break;
  2632.             }
  2633.         }
  2634.        
  2635.         $args = array();
  2636.  
  2637.         $args['orderby'] = strtolower( $orderby );
  2638.         $args['order'] = ( 'DESC' === strtoupper( $order ) ) ? 'DESC' : 'ASC';
  2639.         $args['meta_key'] = '';    
  2640.  
  2641.         return $args;
  2642.     }
  2643. }
  2644.  
  2645.  
  2646. if( ! function_exists( 'avia_wc_clear_catalog_ordering_args_filters' ) )
  2647. {
  2648.     /**
  2649.      * Remove all filters set by a call to WC()->query->get_catalog_ordering_args();
  2650.      */
  2651.     function avia_wc_clear_catalog_ordering_args_filters()
  2652.     {
  2653.         WC()->query->remove_ordering_args();
  2654.     }
  2655. }
  2656.  
  2657.  
  2658.  
  2659. add_filter( 'woocommerce_product_is_visible', 'avia_wc_product_is_visible', 10, 2 );
  2660. if( ! function_exists( 'avia_wc_product_is_visible' ) )
  2661. {
  2662.     /**
  2663.      * Allows to change the default visibility for products in catalog.
  2664.      *
  2665.      * 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
  2666.      * builder elements we have to force the display even if visibility is false
  2667.      *
  2668.      * @param boolean $visible
  2669.      * @param int $product_id
  2670.      * @return boolean
  2671.      */
  2672.     function avia_wc_product_is_visible( $visible, $product_id )
  2673.     {
  2674.         global $avia_config;
  2675.        
  2676.         if( ! isset( $avia_config['woocommerce']['catalog_product_visibility'] ) )
  2677.         {
  2678.             return $visible;
  2679.         }
  2680.        
  2681.         switch( $avia_config['woocommerce']['catalog_product_visibility'] )
  2682.         {
  2683.             case 'show_all':
  2684.                 return true;
  2685.             case 'hide_out_of_stock':
  2686.                 $product = wc_get_product( $product_id );
  2687.                 if( ! $product instanceof WC_Product )
  2688.                 {
  2689.                     return $visible;
  2690.                 }
  2691.                 return $product->is_in_stock();
  2692.             case 'use_default':
  2693.             default:
  2694.                 return $visible;
  2695.         }
  2696.     }
  2697. }
  2698.  
  2699.  
  2700. if( ! function_exists( 'avia_wc_remove_inline_terms' ) )
  2701. {
  2702.    
  2703.     /**
  2704.      * If a template builder page with a fullwidth el is used for terms and conditions the terms and conditions are not displayed properly. we need to filter that.
  2705.      * in case the user uses a template builder page do not display the inline terms. returning an empty string will just show the link to the TOS page
  2706.      */
  2707.     add_filter('woocommerce_format_content', 'avia_wc_remove_inline_terms', 10, 2);
  2708.    
  2709.     function avia_wc_remove_inline_terms( $apply_filters, $raw_string )
  2710.     {
  2711.         if( is_checkout() ) {
  2712.            
  2713.             $id = wc_get_page_id( 'terms' );
  2714.            
  2715.             if(get_post_meta($id, '_aviaLayoutBuilder_active', true) == "active")
  2716.             {
  2717.                 return '';
  2718.             }
  2719.         }
  2720.        
  2721.         return $apply_filters;
  2722.     }
  2723. }
  2724.  
  2725.  
  2726. add_filter( 'woocommerce_get_settings_checkout' , 'avia_wc_filter_terms_page_selection', 10 , 2);
  2727.  
  2728.  
  2729. if( ! function_exists( 'avia_wc_filter_terms_page_selection' ) )
  2730. {
  2731.     /**
  2732.      * Filter the content description for TOS page selection
  2733.      */
  2734.     function avia_wc_filter_terms_page_selection($settings)
  2735.     {
  2736.         foreach($settings as $key => $setting)
  2737.         {
  2738.             if( isset( $setting['id'] ) && ( $setting['id'] == "woocommerce_terms_page_id" ) )
  2739.             {
  2740.                 $settings[$key]['desc'] .= "<br><br>".__('Attention! Pages built with the Enfold Advanced Template Builder will not be displayed at the bottom of the checkout page but only with a link.', 'avia_framework');
  2741.                 break;
  2742.             }
  2743.         }
  2744.        
  2745.         return $settings;
  2746.     }
  2747. }
  2748.  
  2749. /**
  2750.  * Force WC images in widgets to have Enfold default image size
  2751.  *
  2752.  * @since 4.4.2
  2753.  * @added_by Günter
  2754.  */
  2755. add_action( 'woocommerce_widget_product_item_start', 'avia_wc_widget_product_item_start', 10, 1 );
  2756. add_filter( 'woocommerce_product_get_image', 'avia_wc_widget_product_image_size', 10, 6 );
  2757. add_action( 'woocommerce_widget_product_item_end', 'avia_wc_widget_product_item_end', 10, 1 );
  2758.  
  2759. global $avia_wc_product_widget_active;
  2760. $avia_wc_product_widget_active = false;
  2761.  
  2762. if( ! function_exists( 'avia_wc_widget_product_item_start' ) )
  2763. {
  2764.     /**
  2765.      * Set a global variable to limit changeing to widget areas only
  2766.      *
  2767.      * @since 4.4.2
  2768.      * @added_by Günter
  2769.      * @param array $args
  2770.      * @return array
  2771.      */
  2772.     function avia_wc_widget_product_item_start( $args )
  2773.     {
  2774.         global $avia_wc_product_widget_active;
  2775.  
  2776.         /**
  2777.          * @since 4.4.2
  2778.          * @return boolean
  2779.          */
  2780.         if( false !== apply_filters( 'avf_wc_widget_product_image_size_ignore', false, $args ) )
  2781.         {
  2782.             return;
  2783.         }
  2784.  
  2785.         $avia_wc_product_widget_active = true;
  2786.     }
  2787. }
  2788.  
  2789. if( ! function_exists( 'avia_wc_widget_product_image_size' ) )
  2790. {
  2791.     /**
  2792.      * Modify default WC behaviour.
  2793.      * Based on the function WC_Product::get_image
  2794.      *
  2795.      * @since 4.4.2
  2796.      * @param string $image
  2797.      * @param WC_Product $product
  2798.      * @param string $size
  2799.      * @param array $attr
  2800.      * @param boolean $placeholder
  2801.      * @param string $image1
  2802.      * @return string
  2803.      */
  2804.     function avia_wc_widget_product_image_size( $image, $product, $size, $attr, $placeholder, $image1 )
  2805.     {
  2806.         global $avia_wc_product_widget_active, $avia_config;
  2807.  
  2808.         if( ! $avia_wc_product_widget_active )
  2809.         {
  2810.             return $image;
  2811.         }
  2812.  
  2813.         /**
  2814.          * @since 4.4.2
  2815.          * @return string
  2816.          */
  2817.         $size = apply_filters( 'avf_wc_widget_product_image_size', 'widget', $product, $size, $attr, $placeholder );
  2818.  
  2819.         if ( has_post_thumbnail( $product->get_id() ) )
  2820.         {
  2821.             $image = get_the_post_thumbnail( $product->get_id(), $size, $attr );
  2822.         }
  2823.         elseif ( ( $parent_id = wp_get_post_parent_id( $product->get_id() ) ) && has_post_thumbnail( $parent_id ) )  // @phpcs:ignore Squiz.PHP.DisallowMultipleAssignments.Found
  2824.         {
  2825.             $image = get_the_post_thumbnail( $parent_id, $size, $attr );
  2826.         }
  2827.         elseif ( $placeholder )
  2828.         {
  2829.             $image = wc_placeholder_img( $size );
  2830.         }
  2831.         else
  2832.         {
  2833.             $image = '';
  2834.         }
  2835.        
  2836.         return $image;
  2837.     }
  2838. }
  2839.  
  2840. if( ! function_exists( 'avia_wc_widget_product_item_end' ) )
  2841. {
  2842.     /**
  2843.      * Reset a global variable to limit changeing to widget areas only
  2844.      *
  2845.      * @since 4.4.2
  2846.      * @param array $args
  2847.      */
  2848.     function avia_wc_widget_product_item_end( $args )
  2849.     {
  2850.         global $avia_wc_product_widget_active;
  2851.        
  2852.         $avia_wc_product_widget_active = false;
  2853.     }
  2854. }
  2855.  
  2856.  
  2857. /**
  2858.  * Fix problem with ALB pages used as "Terms and Conditions" page on checkout.
  2859.  * WC loads page content above the checkbox with js. With ALB this breaks and might also lead to styling problems.
  2860.  * Therefore we link to an external page.
  2861.  *
  2862.  * Up to WC 3.4.5 no hooks are provided to fix this in php. Therefore we have to add a js snippet.
  2863.  *
  2864.  * @since 4.4.2
  2865.  * @added_by Günter
  2866.  */
  2867. if( ! is_admin() && avia_woocommerce_version_check( '3.4.0' ) )
  2868. {
  2869.     add_action( 'woocommerce_checkout_terms_and_conditions', 'avia_wc_checkout_terms_and_conditions' );
  2870.    
  2871.     if( ! function_exists( 'avia_wc_checkout_terms_and_conditions' ) )
  2872.     {
  2873.         function avia_wc_checkout_terms_and_conditions()
  2874.         {
  2875.             $terms_id = wc_get_page_id('terms');
  2876.             if( 'active' == Avia_Builder()->get_alb_builder_status( $terms_id ) )
  2877.             {
  2878.                 add_action( 'wp_footer', 'avia_woocommerce_fix_checkout_term_link' );
  2879.             }
  2880.         }
  2881.     }
  2882.     if( ! function_exists( 'avia_woocommerce_fix_checkout_term_link' ) )
  2883.     {
  2884.         function avia_woocommerce_fix_checkout_term_link()
  2885.         {
  2886.             $i = 1;
  2887.             ?>
  2888.     <script>
  2889.     (function($) {
  2890.         // wait until everything completely loaded all assets
  2891.         $(window).on('load', function() {
  2892.             // remove the click event
  2893.             $( document.body ).off( 'click', 'a.woocommerce-terms-and-conditions-link' );
  2894.         });
  2895.     }(jQuery));
  2896.     </script>
  2897.     <?php
  2898.         }
  2899.     }
  2900.    
  2901. }
  2902.  
  2903. if( ! function_exists( 'avia_woocommerce_shortcode_current_post' ) )
  2904. {
  2905.    
  2906.     /**
  2907.      * Shop page might have another query for products and global $post might be a product
  2908.      *
  2909.      * @since 4.5.6
  2910.      * @param null|WP_Post $current_post
  2911.      * @return null|WP_Post
  2912.      */
  2913.     function avia_woocommerce_shortcode_current_post( $current_post )
  2914.     {
  2915.         if( ! avia_woocommerce_enabled() )
  2916.         {
  2917.             return $current_post;
  2918.         }
  2919.        
  2920.         if( ! is_shop() )
  2921.         {
  2922.             return $current_post;
  2923.         }
  2924.        
  2925.         $post = get_post( wc_get_page_id( 'shop' ) );
  2926.        
  2927.         return $post;
  2928.     }
  2929.    
  2930.     add_filter( 'avf_shortcode_handler_prepare_current_post', 'avia_woocommerce_shortcode_current_post', 10, 1 );
  2931. }
  2932.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement