2 Column DIV/Table for Taxonomies - Woocommerce - php

I am using 10 custom taxonomies for woocommerce in wordpress and would like to display them in a 2 column div or table on the product page. Not every taxonomy is completed for every item, some may have all and some may only have 1.
At the moment I have created the first column (to show the taxonomy name) using
echo '<div class="tasting-notes">';
if( false != get_the_term_list( $post->ID, 'classifications' ) ) { echo '<li>Classification:</li>';}
if( false != get_the_term_list( $post->ID, 'producers' ) ) { echo '<li>Producer:</li>';}
if( false != get_the_term_list( $post->ID, 'milk_source' ) ) { echo '<li>Milk Type:</li>';}
if( false != get_the_term_list( $post->ID, 'food_pairings' ) ) { echo '<li>Food Pairing:</li> ';}
if( false != get_the_term_list( $post->ID, 'drink_pairings' ) ) { echo '<li>Drink Pairing:</li>';}
if( false != get_the_term_list( $post->ID, 'distribution' ) ) { echo '<li>Distribution:</li> ';}
echo '</div>';
and the second column (to show taxonomy result(s) ) using
echo '<div class="tasting-notes">';
if( false != get_the_term_list( $post->ID, 'classifications' ) ) { echo '<li>' . get_the_term_list($post->ID,'classifications', '', ', ' );}
if( false != get_the_term_list( $post->ID, 'producers' ) ) { echo '<li>' . get_the_term_list($post->ID,'producers', '', ', ' );}
if( false != get_the_term_list( $post->ID, 'food_pairings' ) ) { echo '<li>' . get_the_term_list($post->ID,'food_pairings', '', ' | ' );}
if( false != get_the_term_list( $post->ID, 'drink_pairings' ) ) { echo '<li>' . get_the_term_list($post->ID,'drink_pairings', '', ' | ' );}
if( false != get_the_term_list( $post->ID, 'distribution' ) ) { echo '<li>' . get_the_term_list($post->ID,'distribution', '', ' | ' );}
echo '</div>';
This seems to work ok, except my problem is when one of the taxonomies wraps into a second line, it wraps column 2 but not column 1. So the taxonomy headers and results do not align.
I also tried to return both the taxonomy header and name in the one query using the following
if( false != get_the_term_list( $post->ID, 'producers' ) ) { echo 'ProducerS:' . get_the_term_list($post->ID,'producers', '', ', ' );}
That also works except when one of the results is empty and I can see the blank div. I tried with tables and ran into the same errors.
Please explain how to show 2 columns with Taxonomy and matching results that is also fluid and allows for empty taxonomies.
Example image with food pairing not wrapping correctly ↓
This made more sense in my head.
Thanks,
Richard

Related

Wordpress's WooCommerce how to use callback attribute & fetch product's specification

I have a WordPress website with WooCommerce installed. There are some global attributes with a product's specification. I want to callback them as text in long description using shortcode.
Here's what I am able to get at . Yet it shows only a label for only one attribute, and I want to include multiple attributes with their attribute labels.
/**
* Attribute shortcode callback.
*/
function testfunction( $atts ) {
global $product;
if( ! is_object( $product ) || ! $product->has_attributes() ){
return;
}
// parse the shortcode attributes
$args = shortcode_atts( array(
'attribute' => ''
), $atts );
// start with a null string because shortcodes need to return not echo a value
$html = '';
if( $args['attribute'] ){
// get the WC-standard attribute taxonomy name
$taxonomy = strpos( $args['attribute'], 'pa_' ) === false ? wc_attribute_taxonomy_name( $args['attribute'] ) : $args['attribute'];
if( taxonomy_is_product_attribute( $taxonomy ) ){
// Get the attribute label.
$attribute_label = wc_attribute_label( $taxonomy );
// Build the html string with the label followed by a clickable list of terms.
// heads up that in WC2.7 $product->id needs to be $product->get_id()
//echo strip_tags( get_the_term_list( $product->id, $taxonomy, $attribute_label . ' ' , ', ', '' ));
if ($attribute = Test){
echo $product->get_attribute('Test');
}
elseif ($attribute = Test1){
return $product->get_attribute('Test1');
}
}
}
return $html;
}
add_shortcode( 'display_attribute', 'testfunction' );
Reference 1: Woocommerce - Display single product attribute(s) with shortcodes in Frontend
[Reference 1] that I mentioned above allows me to fetch multiple attributes with multiple attribute labels as I want, but with this the only issue is that it also includes the attribute name which I can't have.
In short, I'd like to fetch 'multiple attribute labels' but without including the attribute itself within the callback text. Any clue ?
Thanks Kathy #helgatheviking for giving a real quick solution. Here's what I was looking for: Hope it helps other community members looking for something similar.
/**
* Attributes shortcode callback.
*/
function so_39394127_attributes_shortcode( $atts ) {
global $product;
if( ! is_object( $product ) || ! $product->has_attributes() ){
return;
}
// parse the shortcode attributes
$args = shortcode_atts( array(
'attributes' => array_keys( $product->get_attributes() ), // by default show all attributes
), $atts );
// is pass an attributes param, turn into array
if( is_string( $args['attributes'] ) ){
$args['attributes'] = array_map( 'trim', explode( '|' , $args['attributes'] ) );
}
// start with a null string because shortcodes need to return not echo a value
$html = '';
if( ! empty( $args['attributes'] ) ){
foreach ( $args['attributes'] as $attribute ) {
// get the WC-standard attribute taxonomy name
$taxonomy = strpos( $attribute, 'pa_' ) === false ? wc_attribute_taxonomy_name( $attribute ) : $attribute;
if( taxonomy_is_product_attribute( $taxonomy ) ){
// Build the html string with the label followed by a clickable list of terms.
//$html .= get_the_term_list( $product->get_id(), $taxonomy, '<li class="bullet-arrow">': ' , ', ', '</li>' );
$html .= get_the_term_list( $product->get_id(), $taxonomy, '', ', ', '' );
}
}
// if we have anything to display, wrap it in a <ul> for proper markup
// OR: delete these lines if you only wish to return the <li> elements
if( $html ){
$html = '' . $html . '';
}
}
return $html;
}
add_shortcode( 'display_attributes', 'so_39394127_attributes_shortcode' );

Show Woocommerce subcategories on single page by their parents slug or ID

I am trying to add Publisher, Topic and Author to a Single Product with help of categories/subcategories. This is how it looks after hours of coding/and copying (very fresh with WooCommerce tbh)
This is what I am getting, but it shows ALL subcategories, not only the ones associated to the Product, this is the code I am using
function get_product_subcategories_list( $category_slug ){
$terms_html = array();
$taxonomy = 'product_cat';
// Get the product category (parent) WP_Term object
$parent = get_term_by( 'slug', $category_slug, $taxonomy );
// Get an array of the subcategories IDs (children IDs)
$children_ids = get_term_children( $parent->term_id, $taxonomy );
// Loop through each children IDs
foreach($children_ids as $children_id){
$term = get_term( $children_id, $taxonomy ); // WP_Term object
$term_link = get_term_link( $term, $taxonomy ); // The term link
if ( is_wp_error( $term_link ) ) $term_link = '';
// Set in an array the html formated subcategory name/link
$terms_html[] = '' . $term->name . '';
}
return '<span class="subcategories-' . $category_slug . '">' . implode( ', ', $terms_html ) . '</span>';
}
add_action('woocommerce_single_product_summary','monolith_cat_scan', 31);
function monolith_cat_scan() {
echo '<p>Topic : ';
echo get_product_subcategories_list( 'topics' );
echo '</p>';
echo '<p>Publisher : ';
echo get_product_subcategories_list( 'publishers' );
echo '</p>';
echo '<p>Author: ';
echo get_product_subcategories_list( 'authors' );
echo '</p>';
}
But I can't get the whole thing to work like I want to and get the subcategories of the Single Product, in this example only Spirituality, SOUNDS TRUE INC (only sub cat in Publishers), and Allan Watts should be there.
I'd appreciate every help!
I got a working code (it doesn't look beautiful I know but it's the best I could do and it does the trick.
add_action('woocommerce_single_product_summary','monolith_cat_scan', 31);
function monolith_cat_scan() {
global $post;
$cats = get_the_terms( $post->ID, 'product_cat' );
if ( ! empty( $cats ) ) {
foreach ( $cats as $term ) {
if( $term->parent == 30 ) {
echo '<p>Topic : ' . $term->name . '';
}
}
}
}
add_action('woocommerce_single_product_summary','monolith_cat_scan2', 32);
function monolith_cat_scan2() {
global $post;
$cats = get_the_terms( $post->ID, 'product_cat' );
if ( ! empty( $cats ) ) {
foreach ( $cats as $term ) {
if( $term->parent == 31 ) {
echo '<p>Author : ' . $term->name . '';
}
}
}
}
add_action('woocommerce_single_product_summary','monolith_cat_scan3', 33);
function monolith_cat_scan3() {
global $post;
$cats = get_the_terms( $post->ID, 'product_cat' );
if ( ! empty( $cats ) ) {
foreach ( $cats as $term ) {
// If parent cat ID = 116 echo subcat name...
if( $term->parent == 32 ) {
echo '<p>Publisher : ' . $term->name . '';
}
}
}
}

Adding "Home" at the beginning of breadcrumbs

I'm having issues formatting the breadcrumbs on my staging site. I had them set up with the correct categories before, but the parent category kept displaying the wrong category (which I did fix, but...). The fix caused the "home" option to disappear. I don't know any php and have been trying without success to figure out how to add a "Home /" option on each page before the category. Below is the code I'm using now. Anyone have any ideas?
if ( ! empty( $breadcrumb ) ) {
echo $wrap_before;
if ( is_single() && get_post_type() == 'product' ) {
echo $prepend;
if ( $terms = get_the_terms( $post->ID, 'product_cat' ) ) {
$referer = wp_get_referer();
$printed = array();
foreach( $terms as $term){
if(in_array($term->id, $printed)) continue;
$referer_slug = (strpos($referer, '/'.$term->slug.'/'));
if(!$referer_slug==false){
$printed[] = $term->id;
$category_name = $term->name;
$ancestors = get_ancestors( $term->term_id, 'product_cat' );
$ancestors = array_reverse( $ancestors );
foreach ( $ancestors as $ancestor ) {
$ancestor = get_term( $ancestor, 'product_cat' );
if ( ! is_wp_error( $ancestor ) && $ancestor )
echo $before . '' . $ancestor->name . '' . $after . $delimiter;
}
echo $before . '' . $category_name . '' . $after . $delimiter;
}
}
}
echo $before . get_the_title() . $after;
} else {
foreach ( $breadcrumb as $key => $crumb ) {
echo $before;
if ( ! empty( $crumb[1] ) && sizeof( $breadcrumb ) !== $key + 1 ) {
echo '' . esc_html( $crumb[0] ) . '';
} else {
echo esc_html( $crumb[0] );
}
echo $after;
if ( sizeof( $breadcrumb ) !== $key + 1 ) {
echo $delimiter;
}
}
}
echo $wrap_after;
}
Thanks for the help in advance!
I think you can achieve this by changing the following line echo $wrap_before; to this:
echo $wrap_before.'Home »';

Print $variable into an HTML table

I have no knowledge of PHP but I have already been hit so far that I want to put the last bit myself. As you can see below, I retrieve data from the database. Below is the part that it is about.
<div class="gp-hub-block gp-hub-block-three">
<?php if ( ghostpool_option( 'hub_fields' ) ) {
// Support for foreign characters
$char_table = array();
if ( function_exists( 'ghostpool_hub_field_characters' ) ) {
$char_table = ghostpool_hub_field_characters();
}
foreach( ghostpool_option( 'hub_fields' ) as $gp_hub_field ) {
$gp_hub_field_slug = strtr( $gp_hub_field, $char_table );
if ( function_exists( 'iconv' ) ) {
$gp_hub_field_slug = iconv( 'UTF-8', 'UTF-8//TRANSLIT//IGNORE', $gp_hub_field_slug );
}
$gp_hub_field_slug = sanitize_title( $gp_hub_field_slug );
$gp_hub_field_slug = substr( $gp_hub_field_slug, 0, 32 );
$gp_term_list = get_the_term_list( $post_id, $gp_hub_field_slug, '<table class="casino-info"><tbody><span class="aaa"><tr class="casino-info-titel"><th scope="row">' . __($gp_hub_field,'JDcustomnew') . ':</th><td scope="row" data-label="'<?php echo ($gp_hub_field ); ?>'">', ', ', '</td></tbody></table>' );
if ( ! $gp_term_list OR is_wp_error( $gp_term_list ) ) {
continue;
}
if ( ghostpool_option( 'hub_field_links' ) == 'disabled' ) {
$gp_term_list = preg_replace( '/<\/?a[^>]*>/', '', $gp_term_list );
}
echo wp_kses_post( $gp_term_list );
}
} ?>
</div>
</div>
You will see below for which part it is exactly
$gp_term_list = get_the_term_list( $post_id, $gp_hub_field_slug, '<table class="casino-info"><tbody><span class="aaa"><tr class="casino-info-titel"><th scope="row">' . __($gp_hub_field,'JDcustomnew') . ':</th><td scope="row" data-label="'<?php echo ($gp_hub_field ); ?>'">', ', ', '</td></tbody></table>' );
What I want to do is return the $gp_hub_field to the datalabel of my table. Unfortunately, I can not do this. Someone who can help me?
Don't blame me for my PHP skills please.
EDIT:
I see this with the PHP code in the data-label:
Its important for Google that the data label has the same title as the <TH> I need to see this with the good data-lable title:

Presumably simple PHP if/else statement not working

I seem to be having trouble with another piece of what seems to be super basic PHP, but it just won't work for me.
My client (real estate website) needs to be able to have properties with no price to be either “price upon request” OR “auction”. Currently, leaving the price field blank only allows for one.
I tried changing the following code:
$listing_price_labels = array(
‘sold’ => __( ‘Sold’, ‘wpsight’ ),
‘rented’ => __( ‘Rented’, ‘wpsight’ ),
‘request’ => __( ‘Price on request’, ‘wpsight’ ),
‘auction’ => __( ‘Auction’, ‘wpsight’ ), ***– Added this line***
);
And where this code is found…
if( is_admin() )
$listing_price .= ‘<br />’ . wpsight_get_price_value();
} elseif( empty( $listing_price ) ) {
// When no price available Price on request
$listing_price = ‘<span class=”listing-price-on-request”>’ . $listing_price_labels['request'] . ‘</span><!– .listing-price-on-request –>’;
} elseif( $listing_price = ‘auction’ ) {
// When price field contains ‘auction’ (case sensitive)
$listing_price = ‘<span class=”listing-price-on-request”>’ . $listing_price_labels['auction'] . ‘</span><!– .listing-price-on-request –>’;
}
function wpsight_get_price( $post_id = '' ) {
// Get post ID from $post_id
if( empty( $post_id ) )
$post_id = get_the_ID();
// If still empty, return false
if( empty( $post_id ) )
return false;
// Set listing price labels
$listing_price_labels = array(
'sold' => __( 'Sold', 'wpsight' ),
'rented' => __( 'Rented', 'wpsight' ),
'request' => __( 'Price on request', 'wpsight' ),
'auction' => __( 'Auction', 'wpsight' ),
);
$listing_price_labels = apply_filters( 'wpsight_get_price_labels', $listing_price_labels );
// Get listing price
$listing_price = wpsight_get_price_value();
// Get custom fields
$custom_fields = get_post_custom( $post_id );
$listing_status = isset( $custom_fields['_price_status'][0] ) ? $custom_fields['_price_status'][0] : false;
$listing_availability = isset( $custom_fields['_price_sold_rented'][0] ) ? $custom_fields['_price_sold_rented'][0] : false;
// Create price output
if( ! empty( $listing_availability ) ) {
// When listing is not available
$sold_rented = ( $listing_status == 'sale' ) ? $listing_price_labels['sold'] : $listing_price_labels['rented'];
// Display sold/rented bold red in admin
$style = is_admin() ? ' style="color:red;font-weight:bold"' : false;
$listing_price = '<span class="listing-price-sold-rented"' . $style . '>' . $sold_rented . '</span><!-- .listing-price-sold-rented -->';
if( is_admin() )
$listing_price .= '<br />' . wpsight_get_price_value();
} elseif( empty( $listing_price ) ) {
// When no price available Price on request
$listing_price = '<span class="listing-price-on-request">' . $listing_price_labels['request'] . '</span><!-- .listing-price-on-request -->';
} elseif( $listing_price == "auction" ) {
// When price field contains 'auction' (case sensitive)
$listing_price = '<span class="listing-price-on-request">' . $listing_price_labels['auction'] . '</span><!-- .listing-price-on-request -->';
}
return apply_filters( 'wpsight_listing_price', $listing_price );
}
I’m sure my syntax must just be wrong, because with that code in place it makes any property with anything at all written into the price field display “auction”.
Can anyone see what I've done wrong?
try:
if( is_admin() ){
$listing_price .= ‘<br />’ . wpsight_get_price_value();
} elseif( empty( $listing_price ) ) {
// When no price available Price on request
$listing_price = ‘<span class=”listing-price-on-request”>’ . $listing_price_labels['request'] . ‘</span><!– .listing-price-on-request –>’;
}
btw its not recommended to use if( is_admin() ) since its only applicable on 1 line.

Categories