Skip to content
Advertisement

Show Woocommerce taxonomy in emails

I have set up an attribute for my products “Delivery Time”. I need to display this on the single product page, in the cart&checkout table an in the order emails.

I have been sucessfull in displaying in on the single product page and in the cart/order tables with this:

function wp_woo_cart_attributes( $cart_item, $cart_item_key ) {

$item_data = $cart_item_key['data'];
$attributes = $item_data->get_attributes();


if ( ! $attributes ) {
    return $cart_item;
}

$out = $cart_item . '<br />';

foreach ( $attributes as $attribute ) {

    // skip variations
    if ( $attribute->get_variation() ) {
        continue;
    }
    $name = $attribute->get_name();
    if ( $attribute->is_taxonomy() ) {

        $product_id = $item_data->get_id();
        $terms = wp_get_post_terms( $product_id, $name, 'all' );

        if ( ! empty( $terms ) ) {
            if ( ! is_wp_error( $terms ) ) {

                // get the taxonomy
                $tax = $terms[0]->taxonomy;

                // get the tax object
                $tax_object = get_taxonomy($tax);

                // get tax label
                if ( isset ( $tax_object->labels->singular_name ) ) {
                    $tax_label = $tax_object->labels->singular_name;
                } elseif ( isset( $tax_object->label ) ) {
                    $tax_label = $tax_object->label;
                    // Trim label prefix since WC 3.0
                    $label_prefix = 'Product ';
                    if ( 0 === strpos( $tax_label,  $label_prefix ) ) {
                        $tax_label = substr( $tax_label, strlen( $label_prefix ) );
                    }
                }
                $out .= $tax_label . ': ';
                $tax_terms = array();
                foreach ( $terms as $term ) {
                    $single_term = esc_html( $term->name );
                    array_push( $tax_terms, $single_term );
                }
                $out .= implode(', ', $tax_terms). '<br />';

            }
        }

    } else {

        // not a taxonomy 

        $out .= $name . ': ';
        $out .= esc_html( implode( ', ', $attribute->get_options() ) ) . '<br />';
    }
}
echo $out;
}

add_filter( 'woocommerce_cart_item_name', 'wp_woo_cart_attributes', 10, 2 );

//Single Product
function product_attribute_delivery(){
global $product;

$taxonomy = 'pa_delivery';
$value = $product->get_attribute( $taxonomy );

if ( $value ) {
    $label = get_taxonomy( $taxonomy )->labels->singular_name;
    echo '<p>' . $label . ': ' . $value . '</p>';
}}
add_action( 'woocommerce_single_product_summary', 'product_attribute_dimensions', 25 );

How do I add it to the order emails, and is there even a solution for only displaying it on the single product page when the item is in stock?

Advertisement

Answer

well let’s make first the above code more simplified and keep it clean.

You can delete this one if you want only to display the attribute in Single product page only

//for Cart items and mini cart
add_filter('woocommerce_cart_item_name', 'wp_woo_cart_attributes', 10, 2);
function wp_woo_cart_attributes($cart_item, $cart_item_key)
{

    $productId = $cart_item_key['product_id'];
    $product = wc_get_product($productId);
    $taxonomy = 'pa_delivery';
    $value = $product->get_attribute($taxonomy);
    if ($value) {
        $label = get_taxonomy($taxonomy)->labels->singular_name;
        $cart_item .= "<br><p> $label : $value</p>";
    }
    return $cart_item;
}

Single Product

add_action('woocommerce_single_product_summary', 'product_attribute_delivery', 25);
function product_attribute_delivery()
{
    global $product;
    $taxonomy = 'pa_delivery';
    $value = $product->get_attribute($taxonomy);

    if ($value) {
        $label = get_taxonomy($taxonomy)->labels->singular_name;
        echo '<p>' . $label . ': ' . $value . '</p>';
    }}

to display specific taxonomy in Email Order Items

add_action('woocommerce_order_item_meta_end', 'custom_item_meta', 10, 4);

function custom_item_meta($item_id, $item, $order, $plain_text)
{

    $productId = $item->get_product_id();
    $product = wc_get_product($productId);
    $taxonomy = 'pa_delivery';
    $value = $product->get_attribute($taxonomy);
    if ($value) {
        $label = get_taxonomy($taxonomy)->labels->singular_name;
        echo  "<br><p> $label : $value</p>";
    }

}

the code above is tested and working correctly

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement