Skip to content

Instantly share code, notes, and snippets.

@gagnonconsulting
Created August 10, 2015 16:16
Show Gist options
  • Save gagnonconsulting/f6c18bf97dbb90617199 to your computer and use it in GitHub Desktop.
Save gagnonconsulting/f6c18bf97dbb90617199 to your computer and use it in GitHub Desktop.
<?php
/* Display the current SKU of the variable product */
add_filter( 'woocommerce_cart_item_name', 'add_sku_in_cart', 20, 3);
function add_sku_in_cart( $title, $values, $cart_item_key ) {
$sku = $values['data']->get_sku();
return $sku ? $title . sprintf(" (SKU: %s)", $sku) : $title;
}
/* Make Variation Visible */
add_filter('woocommerce_variation_is_visible', 'product_variation_always_shown', 10, 2);
function product_variation_always_shown($is_visible, $id){
return true;
}
/* Display "Call for Price" instead of empty price */
add_filter('woocommerce_empty_price_html', 'custom_call_for_price');
add_filter('woocommerce_variable_empty_price_html', 'custom_call_for_price');
add_filter('woocommerce_variation_empty_price_html', 'custom_call_for_price');
function custom_call_for_price() {
// return '<a href="http://psuidev.com/wp/contact?sku='+$values['data']->get_sku()+'" target="_blank">Call for Price</a>';
return '<a href="http://psuidev.com/wp/contact" target="_blank">Call for Price</a>';
}
//* Display cats at bottom of all pages */
add_filter('themify_footer_start', 'show_product_cats',20,0);
function show_product_cats() {
$taxonomy = 'product_cat';
$orderby = 'id';
$show_count = 0; // 1 for yes, 0 for no
$pad_counts = 0; // 1 for yes, 0 for no
$hierarchical = 1; // 1 for yes, 0 for no
$title = '';
$empty = 0;
$args = array(
'taxonomy' => $taxonomy,
'orderby' => $orderby,
'order' => $asc,
'show_count' => $show_count,
'pad_counts' => $pad_counts,
'hierarchical' => $hierarchical,
'title_li' => $title,
'hide_empty' => $empty
);
$all_categories = get_categories( $args );
//print_r($all_categories);
echo "<div class='cat_page_list_box'>";
foreach ($all_categories as $cat) {
//print_r($cat);
if (($cat->category_parent == 0) && ($cat->slug != 'featured-products')) {
$category_id = $cat->term_id;
echo '<div class="cat_page_list"><strong><a href="'. get_term_link($cat->slug, 'product_cat') .'">'. $cat->name .'</a></strong>';
$args2 = array(
'taxonomy' => $taxonomy,
'child_of' => 0,
'parent' => $category_id,
'orderby' => $orderby,
'show_count' => $show_count,
'pad_counts' => $pad_counts,
'hierarchical' => $hierarchical,
'title_li' => $title,
'hide_empty' => $empty
);
$sub_cats = get_categories( $args2 );
if($sub_cats) {
$catcnt = 0;
foreach($sub_cats as $sub_category) {
$catcnt++;
if ($catcnt < 11) {
echo '<br /><a href="'. get_term_link($sub_category->slug, 'product_cat') .'">'. $sub_category->name .'</a>';
} else {
echo '<br /><span style="font-size:0.9em"><a href="'. get_term_link($cat->slug, 'product_cat') .
'">...more <strong>'.$cat->name .'</strong></a></span>';
break;
}
}
}
echo '</div>'; // end class cat_page_list
}
}
echo "</div>"; // end class cat_page_list_box
echo "<div class='clear_the_floats'></div>";
}
//add_filter('woocommerce_get_product_thumbnail', 'custom_get_product_thumbnail');
function custom_get_product_thumbnail( ){
global $post;
if ( has_post_thumbnail() ) {
echo get_the_post_thumbnail( $post->ID, 'thumbnail' );
return;
} else {
echo "<p align='center'>No Image<br><strong>View details...</strong></p>";
return;
}
} /* end function */
function format_and_show ($inputstr, $pdffilepath, $label) {
/* print the beginning of the list entry */
echo "<li><strong>$label:</strong>";
/* does it look like an outside link ? */
if (strpos($inputstr, 'http') !== FALSE) {
echo " <a href='$inputstr'";
} else {
echo " <a href='$pdffilepath$inputstr'";
}
/* what type of file is it ? */
if (strpos($inputstr, '.pdf') !== FALSE) {
echo " class='pdflink'";
} else if (strpos($inputstr, '.zip') !== FALSE) {
echo " class='ziplink'";
}
/* finish up the string */
echo " target='_blank' >$inputstr</a></li>";
return;
} /* end funtion */
// Product Attributes shortcode
function custom_product_docs_shortcode( ) {
global $product;
/**** definitions of spec-sheet types
attribute:pa_spec-sheet = Product Datasheet
attribute:pa_spec-sheet-2 = Reach Declaration Certificate
attribute:pa_spec-sheet-3 = 3D Drawings
attribute:pa_spec-sheet-4 = RoHS Certificate
attribute:pa_spec-sheet-5 = CB Report
attribute:pa_spec-sheet-6 = Certifications
attribute:pa_spec-sheet-7 = Application Note
attribute:pa_spec-sheet-8 = Product Documentation
attribute:pa_spec-sheet-9 = Product Documentation
attribute:pa_spec-sheet-10 = Product Documentation
*/
$UPLOADPATH = '/wp/wp-content/uploads/products/';
if ( $product->get_attribute( 'pa_spec-sheet' ) ||
$product->get_attribute( 'pa_spec-sheet-2' ) ||
$product->get_attribute( 'pa_spec-sheet-3' ) ||
$product->get_attribute( 'pa_spec-sheet-4' ) ||
$product->get_attribute( 'pa_spec-sheet-5' ) ||
$product->get_attribute( 'pa_spec-sheet-6' ) ||
$product->get_attribute( 'pa_spec-sheet-7' ) ||
$product->get_attribute( 'pa_spec-sheet-8' ) ||
$product->get_attribute( 'pa_spec-sheet-9' ) ||
$product->get_attribute( 'pa_spec-sheet-10' ) ) {
$pdffilepath =
$UPLOADPATH . $product->get_attribute (' manufacturer' ) . '/DataSheets/';
}
echo "<ul>";
$terms = get_the_terms( $product->id,'pa_spec-sheet');
if ($terms) {
foreach ( $terms as $term ) {
if (($term->name) && ($term->name != 'na')) {
format_and_show ($term->name, $pdffilepath, 'Product Datasheet');
}
}
}
$terms = get_the_terms( $product->id,'pa_spec-sheet-2');
if ($terms) {
foreach ( $terms as $term ) {
if (($term->name) && ($term->name != 'na')) {
format_and_show ($term->name, $pdffilepath , 'Reach Declaration Certificate');
}
}
}
$terms = get_the_terms( $product->id,'pa_spec-sheet-3');
if ($terms) {
foreach ( $terms as $term ) {
if (($term->name) && ($term->name != 'na')) {
format_and_show ($term->name, $pdffilepath, '3D Drawings');
}
}
}
$terms = get_the_terms( $product->id,'pa_spec-sheet-4');
if ($terms) {
foreach ( $terms as $term ) {
if (($term->name) && ($term->name != 'na')) {
format_and_show ($term->name, $pdffilepath ,'RoHS Certificate');
}
}
}
$terms = get_the_terms( $product->id,'pa_spec-sheet-5');
if ($terms) {
foreach ( $terms as $term ) {
if (($term->name) && ($term->name != 'na')) {
format_and_show ($term->name, $pdffilepath, 'CB Report');
}
}
}
$terms = get_the_terms( $product->id,'pa_spec-sheet-6');
if ($terms) {
foreach ( $terms as $term ) {
if (($term->name) && ($term->name != 'na')) {
format_and_show ($term->name, $pdffilepath, 'Certifications');
}
}
}
$terms = get_the_terms( $product->id,'pa_spec-sheet-7');
if ($terms) {
foreach ( $terms as $term ) {
if (($term->name) && ($term->name != 'na')) {
format_and_show ($term->name, $pdffilepath, 'Application Note');
}
}
}
$terms = get_the_terms( $product->id,'pa_spec-sheet-8');
if ($terms) {
foreach ( $terms as $term ) {
if (($term->name) && ($term->name != 'na')) {
format_and_show ($term->name, $pdffilepath, 'Product Documentation');
}
}
}
$terms = get_the_terms( $product->id,'pa_spec-sheet-9');
if ($terms) {
foreach ( $terms as $term ) {
if (($term->name) && ($term->name != 'na')) {
format_and_show ($term->name, $pdffilepath, 'Product Documentation');
}
}
}
$terms = get_the_terms( $product->id,'pa_spec-sheet-10');
if ($terms) {
foreach ( $terms as $term ) {
if (($term->name) && ($term->name != 'na')) {
format_and_show ($term->name, $pdffilepath, 'Product Documentation:');
}
}
}
echo "</ul>";
}
add_shortcode('product_docs', 'custom_product_docs_shortcode');
// get taxonomies terms links
function custom_taxonomies_terms_links(){
// get post by post id
$post = get_post( $post->ID );
// get post type by post
$post_type = $post->post_type;
// get post type taxonomies
$taxonomies = get_object_taxonomies( $post_type, 'objects' );
$out = array();
foreach ( $taxonomies as $taxonomy_slug => $taxonomy ){
// get the terms related to post
$terms = get_the_terms( $post->ID, $taxonomy_slug );
if ( !empty( $terms ) ) {
$out[] = "<h2>" . $taxonomy->label . "</h2>\n<ul>";
foreach ( $terms as $term ) {
$out[] =
' <li><a href="'
. get_term_link( $term->slug, $taxonomy_slug ) .'">'
. $term->name
. "</a></li>\n";
}
$out[] = "</ul>\n";
}
}
return implode('', $out );
}
add_shortcode('product_taxonomies','custom_taxonomies_terms_links');
// USED TO ALLOW SearchWP to find Product Variations
// index WooCommerce product_variation SKUs with the parent post
function my_searchwp_index_woocommerce_variation_skus( $extra_meta, $post_being_indexed ) {
// we only care about WooCommerce Products
if ( 'product' !== get_post_type( $post_being_indexed ) ) {
return $extra_meta;
}
// retrieve all the product variations
$args = array(
'post_type' => 'product_variation',
'posts_per_page' => -1,
'fields' => 'ids',
'post_parent' => $post_being_indexed->ID,
);
$product_variations = get_posts( $args );
if ( ! empty( $product_variations ) ) {
// store all SKUs as a Custom Field with a key of 'my_product_variation_skus'
$extra_meta['my_product_variation_skus'] = array();
// loop through all product variations, grab and store the SKU
foreach ( $product_variations as $product_variation ) {
$extra_meta['my_product_variation_skus'][] = get_post_meta( absint( $product_variation ), '_sku', true );
}
}
return $extra_meta;
}
add_filter( 'searchwp_extra_metadata', 'my_searchwp_index_woocommerce_variation_skus', 10, 2 );
// reference: https://gist.github.com/jchristopher/0cad8418fd9477c57b53
function my_searchwp_custom_field_keys_variation_skus( $keys ) {
$keys[] = 'my_product_variation_skus';
return $keys;
}
add_filter( 'searchwp_custom_field_keys', 'my_searchwp_custom_field_keys_variation_skus', 10, 1 );
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment