I was working on adding the product categories to under the shop image on my site. I have it working using the code below, but the categories do not have spaces between them. I am a bit of a novice here so wondered if someone can help me where I could add a space of comma between each category listing that would be great! Thanks in advance.
add_action( 'woocommerce_after_shop_loop_item', 'after_shop_loop_item_title', 1 );
function after_shop_loop_item_title() {
global $post;
$terms = get_the_terms( $post->ID, 'videocategories' );
$text = "<h3>Category: ";
foreach ($terms as $term) {
$text .= $term->name;
}
$text .= "</h3>";
echo $text;
}
You can use the PHP implode() function. This will create a string from an array and separate them with a separator of your choosing.
In the example below I first created an array of the categories and then used the implode() function in combination with the printf() function to print a comma separated list enclosed by h3 tags:
add_action( 'woocommerce_after_shop_loop_item', 'after_shop_loop_item_title', 10 );
function after_shop_loop_item_title() {
global $post;
$terms = get_the_terms( $post->ID, 'videocategories' );
$product_cats = array();
if ( !empty( $terms ) ) {
// Get an array of the categories
foreach ( $terms as $key => $term ) {
$product_cats[] = sprintf( '%s', get_term_link( $term->slug, 'videocategories' ), $term->name );
}
// Convert product category array into comma separated string
printf( '<h3>Category: %s</h3>', implode( ', ', $product_cats ) );
}
}
Related
I'm needing help trying to add a custom product taxonomy to the orders table of Woocommerce. I have created a php function that works elsewhere on the site that pulls it into the cart table just fine, however it doesn't pull anything into the orders table. If you could take a look at my code and see what I'm missing that would be great! I have a feeling it has to do with calling the product_id instead of the item_id but I tried switching out the two and nothing happened.
add_action( 'woocommerce_order_item_meta_start', 'declaration_order_email_pages', 9999, 4 );
function declaration_order_email_pages( $item_id, $item, $order, $plain_text ) {
$terms = get_the_terms( $product_id, 'declarations' );
$product_cat = array();
foreach ($terms as $term) {
$product_cat[] .= $term->name;
}
echo implode(', ', $product_cat);}
The problem is that there is no $product_id in your code. but since in this hook you get $item object you can get product id like this $item->get_product_id():
add_action('woocommerce_order_item_meta_start','declaration_order_email_pages',9999,4 );
function declaration_order_email_pages( $item_id, $item, $order, $plain_text ) {
$terms = get_the_terms( $item->get_product_id(), 'declarations' );
$product_cat = array();
foreach ($terms as $term) {
$product_cat[] .= $term->name;
}
echo implode(', ', $product_cat);
}
I keep searching for a way to do this, but I can't find anything unfortunately.
I an trying to display all the product's attributes and values, separated by a pipe, in a custom place on the single-product (so for that I was thinking to create a shortcode, so I can place it anywhere I want). the output would be something like this:
BRAND: RENAULT | MODEL: 12 | YEAR: 1973
The code on the Woocommerce template product-attributes.php lists the attributes of the current product on single-product page, but it will list it with some styles I don't want in a place I don't want.
I want to create a shortcode with that code, which is:
<?php foreach ( $product_attributes as $product_attribute_key => $product_attribute ) : ?>
<?php echo wp_kses_post( $product_attribute['label'] ); ?>: <?php echo wp_kses_post( $product_attribute['value'] ); ?> |
<?php endforeach; ?>
How can I create a shortcode with it? I know the general code for a shortcode, but I don't know how to actually integrate the above one in it:
function custom_attributes_product_page() {
// integrate the required code
// Output needs to be return
return
}
// register shortcode
add_shortcode('custom-attributes', 'custom_attributes_product_page');
Would be great if this shortcode would list the attributes and their values separated by a column, like I said above (how to do that?)
Any help is highly appreciated.
Try the following shortcode that will display all product attribute(s) set for a product and their value(s), handling custom attributes too:
function get_product_attributes_shortcode($atts ) {
// Extract shortcode attributes
extract( shortcode_atts( array(
'id' => get_the_ID(),
), $atts, 'display-attributes' ) );
global $product;
if ( ! is_a($product, 'WC_Product') ) {
$product = wc_get_product( $id );
}
if ( is_a($product, 'WC_Product') ) {
$html = []; // Initializing
foreach ( $product->get_attributes() as $attribute => $values ) {
$attribute_name = wc_attribute_label($values->get_name());
$attribute_data = $values->get_data();
$is_taxonomy = $attribute_data['is_taxonomy'];
$option_values = array(); // Initializing
// For taxonomy product attribute values
if( $is_taxonomy ) {
$terms = $values->get_terms(); // Get attribute WP_Terms
// Loop through attribute WP_Term(s)
foreach ( $terms as $term ) {
$term_link = get_term_link( $term, $attribute );
$option_values[] = ''.$term->name.'';
}
}
// For "custom" product attributes values
else {
// Loop through attribute option values
foreach ( $values->get_options() as $term_name ) {
$option_values[] = $term_name;
}
}
$html[] = '<strong>' . $attribute_name . '</strong>: ' . implode(', ', $option_values);
}
return '<div class="product-attributes">' . implode(' | ', $html) . '<div>';
}
}
add_shortcode( 'display-attributes', 'get_product_attributes_shortcode' );
Code goes in functions.php file of the active child theme (or active theme). Tested and works.
USAGE: [display-attributes] or with a defined product Id [display-attributes id="254"]
You will get a display like: BRAND: RENAULT | MODEL: 12 | YEAR: 1973
If you don't want the linked terms, replace:
$term_link = get_term_link( $term, $attribute );
$option_values[] = ''.$term->name.'';
by this:
$option_values[] = $term->name;
I have looked all over for a fix for this but I can't find one that works.
I have this code:
<?php
$terms = get_the_terms( $post->ID, 'dining' );
if ( $terms && ! is_wp_error( $terms ) ) :
$dining = array();
foreach ( $terms as $term ) {
$dining[] = $term->name;
}
$dining = implode(", ", $dining );
?>
<div><b>Open For:</b> <?php echo $dining; ?> </div>
I have a custom post type with it's own custom categories. I want to remove one category from the displayed list. It currently displays like this:
Open For: Category 1, Category 2, Category 3, Category 4
I want to remove Category 2 from this list. How do I do that? I have searched all over but none of the results seem to work in this scenario.
Just don't push it into the $dining array, like this:
$dining = array();
foreach ( $terms as $term ) {
if(!$term->name == 'Category 2'){
$dining[] = $term->name;
}
}
You can try this way:
$list_ignore_categories = array( 'Category 2', 'Else Category' );
foreach ( $terms as $term ) {
if ( in_array( $term->name , $list_ignore_categories ) ) {
continue;
}
$dining[] = $term->name;
}
I need an imploded list of terms from three custom Woocommerce taxonomies including the taxonomy name to display in the product loop. I'll also need to be able to display multiple terms for each taxonomy. However, I can only get it to work with one taxonomy. And I can't figure out how to display the corresponding taxonomy name.
My custom taxonomies are 'artists', 'illustrators' and 'authors'. Here's what I'm trying to accomplish:
Robert Douglas (Author), Bill Johnston (Illustrator), Kyle McBeth (Artist)
function list_author_terms() {
global $post;
$person = get_the_terms(get_the_ID(), 'authors', 'artists', 'illustrators');
if ( $person
&& !is_wp_error( $person )
) {
#usort( $person, function ( $a, $b )
{
return strcasecmp(
$a->slug,
$b->slug
);
});
// Display your terms as normal
$term_list = [];
foreach ( $person as $term )
$term_list[] = '' . esc_html( $term->name ) . '<span class="attribute"> (Author)</span> ';
$term_names[] = $term->name;
echo implode( ', ', $term_list);
echo '<br>';
}
}
Add follows code snippet to achieve your task -
add_action( 'woocommerce_after_shop_loop_item', 'list_author_terms', 6 );
function list_author_terms(){
$taxonomies = array( 'authors', 'artists', 'illustrators' );
$pro_list_terms = array();
foreach ( $taxonomies as $taxonomy ) {
$term_obj_list = get_the_terms( get_the_ID(), $taxonomy );
$tax_obj = get_taxonomy( $taxonomy );
if( $term_obj_list && ! is_wp_error( $term_obj_list ) ){
foreach ( $term_obj_list as $term ) {
$link = get_term_link( $term, $taxonomy );
$pro_list_terms[] = '' . $term->name . ' (' .$tax_obj->labels->singular_name . ')';
}
}
}
echo join( ', ', $pro_list_terms );
}
I want to show two of many attributes of my products on category pages namely heating capacity and cooling capacity as i am making an online store in woocommerce and i am working with my custom theme. it is not a variation or something it's just a normal attribute with text which i would like to show something like this as you can see the heating and cooling system listed on the category page itself i tried the code
if (!function_exists('shop_attributes_in_loop')) {
function shop_attributes_in_loop(){
global $product;
$attributes = $product->get_attributes();
if(!empty($attributes)){
$attribute_single = array_keys($attributes);
$myArray = array();
echo '<div class="product_attributes">';
foreach ($attribute_single as $attribute => $value) {
$myArray[] = ucfirst($value);
}
echo implode(', ', $myArray).'</div>';
}
}
}
add_action('woocommerce_after_shop_loop_item', 'shop_attributes_in_loop');
but it shows something wierd like
Placement-of-outdoor-unit, Installation, Pa_model-no-indoor, Pa_model-no-outdoor etc.. can anyone help me..
You can use this snippet for getting custom field value at archive / category page:
add_action( 'woocommerce_after_shop_loop_item', 'custom_display_post_meta', 9 );
function custom_display_post_meta() {
global $product;
$heating_capacity = get_post_meta( $product->id, 'heating_capacity', true );
$cooling_capacity = get_post_meta( $product->id, 'cooling_capacity', true );
echo $heating_capacity;
echo $cooling_capacity;
}
you can read this article as reference: https://www.skyverge.com/blog/add-information-to-woocommerce-shop-page/
I actually found out how its to be done :)
add_action( 'woocommerce_after_shop_loop_item', 'custom_display_post_meta', 9 );
function custom_display_post_meta() {
global $product;
$attr = array('pa_cooling-capacity-watts', 'pa_heating-capacity-watts');
foreach ( $attr as $attribute ) {
$values = wc_get_product_terms( $product->id, $attribute, array( 'fields' => 'names' ) );
echo apply_filters( 'woocommerce_attribute', wpautop( wptexturize( implode( ', ', $values ) ) ), $attribute, $values );
}
}