Monthly Archives: January 2020

Add woocommerce product category description to main wordpress search

The only way I could find to do this was to search for the terms separately and then insert the results before the main result (only on page 1 though).

add_action( 'loop_start', function(){
  $searchvar = get_search_query();
  $paged = get_query_var('paged'); 
  $debug = 'search: '. $searchvar;
  // only show on seach page and only for first page of results
  if($searchvar>'' && $paged==0){
	$s = $searchvar;
	$results = array();
	$searchterms = explode(' ',$s); // "+" is removed!
	//print_r($searchterms);
	$termcount=0;
	foreach($searchterms as $searchterm){
		$debug .= '

' . $searchterm . '
--------------------------'; $termcount++; $exclude_arr = array(); ${'termsfound'.$termcount} = array($searchterm); // search product category title $debug .= '

category title'; $terms = get_terms(array( 'taxonomy' => 'product_cat', 'name__like' => $searchterm, 'hide_empty' => true, // Optional )); if ( count($terms) > 0 ){ foreach ( $terms as $term ) { $results[] = $term; $exclude_arr[] = $term->term_id; ${'termsfound'.$termcount}[] = $term->term_taxonomy_id; } $debug .= '
excluded=' . implode(',',$exclude_arr); $debug .= '
terms=' . implode(',',${'termsfound'.$termcount}); } // search product category description $debug .= '

category description'; $exclude = implode(",", $exclude_arr); $terms2 = get_terms(array( 'taxonomy' => 'product_cat', 'description__like' => $searchterm, 'hide_empty' => true, // Optional 'exclude' => $exclude, )); if ( count($terms2) > 0 ){ foreach ( $terms2 as $term2 ) { $results[] = $term2; $exclude_arr[] = $term2->term_id; ${'termsfound'.$termcount}[] = $term2->term_taxonomy_id; } $debug .= '
excluded=' . implode(',',$exclude_arr); $debug .= '
terms2=' . implode(',',${'termsfound'.$termcount}); } // search product category acf description2 field $debug .= '

acf description2 field'; $exclude = implode(",", $exclude_arr); $description2terms = get_terms(array( 'taxonomy' => 'product_cat', 'hide_empty' => true, 'exclude' => $exclude, 'meta_query' => array( array( 'key' => 'description_2', 'value' => $searchterm, 'compare' => 'LIKE' ), ), )); if ( count($description2terms) > 0 ){ foreach ( $description2terms as $term3 ) { $results[] = $term3; ${'termsfound'.$termcount}[] = $term3->term_taxonomy_id; } $debug .= '
terms3=' . implode(',',${'termsfound'.$termcount}); } } if ($termcount>1) { // search is AND, therefore only show results which appear in all result sets $commonterms = $termsfound1; for ($i=2; $i<=$termcount; $i++) { $commonterms = array_intersect($commonterms, ${'termsfound'.$i}); } $commonterms = array_values($commonterms); if(count($commonterms) > 0){ $debug .= '

common terms=' . implode(',',$commonterms); $terms3 = get_terms(array( 'taxonomy' => 'product_cat', 'include' => $commonterms, )); foreach ( $terms3 as $term ) { $finalresults[] = $term; } } else { $finalresults = array(); // no common results //echo 'NOTHING!'; } } else { $finalresults = $results; // one one term searched for } //echo '
'; echo $debug; echo '

';
// combine results
if ( count($finalresults) > 0 ){
/* [term_id] [name] [slug] [term_group] [term_taxonomy_id] [taxonomy] [description] [parent] [count] [filter] [term_order] */
foreach ( $finalresults as $result ) {
$thumbnail_id = get_woocommerce_term_meta( $result->term_id, 'thumbnail_id', true );
$imageurl = wp_get_attachment_image_url( $thumbnail_id, 'swiper_news_image_size' );
$imageurlsm = wp_get_attachment_image_url( $thumbnail_id, 'post_image_thumb' );
//$excerpt = esc_html( $result->description );
$excerpt = substr(esc_html( $result->description ),0,strpos(esc_html( $result->description ),' ',250));
echo '

';
}
}
}
});

Gutenburg block editor styles

New JS file (eg theme/js/editor.js)

wp.domReady( () => {
    wp.blocks.registerBlockStyle( ‘core/paragraph’, [
        {
            name: ‘default’,
            label: ‘Default’,
            isDefault: true,
        },
        {
            name: ‘intro’,
            label: ‘Intro’,
        }
    ]);
} );

 

New editor CSS file (eg theme/my-editor.css)


/* Block editor styles */
p.is-style-intro {font-size:30px!important;line-height:40px!important;} /* seem to need "!important" for font-size & line height */
/* tweaks to make block editor preview look more accurate */
.block-editor-block-preview__content p.is-style-intro {font-size:40px!important;line-height:60px!important; font-weight: bold;} /* little preview */
.block-editor-block-switcher__preview p.is-style-intro {font-size:40px!important;line-height:60px!important;} /* larger pop-out preview */

Functions file additions

/**
 * Gutenberg scripts and styles
 * @link https://www.billerickson.net/block-styles-in-gutenberg/
 *
 * enqueue_block_editor_assets = load in backend only
 * enqueue_block_assets = load in front and backend
 */
function be_gutenberg_scripts() {
    wp_enqueue_script(
        ‘be-editor’,
        get_stylesheet_directory_uri() . ‘/js/editor.js’,
        array( ‘wp-blocks’, ‘wp-dom’ ),
        filemtime( get_stylesheet_directory() . ‘/js/editor.js’ ),
        true
    );
}
add_action( ‘enqueue_block_editor_assets’, ‘be_gutenberg_scripts’ );
function be_gutenberg_styles() {
    wp_enqueue_style(
        ‘be-editor-styles’,
        get_stylesheet_directory_uri() . ‘/my-editor-styles.css’,
        false,
        filemtime( plugin_dir_path( __FILE__ )  . ‘my-editor-styles.css’ )
    );
}
add_action( ‘enqueue_block_editor_assets’, ‘be_gutenberg_styles’ );

 

And style for front end!
p.is-style-intro {font-size: 30px;line-height: 40px;color:magenta;}