• Resolved iqbal1486

    (@iqbal1486)


    i am working with order table.
    From order id , i am getting the product id of the product which are in particular order.
    Now i need to fetch the category name or description using the product id i will receive.
    So how to fetch the category name using product id and send the category name in a mail.?
    Please help

    https://www.ads-software.com/plugins/woocommerce/

Viewing 4 replies - 1 through 4 (of 4 total)
  • Plugin Contributor Mike Jolley

    (@mikejolley)

    Thread Starter iqbal1486

    (@iqbal1486)

    Thanking you for your response.
    But..
    I have used th function this way.

    $result =  get_the_term_list( 88, 'product_cat', 'People: ', ', ' );
    echo "<pre>";
    print_r($result);
    echo "</pre>";

    Here , 88 is a product id and ‘product_cat’ is a taxonomy for product post type.
    I have already fetch the product id from order table using order id.
    When i use above code in function file and run website, it trigger me an error.
    i.e. Invalid taxonomy.

    WP_Error Object
    (
        [errors] => Array
            (
                [invalid_taxonomy] => Array
                    (
                        [0] => Invalid taxonomy
                    )
    
            )
    
        [error_data] => Array
            (
            )
    
    )

    Plugin Contributor Mike Jolley

    (@mikejolley)

    You may be running it too early. Taxonomies are not registered until init.

    Thread Starter iqbal1486

    (@iqbal1486)

    Thank you.
    but i need at that stage so.
    I have tried with query and finally got it.

    function get_terms_by_post_type( $taxonomies, $post_types, $pId ) {
        global $wpdb;
        $query = $wpdb->prepare(
            "SELECT t.*, COUNT(*) from $wpdb->terms AS t
            INNER JOIN $wpdb->term_taxonomy AS tt ON t.term_id = tt.term_id
            INNER JOIN $wpdb->term_relationships AS r ON r.term_taxonomy_id = tt.term_taxonomy_id
            INNER JOIN $wpdb->posts AS p ON p.ID = r.object_id
            WHERE p.post_type IN('%s') AND tt.taxonomy IN('%s') AND p.ID = $pId
            GROUP BY t.term_id",
            join( "', '", $post_types ),
            join( "', '", $taxonomies )
        );
        $results = $wpdb->get_results( $query );
        return $results;
    }
    
    function get_description_by_term_id( $termid ) {
        global $wpdb;
        $query = "SELECT t.* from $wpdb->term_taxonomy AS t
             WHERE t.term_id = $termid
            GROUP BY t.term_id";
        $results = $wpdb->get_results( $query );
        return $results;
    }
    
    $terms = get_terms_by_post_type( array('product_cat'), array('product'), $product_id );
    $termDescription = get_description_by_term_id($terms['0']->term_id);

    Is there any other way?
    Then plz mention.
    Again thank you for your kind response.

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Need to fetch category name from product id?’ is closed to new replies.