How to show WooCommerce custom taxonomy links that are linked to product on a product page?

Question

I have created a custom taxonomy for WooCommerce products:

function create_wcproduct_taxonomies() {

    // product materials taxonomy
    $labels = array(
        'name'              => _x( 'Materials', 'taxonomy general name' ),
        'singular_name'     => _x( 'Material', 'taxonomy singular name' ),
        'search_items'      => __( 'Search'),
        'all_items'         => __( 'All' ),
        'parent_item'       => __( 'Parent' ),
        'parent_item_colon' => __( 'Parent'),
        'edit_item'         => __( 'Edit' ),
        'update_item'       => __( 'Edit' ),
        'add_new_item'      => __( 'Add new' ),
        'new_item_name'     => __( 'Add new' ),
        'menu_name'         => __( 'Materials' ),
    );

    $args = array(
        'hierarchical'      => false,
        'labels'            => $labels,
        'show_ui'           => true,
        'show_admin_column' => true,
        'query_var'         => true,
        'rewrite'           => false,
    );
    register_taxonomy( 'product_material', array( 'product', 'page'), $args );

}

add_action( 'init', 'create_wcproduct_taxonomies', 0 );

I am trying to print the taxonomy name and a url on a woocommerce / templates / single-product / meta.php section using this code:

<?php echo wc_get_product_material_list( $product->get_id(), ', ', '<span class="posted_in">' . _n( 'Material:', 'Materials:', count( $product->get_category_ids() ), 'woocommerce' ) . ' ', '</span>' ); ?>

Unfortunately it is wrong. How can I print the url and Material taxonomy which is related to the product?

I made some partly progress with this but it is only showing the taxonomy name without a url to it:

add_action( 'woocommerce_product_meta_end', 'action_product_meta_end' );
function action_product_meta_end() {
    global $product;

    $taxonomy = 'product_material';

    $terms = wp_get_post_terms( $product->get_id(), $taxonomy, ['fields' => 'names']);

    if( ! empty($terms) ) {

        echo '<p class="' . $taxonomy . '">' . implode(', ', $terms) . '</p>';

    }
}
0
twelvell 8 months 2020-12-09T08:10:35-05:00 0 Answers 6 views 0

Leave an answer

Browse
Browse