Category Archives: Woocommerce

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 '

';
}
}
}
});

Make shortcode dynamic in Elementor

Problem adding shortcode with static value to template (which is used for all post or products etc) eg

[premmerce_get_bundles_by_main_product_id id="X"]

Create new shortcode, in corporating original shortcode:

function so_extend_frequent_bought_shortcode() {
global $product;
$id = $product->get_id();

return do_shortcode( '[premmerce_get_bundles_by_main_product_id id="' . $id . '"]');
}
add_shortcode( 'my_new_shortcode', 'so_extend_frequent_bought_shortcode' );

Similarily, to add dynamic Javascript to elementor product template:

// shortcode to add tab-click js to product pages
function redraw_data_table_shortcode( $atts ) {
global $product;
$id = $product->get_id();
$output = '
';
return $output;
}
add_shortcode( 'redrawdatatable', 'redraw_data_table_shortcode' );

Woocommerce use same base url for products and categories

// use same base url for products and categories
function wpse_291143_generate_taxonomy_rewrite_rules( $wp_rewrite ) {

global $wp_rewrite;
$base = "shop";

$rules = array();
$terms = get_terms( array( 'taxonomy' => 'product_cat', 'hide_empty' => false ));

foreach($terms as $term) {
$term_children = get_terms( array(
'taxonomy' => 'product_cat',
'parent' => intval($term->term_id),
'hide_empty' => false
)
);
if($term_children) {
foreach ( $term_children as $term_child ) {
$rules[$base . '/' . $term->slug . '/' . $term_child->slug . '/?$'] = 'index.php?product_cat=' . $term_child->slug;
}
}
$rules[$base . '/' . $term->slug . '/?$'] = 'index.php?product_cat=' . $term->slug;
}

$wp_rewrite->rules = $rules + $wp_rewrite->rules;
return $wp_rewrite->rules;
}
add_action('generate_rewrite_rules', 'wpse_291143_generate_taxonomy_rewrite_rules');

Add “Products” to breadcrumb on woocommerce category pages

// add "Products" to breadcrumb on category pages and other missing levels
add_filter( 'wpseo_breadcrumb_links', 'wpseo_breadcrumb_add_woo_shop_link' );
function wpseo_breadcrumb_add_woo_shop_link( $links ) {
global $post;
if ( is_woocommerce()
&& !is_shop() // not top level shop page
&& !is_product() // not on single product page
) {
$breadcrumb[] = array(
'url' => get_permalink( woocommerce_get_page_id( 'shop' ) ),
'text' => 'Products',
);
array_splice( $links, 1, -2, $breadcrumb );
} elseif ( is_post_type_archive( 'news' ) || is_singular( 'news' ) ) {
$breadcrumb[] = array(
'url' => get_permalink( '29' ),
'text' => 'Information',
);
$breadcrumb[] = array(
'url' => get_permalink( '50' ),
'text' => 'Media',
);
array_splice( $links, 1, -2, $breadcrumb );
} elseif ( is_home() ) { // any other archive...
$breadcrumb[] = array(
'url' => get_permalink( '29' ),
'text' => 'Information',
);
$breadcrumb[] = array(
'url' => get_permalink( '50' ),
'text' => 'Media',
);
array_splice( $links, 1, -2, $breadcrumb );

} elseif ( is_singular( 'post' ) ) {
$breadcrumb[] = array(
'url' => get_permalink( '29' ),
'text' => 'Information',
);
$breadcrumb[] = array(
'url' => get_permalink( '50' ),
'text' => 'Media',
);
$breadcrumb[] = array(
'url' => get_permalink( '54' ),
'text' => 'Blog',
);
array_splice( $links, 1, -2, $breadcrumb );
}
return $links;
}