Forum Replies Created

Viewing 4 replies - 16 through 19 (of 19 total)
  • Thread Starter Cisco75

    (@cisco75)

    @kellymetal Many thanks for your help! Everything works well now! You saved me a lot of time!

    Thread Starter Cisco75

    (@cisco75)

    Up please @kellymetal (if you can help)

    Thanks in advance

    Thread Starter Cisco75

    (@cisco75)

    Thanks my friend, it works, but now the second text is displayed 2 times on product category & product tag pages (and above the pagination block)! It seems to be because of some similarities with the product category code (when I delete the product tag code, the text field displays only one time).

    Do you know what I should do to make these text fields displaying one time on both templates, and below the pagination block?

    Sorry to bother you again and thanks in advance!

    • This reply was modified 4 years, 5 months ago by Cisco75.
    • This reply was modified 4 years, 5 months ago by Cisco75.
    Thread Starter Cisco75

    (@cisco75)

    Hello,

    Thanks a lot for your quick answer!
    I copied and pasted the code into my child theme’s functions.php, but it caused a fatal error that prevent the whole website to work. Maybe this issue is caused by a conflict with the similar code used for categories? Or another piece of code present in my functions.php?
    Please see below the whole code of my child theme’s functions.php, if you can help me to find what makes the code not working (and what to do to make it work…):

    <?php
    /* 	
     * Functions file for Kapee child
     */
    
    /*
     * Enqueue script and styles
     */
    function kapee_child_enqueue_styles() {
    	$parent_style = 'kapee-style';
    	wp_enqueue_style( $parent_style, get_template_directory_uri() . '/style.css' );
        wp_enqueue_style( 'kapee-child-style',
            get_stylesheet_directory_uri() . '/style.css',
            array( $parent_style ),
            wp_get_theme()->get('Version')
        );
    }
    add_action( 'wp_enqueue_scripts', 'kapee_child_enqueue_styles', 1001 );
    
    /********************************************************************* 
                            Cutom Code
    **********************************************************************/
    
    /**
     * Display page title on header.
     */
    if ( ! function_exists( 'kapee_get_page_title' ) ) :
    	function kapee_get_page_title() {		
    		global $wp_query;
    		$output = '';
    
    		if ( is_singular() ) {
    			
    			$post = isset( $GLOBALS['post'] ) ? $GLOBALS['post'] : null;
    			$page_title = '';
    			if ( is_page() && kapee_get_option( 'parent-page-title', 0 ) ) {
    				$page_title = empty($post->post_parent) ? '' : get_the_title($post->post_parent);
    			} else if (!is_page() && kapee_get_option( 'archives-page-title', 0 ) ) {
    				if ( isset( $post->post_type ) && $post->post_type == 'post' && kapee_get_option( 'archives-page-title', 0 ) ) {
    					if (get_option( 'show_on_front' ) == 'page') {
    						$page_title = get_the_title( get_option('page_for_posts', true) );
    					} else {
    						$page_title = kapee_page_title_archive($post->post_type);
    					}
    				} else if ( isset( $post->post_type ) && $post->post_type == 'product' && kapee_get_option( 'archives-page-title', 0 ) ) {
    					$post_type = 'product';
    					$post_type_object = get_post_type_object( $post_type );
    					if ( is_object( $post_type_object ) && function_exists( 'wc_get_page_id' ) ) {
    						$shop_page_id = wc_get_page_id( 'shop' );
    						$page_title  = $shop_page_id ? get_the_title( $shop_page_id ) : '';
    						if ( !$page_title  ) {
    							$page_title  = $post_type_object->labels->name;
    						}else{
    							$page_title .= ' - ' . get_the_title();
    						}
    					}
    				} else {
    					$page_title = kapee_page_title_archive($post->post_type);
    					$page_title .= ' - ' . get_the_title();
    				}
    			}
    
    			if ( $page_title ) {
    				$output.= $page_title;
    			} else {
    				$single_post_title = kapee_get_option( 'single-post-title-text', 'Our Blog' );
    				$custom_page_title 				= kapee_get_post_meta('custom_page_title');
    				if(!empty($custom_page_title )){
    					$output .= $custom_page_title ;
    				}elseif(!empty($single_post_title) && is_singular('post')){
    					$output .= kapee_get_option( 'single-post-title-text', 'Our Blog' );
    				}else{
    					$output .= get_the_title( $post->ID );
    				}
    				
    			}
    		} else {
    			
    			if ( is_post_type_archive() ) {
    				
    				if ( is_search() ) {
    					$output .= sprintf( esc_html__( 'Search Results: %s', 'kapee' ), esc_html( get_search_query() ) );
    				} else {
    					$output .= kapee_page_title_archive();
    				}
    			} elseif ( (is_tax() || is_tag() || is_category()) &&  kapee_get_option( 'blog-page-title', 1 ) ) { 
    				$term = $wp_query->get_queried_object();
    				$html = $title = $term->name;
    
    				if ( is_tag() ) {
    					$output .= sprintf( __( '%s', 'kapee' ), $html );
    				} elseif ( is_tax('product_tag') ) {
    					$output .= sprintf( __( '%s', 'kapee' ), $html );
    				} else {
    					$output .= $html;
    				}
    			} elseif ( is_date() &&  kapee_get_option( 'blog-page-title', 1 ) ) {
    				if ( is_year() ) {
    					$output .= sprintf( esc_html__( '%s', 'kapee' ), get_the_date( _x( 'Y', 'yearly archives date format', 'kapee' ) ) );
    				} elseif ( is_month() ) {
    					$output .= sprintf( esc_html__( '%s', 'kapee' ), get_the_date( _x( 'F Y', 'monthly archives date format', 'kapee' ) ) );
    				} elseif ( is_day() ) {
    					$output .= sprintf( esc_html__( '%s', 'kapee' ), get_the_date() );
    				}else{
    					$output .= esc_html__( 'Archives', 'kapee' );
    				}
    			} elseif ( is_author() &&  kapee_get_option( 'blog-page-title', 1 ) ) {
    				$user 	= $wp_query->get_queried_object();
    				$output .= sprintf( esc_html__( '%s', 'kapee' ), $user->display_name );
    			} elseif ( is_search() ) {
    				$output .= sprintf( esc_html__( 'Résultats pour : %s', 'kapee' ), esc_html( get_search_query() ) );
    			} elseif ( is_404() ) {
    				$output .= esc_html__( 'La page demandée est introuvable', 'kapee' );
    			}else {
    				
    				if ( is_home() && !is_front_page() ) {
    					if ( get_option( 'show_on_front' ) == 'page'  && kapee_get_option( 'blog-page-title', 1 )) {
    						$output .= get_the_title( get_option('page_for_posts', true) );
    					} else {
    						if(kapee_get_option( 'blog-page-title', 1 )){
    						$output .= kapee_get_option( 'blog-page-title-text', 'Blog' );
    						}
    					}
    				}else{
    					if(kapee_get_option( 'blog-page-title', 1 )){
    						$output .= kapee_get_option( 'blog-page-title-text', 'Blog' );
    					}
    				}
    			}
    		}
    
    		return apply_filters( 'kapee_get_page_title', $output );
    	}
    endif;
    
    /**
     * Breadcrumb / fil d'Ariane
     */
    
    if( ! class_exists( 'Kapee_Breadcrumb' )) {
    	class Kapee_Breadcrumb{
    		/**
    		 * Breadcrumb trail.
    		 * 
    		 * @var array
    		 */
    		private $crumbs = array();
    
    		/**
    		 * Add a crumb so we don't get lost.
    		 *
    		 * @param string $name Name.
    		 * @param string $link Link.
    		 */
    		public function add_crumb( $name, $link = '' ) {
    			
    			$name = !is_array($name) ? strip_tags( $name ) : $name;
    			$this->crumbs[] = array(
    				$name,
    				$link,
    			);
    		}
    
    		/**
    		 * Reset crumbs.
    		 */
    		public function reset() {
    			$this->crumbs = array();
    		}
    		
    		/**
    		 * Get the breadcrumb.
    		 *
    		 * @return array
    		 */
    		public function get_breadcrumb() {
    			return apply_filters( 'kapee_get_breadcrumb', $this->crumbs, $this );
    		}
    		
    		/**
    		 * Generate breadcrumb trail.
    		 *
    		 * @return array of breadcrumbs
    		 */
    		public function generate() {
    			global $post;
    			$breadcrumbs_archives_link = true;
    			$blog_link = true;
    			
    			if ( ! is_front_page() ) {
    				$this->add_crumb(esc_html__('Home', 'kapee'),home_url( '/' ));
    			} elseif ( is_home() ) {
    				$this->add_crumbs_home();
    			}
    			
    			// add woocommerce shop page link
    			if ( class_exists( 'WooCommerce' ) && ( ( is_woocommerce() && is_archive() && ! is_shop() ) || is_product() || is_cart() || is_checkout() || is_account_page() ) ) {
    				
    				$this->add_crumbs_shop_link();
    			}
    			
    			// add bbpress forums link
    			if ( class_exists( 'bbPress' ) && is_bbpress() && ( bbp_is_topic_archive() || bbp_is_single_user() || bbp_is_search() || bbp_is_topic_tag()  || bbp_is_edit() ) ) {
    				$this->add_crumb(bbp_get_forum_archive_title(),get_post_type_archive_link( 'forum' ));				
    			}
    			
    			if ( is_singular() ) {
    				if ( isset( $post->post_type ) && $post->post_type !== 'product' && get_post_type_archive_link( $post->post_type ) && $breadcrumbs_archives_link) {
    					$this->add_crumbs_archive_link();
    				} elseif ( isset( $post->post_type ) && $post->post_type == 'post' && get_option( 'show_on_front' ) == 'page' && $blog_link) {
    					$this->add_crumb(get_the_title( get_option('page_for_posts', true) ),get_permalink( get_option('page_for_posts' ) ));
    				}
    			
    				if ( isset( $post->post_parent ) && $post->post_parent == 0 ) {
    					
    					$this->add_crumbs_terms_link();
    				} else {
    					
    					$this->add_crumbs_ancestors_link();
    				}				
    				$this->add_crumbs_leaf();				
    			} else {
    				if ( is_post_type_archive() ) {
    					if ( is_search() ) {
    						$this->add_crumbs_archive_link();
    						$this->add_crumbs_leaf('search');					
    					} else {
    						$this->add_crumbs_archive_link(false);
    					}
    				} elseif ( is_tax() || is_tag() || is_category() ) {
    					if ( is_tag() ) {
    						if ( get_option( 'show_on_front' ) == 'page' && $blog_link ) {
    							$this->add_crumb(get_the_title( get_option('page_for_posts', true) ),get_permalink( get_option('page_for_posts' ) ));
    						}
    						$this->add_crumbs_tag();
    					} elseif ( is_tax('product_tag') ) {
    						$this->add_crumbs_product_tag();
    					} else {
    						if ( is_category() && get_option( 'show_on_front' ) == 'page' && $blog_link ) {
    							$this->add_crumb(get_the_title( get_option('page_for_posts', true) ),get_permalink( get_option('page_for_posts' ) ));
    						}
    						if ( is_tax('portfolio_cat') || is_tax('portfolio_skills') ) {
    							$this->add_crumb($this->get_archive_name('portfolio'),get_post_type_archive_link( 'portfolio' ));
    						}
    						$this->add_crumbs_taxonomies_link();
    						$this->add_crumbs_leaf('term');
    					}
    				} elseif ( is_date() ) {
    						global $wp_locale;
    
    						if ( get_option( 'show_on_front' ) == 'page' && $blog_link ) {
    							$this->add_crumb(get_the_title( get_option('page_for_posts', true) ), get_permalink( get_option('page_for_posts' ) ) );
    						}
    
    						$year = get_the_time('Y');
    						if ( is_month() || is_day() ) {
    							$month = get_the_time('m');	
    							$month_name = $wp_locale->get_month( $month );
    						}
    
    						if ( is_year() ) {
    							$this->add_crumbs_leaf('year');
    						} elseif ( is_month() ) {
    							$this->add_crumb($year, get_year_link( $year ));
    							$this->add_crumbs_leaf('month');
    						} elseif ( is_day() ) {						
    							$this->add_crumb($year, get_year_link( $year ));
    							$this->add_crumb($month_name, get_month_link( $month ));
    							$this->add_crumbs_leaf('day');
    						}
    				} elseif ( is_author() ) {
    					$this->add_crumbs_leaf('author');
    				} elseif ( is_search() ) {
    					$this->add_crumbs_leaf('search');
    				} elseif ( is_404() ) {
    					$this->add_crumbs_leaf('404');
    				} elseif ( class_exists( 'bbPress' ) && is_bbpress() ) {
    					if ( bbp_is_search() ) {
    						$this->add_crumbs_leaf('bbpress_search');
    					} elseif ( bbp_is_single_user() ) {
    						$this->add_crumbs_leaf('bbpress_user');
    					} else {
    						$this->add_crumbs_leaf();
    					}
    				} else {
    					if ( is_home() && !is_front_page() ) {
    						if ( get_option( 'show_on_front' ) == 'page' ) {
    							$this->add_crumb(get_the_title( get_option('page_for_posts', true) ));
    						} else {
    						
    						$this->add_crumb('Default title');
    						}
    					}
    				}
    			}			
    			return $this->get_breadcrumb();			
    		}
    		
    		/**
    		 * Is home trail..
    		 */
    		private function add_crumbs_home() {
    			$this->add_crumb(esc_html__('Home', 'kapee'));
    		}
    		
    		/**
    		 * Tag trail.
    		 */
    		private function add_crumbs_tag() {
    			$queried_object = $GLOBALS['wp_query']->get_queried_object();
    
    			/* translators: %s: tag name */
    			$this->add_crumb( sprintf( __( 'Article tagged &ldquo;%s&rdquo;', 'kapee' ), single_tag_title( '', false ) ), get_tag_link( $queried_object->term_id ) );
    		}
    		
    		/**
    		 * Product Tag trail.
    		 */
    		private function add_crumbs_product_tag() {
    			$queried_object = $GLOBALS['wp_query']->get_queried_object();
    
    			/* translators: %s: tag name */
    			$this->add_crumb( sprintf( __( '%s', 'kapee' ), single_tag_title( '', false ) ), get_tag_link( $queried_object->term_id ) );
    		}
    	
    		private function add_crumbs_shop_link($linked = true) {
    			$post_type = 'product';
    			$post_type_object = get_post_type_object( $post_type );
    			$link = '';
    			if ( is_object( $post_type_object ) && class_exists( 'WooCommerce' ) && ( is_woocommerce() || is_cart() || is_checkout() || is_account_page() ) ) {
    				$shop_page_id = wc_get_page_id( 'shop' );
    				$shop_page_name = $shop_page_id ? get_the_title( $shop_page_id ) : '';
    
    				if ( ! $shop_page_name ) {
    					$shop_page_name = $post_type_object->labels->name;
    				}
    				if ($linked ) {
    					$link = $shop_page_id !== -1 ? get_permalink($shop_page_id) : get_post_type_archive_link( $post_type );
    				}
    				
    				$this->add_crumb($shop_page_name,$link);
    			}
    			
    		}
    		
    		private function add_crumbs_archive_link($linked = true) {
    			global $wp_query;
    
    			$post_type = $wp_query->query_vars['post_type'];
    			$post_type_object = get_post_type_object( $post_type );
    			$link = '';
    			$archive_title = '';
    
    			if ( is_object( $post_type_object ) ) {
    
    				// woocommerce
    				if ( $post_type == 'product') {
    					$this->add_crumbs_shop_link();
    					return;
    				}
    
    				// bbpress
    				if ( class_exists( 'bbPress' ) && $post_type == 'topic' ) {
    					if ( $linked ) {
    						$archive_title = bbp_get_forum_archive_title();
    						$link = get_post_type_archive_link( bbp_get_forum_post_type() );
    					} else {
    						$archive_title = bbp_get_topic_archive_title();
    					}
    					$this->add_crumb($archive_title,$link);
    					return;
    				}
    
    				// default
    				$archive_title = $this->get_archive_name( $post_type );
    			}
    
    			if ( $linked ) {
    				$link = get_post_type_archive_link( $post_type );
    			}
    
    			if ( $archive_title ) {				
    				$this->add_crumb($archive_title,$link);
    				return;
    			}
    
    		}
    		
    		private function add_crumbs_terms_link() {
    
    			global $kapee_settings;
    
    			$output = array();
    			$post = isset( $GLOBALS['post'] ) ? $GLOBALS['post'] : null;
    			
    			$breadcrumbs_categories = true;
    			
    			if ( !$breadcrumbs_categories) {
    				return $output;
    			}
    			$taxonomy = '';
    			if ( $post->post_type == 'post' ) {
    				$taxonomy = 'category';
    			} elseif ( $post->post_type == 'portfolio' ) {
    				$taxonomy = 'portfolio_cat';
    			}elseif ( $post->post_type == 'product' ) {				
    				$taxonomy = 'product_cat';					
    			}
    			if(!empty($taxonomy )){
    				$terms = wp_get_object_terms(
    					$post->ID, $taxonomy, apply_filters(
    						'kapee_breadcrumb_product_terms_args', array(
    							'orderby' => 'parent',
    							'order'   => 'DESC',
    						)
    					)
    				);
    				if ( $terms ) {
    					$main_term = apply_filters( 'kapee_breadcrumb_main_term', $terms[0], $terms );
    					$this->term_ancestors( $main_term->term_id, $taxonomy );
    					$this->add_crumb( $main_term->name, get_term_link( $main_term ) );
    				}
    			}
    		}
    		
    		/**
    		 * Add crumbs for a term.
    		 *
    		 * @param int    $term_id  Term ID.
    		 * @param string $taxonomy Taxonomy.
    		 */
    		private function term_ancestors( $term_id, $taxonomy ) {
    			$ancestors = get_ancestors( $term_id, $taxonomy );
    			$ancestors = array_reverse( $ancestors );
    
    			foreach ( $ancestors as $ancestor ) {
    				$ancestor = get_term( $ancestor, $taxonomy );
    
    				if ( ! is_wp_error( $ancestor ) && $ancestor ) {
    					$this->add_crumb( $ancestor->name, get_term_link( $ancestor ) );
    				}
    			}
    		}
    	
    		private function add_crumbs_ancestors_link() {
    			$output = '';
    
    			$post = isset( $GLOBALS['post'] ) ? $GLOBALS['post'] : null;
    			$post_ancestor_ids = array_reverse( get_post_ancestors( $post ) );
    
    			foreach ( $post_ancestor_ids as $post_ancestor_id ) {
    				$post_ancestor = get_post( $post_ancestor_id );
    				
    				$this->add_crumb($post_ancestor->post_title,get_permalink( $post_ancestor->ID ));
    			}
    		}
    
    		private function add_crumbs_taxonomies_link() {
    			global $wp_query;
    			$term = $wp_query->get_queried_object();
    			$output = '';
    
    			if ( $term && $term->parent != 0 && isset($term->taxonomy) && isset($term->term_id) && is_taxonomy_hierarchical( $term->taxonomy ) ) {
    				$term_ancestors = get_ancestors( $term->term_id, $term->taxonomy );
    				$term_ancestors = array_reverse( $term_ancestors );
    
    				foreach ( $term_ancestors as $term_ancestor ) {
    					$term_object = get_term( $term_ancestor, $term->taxonomy );
    					$this->add_crumb($term_object->name,get_term_link( $term_object->term_id, $term->taxonomy ));
    				}
    			}
    
    			return $output;
    		}
    
    		public function get_archive_name($post_type){
    			$archive_title = '';
    			if ($post_type == 'portfolio') {
    				$archive_title = esc_html__('Portfolio','kapee');
    			} else {
    				$post_type_object = get_post_type_object( $post_type );
    				if ( is_object( $post_type_object ) ) {
    					$archive_title = $post_type_object->labels->singular_name;
    				}
    			}
    			return $archive_title;
    		}
    		
    		function add_crumbs_leaf( $object_type = '' ) {
    			global $wp_query, $wp_locale;
    
    			$post = isset( $GLOBALS['post'] ) ? $GLOBALS['post'] : null;
    
    			switch( $object_type ) {
    				case 'term':
    					$term = $wp_query->get_queried_object();
    					$title = $term->name;
    					break;
    				case 'year':
    					$title = get_the_time('Y');
    					break;
    				case 'month':
    					$month = get_the_time('m');
    					$title = $wp_locale->get_month( $month  );
    					break;
    				case 'day':
    					$title = get_the_time('d');
    					break;
    				case 'author':
    					$user = $wp_query->get_queried_object();
    					$title = $user->display_name;
    					break;
    				case 'search':
    					$search = esc_html( get_search_query() );
    					if ( $product_cat = get_query_var('product_cat') ) {
    						$product_cat = get_term_by('slug', $product_cat, 'product_cat');
    						$search = '<a href="' . esc_url( get_term_link($product_cat, 'product_cat') ) . '">' . esc_html( $product_cat->name ) . '</a>' . ( $search ? ' / ' : '' ) . $search;
    					}
    					$title = sprintf( __( 'Search - %s', 'kapee' ), $search );
    					break;
    				case '404':
    					$title = esc_html__( '404', 'kapee' );
    					break;
    				case 'bbpress_search':
    					$title = sprintf( __( 'Search - %s', 'kapee' ), esc_html( get_query_var( 'bbp_search' ) ) );
    					break;
    				case 'bbpress_user':
    					$current_user = wp_get_current_user();
    					$title = $current_user->user_nicename;
    					break;
    				default:
    					$title = get_the_title( $post->ID );
    					break;
    			}
    
    			$this->add_crumb($title,'');
    		}	
    		
    	}
    }
    
    function kapee_child_enqueue_admin_js() {	
    	wp_enqueue_script(
            'child-admin-script',
            get_stylesheet_directory_uri() . '/js/admin.js',
            array( 'jquery' )
        );
    
    }
    add_action( 'admin_enqueue_scripts', 'kapee_child_enqueue_admin_js');
    
    /**
     * Load Child VC Elements
     */
    if( defined( 'WPB_VC_VERSION' ) ) :
        add_action( 'vc_before_init', 'kapee_child_load_vc_element' ); 
        function kapee_child_load_vc_element() {
    		
    		require ( get_stylesheet_directory() . '/product-tag-box/woo-products-tag-box.php'); 
    	}
    	add_filter( 'vc_autocomplete_kapee_products_tag_box_tags_callback',	'kapee_product_tag_search', 10, 1 );
    	add_filter( 'vc_autocomplete_kapee_products_tag_box_tags_render', 'kapee_product_tag_render', 10, 1 );
    	
    endif;
    
    /**
     * Product tag search
     * @param $search_string
     *
     * @return array
     */
    function kapee_product_tag_search( $search_string ) {
    	$query = $search_string;
    	$data = array();
    	$args = array(
    		'name__like' => $query,
    		'taxonomy' => 'product_tag',
    	);
    	$result = get_terms( $args );
    	if ( is_wp_error( $result ) ) {
    		return $data;
    	}
    	if ( !is_array( $result ) || empty( $result ) ) {
    		return $data;
    	}
    	foreach ( $result as $term_data ) {
    		if ( is_object( $term_data ) && isset( $term_data->name, $term_data->term_id ) ) {
    			$data[] = array(
    				'value' => $term_data->term_id,
    				'label' => $term_data->name,
    				'group' => 'product_tag',
    			);
    		}
    	}
    
    	return $data;
    }
    
    /**
     * Product tag render
     * @param $value
     *
     * @return array|bool
     */
    function kapee_product_tag_render( $value ) {
    	$post = get_post( $value['value'] );
    	$term_data = get_term_by( 'id',  $value['value'],'product_tag' );
    
    	return is_null( $term_data ) ? false : array(
    		'label' => $term_data->name,
    		'value' => $term_data->term_id,
    		'group' => 'product_tag',
    	);
    }
    
    function kapee_child_get_products( $data_source, $atts, $args = array() ) {
    	$defaults = array(
    		'post_type'           	=> 'product',
    		'status'              	=> 'published',
    		'ignore_sticky_posts' 	=> 1,
    		'orderby'             	=> isset($atts['orderby']) ? $atts['orderby'] : 'date',
    		'order'               	=> isset($atts['sortby']) ? $atts['sortby'] : 'desc',
    		'posts_per_page'      	=> isset( $atts['limit'] ) > 0 ? intval( $atts['limit'] ) : 10,
    		'paged'      			=> isset($atts['paged']) > 0 ? intval( $atts['paged'] ) : 1,
    	);
    	$args['meta_query'] 	= WC()->query->get_meta_query();
    	$args['tax_query']   	= WC()->query->get_tax_query();
    	$args = wp_parse_args( $args, $defaults );
    	
    	switch ( $data_source ) {
    		case 'featured_products';
    			$args['tax_query'][] = array(
    				array(
    					'taxonomy' => 'product_visibility',
    					'field'    => 'name',
    					'terms'    => array( 'featured' ),
    					'operator' => 'IN',
    				),
    			);			
    			break;
    		case 'sale_products';
    			$product_ids_on_sale   = wc_get_product_ids_on_sale();
    			$product_ids_on_sale[] = 0;
    			$args['post__in']      = $product_ids_on_sale;
    			break;
    		case 'best_selling_products';
    			$args['meta_key'] = 'total_sales';
    			$args['orderby']  = 'meta_value_num';
    			$args['order']    = 'DESC';
    			break;
    		case 'top_rated_products';
    			$args['meta_key'] = '_wc_average_rating';
    			$args['orderby']  = 'meta_value_num';
    			$args['order']    = 'DESC';
    			break;
    		case 'products';
    			if ( $atts['product_ids'] != '' ) {
    				$args['post__in'] = explode( ',', $atts['product_ids'] );
    			}
    			break;
    	}
    	
    	//Specific categories
    	$categories = isset($atts['categories']) ? trim($atts['categories']) : '';
    	if( !empty($categories) ){
    		$categories_array = explode(',', $categories);
    		$categories_array = array_map( 'trim', $categories_array );
    		if( is_array($categories_array) && !empty($categories_array) ){
    			$args['tax_query'][] = array(
    				array(
    					'taxonomy' => 'product_cat',
    					'field'    => 'term_id',
    					'terms'    => $categories_array
    				)
    			);
    		}
    	}
    	
    	//Specific tags
    	$tags = isset($atts['tags']) ? trim($atts['tags']) : '';
    	if( !empty($tags) ){
    		$tags_array = explode(',', $tags);
    		$tags_array = array_map( 'trim', $tags_array );
    		if( is_array($tags_array) && !empty($tags_array) ){
    			$args['tax_query'][] = array(
    				array(
    					'taxonomy' => 'product_tag',
    					'field'    => 'term_id',
    					'terms'    => $tags_array
    				)
    			);
    		}
    	}
    	
    	// Exclude Products
    	if ( !empty($atts['exclude']) ) {
    		$ids = explode( ',', $atts[ 'exclude' ] );
    		$ids = array_map( 'trim', $ids );			
    		$args['post__not_in'] = $ids;
    		if(!empty($args['post__in'])){
    			$args['post__in'] = array_diff( $args['post__in'], $args['post__not_in'] );
    		}
    	}
    	
    	return $args;
    }
    
    // Product tag field
    add_action( 'product_tag_add_form_fields', 'kapee_child_add_tag_fields', 30 );
    add_action( 'product_tag_edit_form_fields', 'kapee_child_edit_tag_fields', 20 );
    add_action( 'created_term', 'kapee_child_save_tag_fields', 20 );
    add_action( 'edit_term', 'kapee_child_save_tag_fields', 20 );
    
    /**
     * Brand thumbnail fields.
     */
    function kapee_child_add_tag_fields() {
    	$prefix = '_kp_'; // Taking metabox prefix
    	?>
    	<div class="form-field">
    		<label for="kapee-image"><?php echo esc_html__('Header Banner', 'kapee'); ?></label>
    		<input type="hidden" class="kapee-tag-attachment-id" name="<?php echo esc_attr( $prefix );?>kapee_tag_attachment_id">
    		<img class="kapee-tag-attr-img" src="<?php echo esc_url( wc_placeholder_img_src() );?>" alt="<?php echo esc_attr__('Select Image','kapee')?>" height="50px" width="50px">
    		<button class="kapee-tag-image-upload button" type="button"><?php echo esc_html__('Upload/Add Images','kapee');?></button>
    		<button class="kapee-tag-image-clear button" type="button" data-src="<?php echo esc_url( wc_placeholder_img_src() );?>"><?php esc_html_e('Remove image','kapee');?></button>
    		 <p class="description"><?php esc_html_e('Upload banner for this category.', 'kapee'); ?></p>
    	</div>
    	
    	<script>
    		jQuery( document ).ajaxComplete( function( event, request, options ) {
    			if ( request && 4 === request.readyState && 200 === request.status
    				&& options.data && 0 <= options.data.indexOf( 'action=add-tag' ) ) {
    
    				var res = wpAjax.parseAjaxResponse( request.responseXML, 'ajax-response' );
    				if ( ! res || res.errors ) {
    					return;
    				}
    				// Clear Thumbnail fields on submit
    				jQuery( '.kapee-tag-attr-img').attr( 'src', '<?php echo esc_url(wc_placeholder_img_src()); ?>' );
    				jQuery( '.kapee-tag-attachment-id' ).val( '' );
    				return;
    			}
    		} );
    	</script>
    	<?php
    }
    /**
     * Edit tag thumbnail field.
     *
     * @param mixed $term Term (tag) being edited
     */
    function kapee_child_edit_tag_fields( $term ) {
    	$prefix = '_kp_'; // Taking metabox prefix
    	$kapee_tag_attachment_id = get_term_meta( $term->term_id, $prefix.'kapee_tag_attachment_id', true );
    	$image = wc_placeholder_img_src();
    	if(!empty($kapee_tag_attachment_id)){
    		$image = kapee_get_image_src( $kapee_tag_attachment_id,'thumnail');
    	}	
    	?>
    	<tr class="form-field">
    		<th scope="row" valign="top"><label for="kapee-attr-image"><?php esc_html_e('Header Banner', 'kapee'); ?></label></label></th>
    		<td>
    			<input type="hidden" class="kapee-tag-attachment-id" value="<?php echo esc_attr($kapee_tag_attachment_id);?>" name="<?php echo esc_attr( $prefix );?>kapee_tag_attachment_id">
    			<img class="kapee-tag-attr-img" src="<?php echo esc_url($image);?>" alt="<?php esc_attr_e('Select Image','kapee')?>" height="50px" width="50px">
    			<button class="kapee-tag-image-upload button" type="button"><?php esc_html_e('Upload/Add image','kapee');?></button>
    			<button class="kapee-tag-image-clear button" type="button" data-src="<?php echo wc_placeholder_img_src();?>"><?php esc_html_e('Remove image','kapee');?></button>
    			<p class="description"><?php esc_html_e('Upload image for this value.', 'kapee'); ?></p>
    		</td>
    	</tr>
    	<?php
    }
    
    /**
     * save_tag_fields function.
     *
     * @param mixed $term_id Term ID being saved
     */
    function kapee_child_save_tag_fields( $term_id ) {
    	$prefix = '_kp_'; // Taking metabox prefix
    	$kapee_tag_attachment_id = !empty($_POST[$prefix.'kapee_tag_attachment_id']) ? $_POST[$prefix.'kapee_tag_attachment_id'] : '';
    	update_term_meta($term_id, $prefix.'kapee_tag_attachment_id', $kapee_tag_attachment_id);
    } 
    
     /**
     * 1. Display 2nd text field on "Add new product category" admin page
     */
     
    add_action( 'product_cat_add_form_fields', 'bbloomer_wp_editor_add', 10, 2 );
     
    function bbloomer_wp_editor_add() {
        ?>
        <div class="form-field">
            <label for="seconddesc"><?php echo __( 'Second Description', 'woocommerce' ); ?></label>
           
          <?php
          $settings = array(
             'textarea_name' => 'seconddesc',
             'quicktags' => array( 'buttons' => 'em,strong,link' ),
             'tinymce' => array(
                'theme_advanced_buttons1' => 'bold,italic,strikethrough,separator,bullist,numlist,separator,blockquote,separator,justifyleft,justifycenter,justifyright,separator,link,unlink,separator,undo,redo,separator',
                'theme_advanced_buttons2' => '',
             ),
             'editor_css' => '<style>#wp-excerpt-editor-container .wp-editor-area{height:175px; width:100%;}</style>',
          );
     
          wp_editor( '', 'seconddesc', $settings );
          ?>
           
            <p class="description"><?php echo __( 'This is the description that goes BELOW products on the category page', 'woocommerce' ); ?></p>
        </div>
        <?php
    }
     
     /**
     * 2. Display 2nd text field on "Edit product category" admin page
     */
    
    add_action( 'product_cat_edit_form_fields', 'bbloomer_wp_editor_edit', 10, 2 );
     
    function bbloomer_wp_editor_edit( $term ) {
        $second_desc = htmlspecialchars_decode( get_woocommerce_term_meta( $term->term_id, 'seconddesc', true ) );
        ?>
        <tr class="form-field">
            <th scope="row" valign="top"><label for="second-desc"><?php echo __( 'Second Description', 'woocommerce' ); ?></label></th>
            <td>
                <?php
              
             $settings = array(
                'textarea_name' => 'seconddesc',
                'quicktags' => array( 'buttons' => 'em,strong,link' ),
                'tinymce' => array(
                   'theme_advanced_buttons1' => 'bold,italic,strikethrough,separator,bullist,numlist,separator,blockquote,separator,justifyleft,justifycenter,justifyright,separator,link,unlink,separator,undo,redo,separator',
                   'theme_advanced_buttons2' => '',
                ),
                'editor_css' => '<style>#wp-excerpt-editor-container .wp-editor-area{height:175px; width:100%;}</style>',
             );
     
             wp_editor( $second_desc, 'seconddesc', $settings );
             ?>
           
                <p class="description"><?php echo __( 'This is the description that goes BELOW products on the category page', 'woocommerce' ); ?></p>
            </td>
        </tr>
        <?php
    }
    
     /**
     * 3. Save 2nd text field @ admin page
     */
     
    add_action( 'edit_term', 'bbloomer_save_wp_editor', 10, 3 );
    add_action( 'created_term', 'bbloomer_save_wp_editor', 10, 3 );
     
    function bbloomer_save_wp_editor( $term_id, $tt_id = '', $taxonomy = '' ) {
       if ( isset( $_POST['seconddesc'] ) && 'product_cat' === $taxonomy ) {
          update_woocommerce_term_meta( $term_id, 'seconddesc', esc_attr( $_POST['seconddesc'] ) );
       }
    }
     
     /**
     * 4. Display 2nd text field under products @ Product Category pages 
     */
     
    add_action( 'woocommerce_after_shop_loop', 'bbloomer_display_wp_editor_content', 5 );
     
    function bbloomer_display_wp_editor_content() {
       if ( is_product_taxonomy() ) {
          $term = get_queried_object();
          if ( $term && ! empty( get_woocommerce_term_meta( $term->term_id, 'seconddesc', true ) ) ) {
             echo '<p class="term-description">' . wc_format_content( htmlspecialchars_decode( get_woocommerce_term_meta( $term->term_id, 'seconddesc', true ) ) ) . '</p>';
          }
       }
    }
    

    As the code used for categories & tags are very similar, don’t you think we could merge them to reduce the code size/weight? (As I said, I’m not a developer, but it seems to be logical, isn’t it?)

    Thanks a lot in advance!

Viewing 4 replies - 16 through 19 (of 19 total)