• Resolved alexli126

    (@alexli126)


    Hello,

    We need your support for the following purpose:

    When access or viewing a Woo product, show a popup that contain a clickable product category, so that we can show message like this way:

    FIND more similar product in the following category

    I asked a Chatgpt to provide a code snippet and put [product_categories] in the popup content to achieve the above purpose, but it always show all of categories in Popup.

    Here are the code snippets that I already tried:

    // Create a shortcode to display product categories with clickable links
    function display_product_categories() {
    global $product;

    if ( is_product() ) {
    // Get the product categories as clickable links
    $categories = wc_get_product_category_list( $product->get_id(), ', ', '<p class="posted_in">', '</p>' );

    // Return the categories if they exist
    if ( $categories ) {
    return $categories;
    }
    }

    return ''; // Return nothing if not on a product page
    }
    add_shortcode( 'product_categories', 'display_product_categories' );
    // Create a shortcode to display product categories with clickable links
    function display_product_categories() {
    global $product;

    if ( is_product() ) {
    // Get the product categories as clickable links
    $categories = wc_get_product_category_list( $product->get_id(), ', ', '<p class="posted_in">', '</p>' );

    // Return the categories if they exist
    if ( $categories ) {
    return $categories;
    }
    }

    return ''; // Return nothing if not on a product page
    }
    add_shortcode( 'product_categories', 'display_product_categories' );
    // Create a shortcode to display specific categories of the current product
    function display_specific_product_categories() {
    if ( is_product() ) {
    global $post;

    // Get the product object from the post ID
    $product = wc_get_product( $post->ID );

    // Ensure we are working with a valid product
    if ( $product ) {
    // Get the categories of the current product as clickable links
    $categories = wp_get_post_terms( $product->get_id(), 'product_cat', array( 'fields' => 'all' ) );

    if ( ! empty( $categories ) && ! is_wp_error( $categories ) ) {
    $category_links = array();

    // Loop through each category and generate clickable links
    foreach ( $categories as $category ) {
    $category_links[] = '<a href="' . get_term_link( $category ) . '">' . $category->name . '</a>';
    }

    // Return the categories as a comma-separated list
    return '<p class="posted_in">' . implode( ', ', $category_links ) . '</p>';
    }
    }
    }

    return ''; // Return nothing if not on a product page or no categories found
    }
    add_shortcode( 'product_categories', 'display_specific_product_categories' );
    // Create a shortcode to display the relevant categories of the current product
    function display_relevant_product_categories() {
    if ( is_product() ) {
    global $post;

    // Get the product object
    $product = wc_get_product( $post->ID );

    if ( $product ) {
    // Get the specific product categories (taxonomy 'product_cat')
    $categories = get_the_terms( $product->get_id(), 'product_cat' );

    // Check if categories exist and are not an error
    if ( ! empty( $categories ) && ! is_wp_error( $categories ) ) {
    $category_links = array();

    // Loop through each category and create clickable links
    foreach ( $categories as $category ) {
    $category_links[] = '<a href="' . get_term_link( $category->term_id ) . '">' . $category->name . '</a>';
    }

    // Return the categories as a comma-separated list of clickable links
    return '<p class="posted_in">Categories: ' . implode( ', ', $category_links ) . '</p>';
    } else {
    return '<p class="posted_in">No categories assigned.</p>';
    }
    }
    }

    return ''; // Return nothing if not on a product page or no categories found
    }
    add_shortcode( 'product_categories', 'display_relevant_product_categories' );
    // Create a shortcode to display the specific categories of the current product
    function display_current_product_categories() {
    // Ensure we are on a single product page
    if ( is_product() ) {
    global $post;

    // Get the product object from the current post
    $product = wc_get_product( $post->ID );

    // Ensure the product exists
    if ( $product ) {
    // Get the categories for this specific product
    $categories = wp_get_post_terms( $product->get_id(), 'product_cat' );

    // Check if any categories exist for the product
    if ( !empty( $categories ) && !is_wp_error( $categories ) ) {
    $category_links = array();

    // Loop through each category and create a clickable link
    foreach ( $categories as $category ) {
    $category_links[] = '<a href="' . esc_url( get_term_link( $category->term_id ) ) . '">' . esc_html( $category->name ) . '</a>';
    }

    // Return the categories as a comma-separated list of clickable links
    return '<p class="posted_in">Categories: ' . implode( ', ', $category_links ) . '</p>';
    } else {
    // If no categories are found, return a message
    return '<p class="posted_in">No categories assigned.</p>';
    }
    }
    }

    return ''; // Return nothing if not on a product page or if product doesn't exist
    }
    add_shortcode( 'product_categories', 'display_current_product_categories' );
    // Create a shortcode to display the specific categories of the current product
    function display_product_categories_fixed() {
    // Ensure we are on a single product page
    if ( is_product() ) {
    global $post;

    // Get the product object
    $product = wc_get_product( $post->ID );

    // Ensure the product exists
    if ( $product ) {
    // Get the product categories (product_cat) for this specific product
    $categories = get_the_terms( $product->get_id(), 'product_cat' );

    // Check if categories exist and are not an error
    if ( !empty( $categories ) && !is_wp_error( $categories ) ) {
    $category_links = array();

    // Generate clickable links for each category
    foreach ( $categories as $category ) {
    // Ensure each category link is correct
    $category_link = esc_url( get_term_link( $category->term_id ) );
    $category_name = esc_html( $category->name );
    $category_links[] = "<a href='$category_link'>$category_name</a>";
    }

    // Return the categories as a comma-separated list of clickable links
    return '<p class="posted_in">Categories: ' . implode( ', ', $category_links ) . '</p>';
    } else {
    // Return message if no categories are assigned
    return '<p class="posted_in">No categories assigned.</p>';
    }
    }
    }

    return ''; // Return nothing if not on a product page
    }
    add_shortcode( 'product_categories', 'display_product_categories_fixed' );
    // Create a shortcode to display categories of the current product
    function display_specific_product_categories_debug() {
    if ( is_product() ) {
    global $post;

    // Get the product object
    $product = wc_get_product( $post->ID );

    if ( $product ) {
    // Get categories for this specific product
    $categories = get_the_terms( $product->get_id(), 'product_cat' );

    // Debugging: Log the categories to the debug file
    error_log( 'Categories for Product ID ' . $product->get_id() . ': ' . print_r( $categories, true ) );

    if ( !empty( $categories ) && !is_wp_error( $categories ) ) {
    $category_links = array();

    // Generate clickable links for each category
    foreach ( $categories as $category ) {
    $category_links[] = '<a href="' . esc_url( get_term_link( $category->term_id ) ) . '">' . esc_html( $category->name ) . '</a>';
    }

    // Return the categories as a list of clickable links
    return '<p class="posted_in">Categories: ' . implode( ', ', $category_links ) . '</p>';
    } else {
    return '<p class="posted_in">No categories assigned.</p>';
    }
    }
    }

    return ''; // Return nothing if not on a product page
    }
    add_shortcode( 'product_categories_debug', 'display_specific_product_categories_debug' );

    As show above, I have a tried lots of code snippet, but all failed.

    Could you please show workable code snippet please?

    Thanks in advance.

    • This topic was modified 2 months ago by alexli126.
Viewing 3 replies - 1 through 3 (of 3 total)
  • Plugin Support Zafer – WPMU DEV Support

    (@wpmudevsupport15)

    Hi @alexli126,

    I hope you are doing well today!

    We are checking your request with our SLS (Second Line Support) team and will inform you accordingly.

    Thanks for the patience while we are looking into this.

    Kind regards,
    Zafer

    Plugin Support Zafer – WPMU DEV Support

    (@wpmudevsupport15)

    Hi again @alexli126,

    Our SLS team provided the following code snippet that you can use as a mu-plugin. After adding the plugin you can use [wpmudev_product_cat] shortcode to show the link of product category in the popup.

    <?php
    add_shortcode( 'wpmudev_product_cat', 'wpmudev_list_product_cat' );
    function wpmudev_list_product_cat() {
    $cat_link = '';
    if ( is_product() ) {
    $terms = get_the_terms( get_the_ID(), 'product_cat' );
    if ( ! empty( $terms ) ) {
    foreach ( $terms as $term ) {
    $cat_link .= '<a href="' . get_term_link( $term->term_id, 'product_cat' ) . '">' . $term->name . '</a>';
    }
    }
    }
    return $cat_link;
    }

    Please let us know if you need further help.

    Kind regards,
    Zafer

    Thread Starter alexli126

    (@alexli126)

    Great thanks so much fo support.

    Have a nice day.

Viewing 3 replies - 1 through 3 (of 3 total)
  • You must be logged in to reply to this topic.