Viewing 12 posts - 1 through 12 (of 12 total)
  • Author
    Posts
  • #1467863

    Hi,
    is it possible with dynamic WP custom fields to display a product sub category of a certain main category ID within a text field?
    Thanks, Stefan

    #1467908

    Hey westefan,

    Thank you for the inquiry.

    What do you mean by “dynamic WP custom fields”? If you want to display the subcategories of a specific parent category, you can try this shortcode in the functions.php file:

    function av_display_product_subcategories_shortcode( $atts ) {
        $atts = shortcode_atts( array(
            'category_id' => '',
        ), $atts, 'display_subcategories' );
        
        $category_id = intval( $atts['category_id'] );
        
        if ( !$category_id ) {
            return 'Please provide a valid main category ID.';
        }
        
        $args = array(
            'taxonomy'     => 'product_cat',
            'parent'       => $category_id,
            'hide_empty'   => false,
        );
        
        $subcategories = get_terms( $args );
        
        if ( empty( $subcategories ) || is_wp_error( $subcategories ) ) {
            return 'No subcategories found for this category.';
        }
        
        $output = '<ul>';
        
        foreach ( $subcategories as $subcategory ) {
            $output .= '<li>' . esc_html( $subcategory->name ) . '</li>';
        }
        
        $output .= '</ul>';
        
        return $output;
    }
    
    add_shortcode( 'av_display_subcategories', 'av_display_product_subcategories_shortcode' );
    
    

    You can use the shortcode in a Text or Code Block element.

    [av_display_subcategories category_id="123"]
    

    Best regards,
    Ismael

    #1467921

    Hey Ismael,

    wow, many thanks, it works, but I wasn’t exact enough. I meant only subcategories a certain product is assigned to. If a certain product category has subcategories A, B and C and a product is assigned to A and C then only A and C should be displayed. Possible?

    By activating custom elements in Enfold settings it’s possible to insert WP Post Data or ACF fields. And there are also Dynamic WP Custom Fields like Theme Custom Field Shortcodes ({av_dynamic_el src=”wp_custom_field” key=”%metakey%” default=”” link=”” linktext=”” format=””}).And so I thought I can use this. But how?

    Thanks again
    Stefan

    #1468078

    Hi,

    If you want to show subcategories for which a specific product belongs, try replacing the shortcode with this:

    function av_display_product_subcategories_shortcode( $atts ) {
        if ( ! is_product() ) {
            return 'This shortcode can only be used on product pages.';
        }
    
        global $product;
    
        $terms = wp_get_post_terms( $product->get_id(), 'product_cat' );
    
        if ( empty( $terms ) || is_wp_error( $terms ) ) {
            return 'No categories found for this product.';
        }
    
        $category = $terms[0];
    
        $args = array(
            'taxonomy'     => 'product_cat',
            'parent'       => $category->term_id,
            'hide_empty'   => false,
        );
    
        $subcategories = get_terms( $args );
    
        if ( empty( $subcategories ) || is_wp_error( $subcategories ) ) {
            return 'No subcategories found for this category.';
        }
    
        $output = '<ul>';
    
        foreach ( $subcategories as $subcategory ) {
            $output .= '<li>' . esc_html( $subcategory->name ) . '</li>';
        }
    
        $output .= '</ul>';
    
        return $output;
    }
    
    add_shortcode( 'av_display_subcategories', 'av_display_product_subcategories_shortcode' );
    

    You can then use the shortcode in a product like so:

    [av_display_subcategories]
    

    Unfortunately, you cannot use the dynamic content for this.

    Best regards,
    Ismael

    #1468087

    Hi Ismael,

    as I’m not expecting code snippets as part of the support I’m very appreciating your help! When using the shortcode I see “This shortcode can only be used on product pages” in the element preview, but after saving and looking at frontend there is “No subcategories found for this category” – no matter if I use [av_display_subcategories] or [av_display_subcategories category_id=”42″] (the last one with the main category ID is my wish). Can you help again?

    Many thanks
    Stefan

    #1468137

    Hi,

    I meant only subcategories a certain product is assigned to

    The latest modification can only be used on product pages as per your request above. Where are you trying to use the shortcode?

    Best regards,
    Ismael

    #1468149

    Hi,
    of course on product page. My fault was that I have assigned products to subcategories but not to the belonging main categories. So this is corrected now. But: I have 3 main categories with IDs 42, 71 and 120. And the shortcode outputs ALL subcategories of main ID 120 (which is on top of admin product category list).
    So my ideal case is to use [av_display_subcategories category_id=”xxx”] to show only assigned subcategories of each product.
    Thanks again for your help!
    Stefan

    #1468238

    Hi,

    Thank you for the clarification.

    We edited the shortcode a bit and added the parent_category parameter:

    function av_display_product_subcategories_shortcode( $atts ) {
        $atts = shortcode_atts( array(
            'parent_category' => '',
        ), $atts, 'av_display_subcategories' );
    
        if ( ! is_product() ) {
            return 'This shortcode can only be used on product pages.';
        }
    
        global $product;
    
        if ( ! empty( $atts['parent_category'] ) ) {
            $category = get_term_by( 'slug', sanitize_title( $atts['parent_category'] ), 'product_cat' );
            if ( ! $category ) {
                $category = get_term( (int) $atts['parent_category'], 'product_cat' );
            }
        } else {
            $terms = wp_get_post_terms( $product->get_id(), 'product_cat' );
            if ( empty( $terms ) || is_wp_error( $terms ) ) {
                return 'No categories found for this product.';
            }
            $category = $terms[0];
        }
    
        if ( ! $category || is_wp_error( $category ) ) {
            return 'No valid parent category found.';
        }
    
        $args = array(
            'taxonomy'     => 'product_cat',
            'parent'       => $category->term_id,
            'hide_empty'   => false,
        );
    
        $subcategories = get_terms( $args );
    
        if ( empty( $subcategories ) || is_wp_error( $subcategories ) ) {
            return 'No subcategories found for this category.';
        }
    
        $output = '<ul>';
    
        foreach ( $subcategories as $subcategory ) {
            $output .= '<li>' . esc_html( $subcategory->name ) . '</li>';
        }
    
        $output .= '</ul>';
    
        return $output;
    }
    
    add_shortcode( 'av_display_subcategories', 'av_display_product_subcategories_shortcode' );
    
    

    You can use the shortcode like so:

    [av_display_subcategories parent_category="123"]
    

    or

    [av_display_subcategories parent_category="parent-category-slug"]
    

    Best regards,
    Ismael

    #1468255

    Hi Ismael,

    many thanks again! The limitation to a certain main category ID works now, but the output is a list of all existing subcategories and not only these a product is assigned to.

    Here https://chiway.dadanddaughter.design/wp-content/uploads/Screenshot-Abdominale-Akupunktur-CHIWAY.png.you can see under “Kursleitung” (main category) all subcategories (Simon Becker, Pia Columberg, …), but the product has only one instructor resp. subcategory.

    Can you please check the code?

    Best regards, Stefan

    #1468324

    Hi,

    Thank you for the info.

    We updated the code again and added a filter so that only subcategories where the product belongs are included.

    function av_display_product_subcategories_shortcode( $atts ) {
        $atts = shortcode_atts( array(
            'parent_category' => '',
        ), $atts, 'av_display_subcategories' );
    
        if ( ! is_product() ) {
            return 'This shortcode can only be used on product pages.';
        }
    
        global $product;
    
        if ( ! empty( $atts['parent_category'] ) ) {
            $parent_category = get_term_by( 'slug', sanitize_title( $atts['parent_category'] ), 'product_cat' );
            if ( ! $parent_category ) {
                $parent_category = get_term( (int) $atts['parent_category'], 'product_cat' );
            }
        }
    
        if ( ! $parent_category || is_wp_error( $parent_category ) ) {
            return 'No valid parent category found.';
        }
    
        $product_terms = get_the_terms( $product->get_id(), 'product_cat' );
    
        if ( empty( $product_terms ) || is_wp_error( $product_terms ) ) {
            return 'No categories found for this product.';
        }
    
        $filtered_terms = [];
        foreach ( $product_terms as $term ) {
            if ( $term->parent == $parent_category->term_id ) {
                $filtered_terms[] = $term;
            }
        }
    
        if ( empty( $filtered_terms ) ) {
            return 'No subcategories found for this product under the specified parent category.';
        }
    
        $output = '<ul>';
        foreach ( $filtered_terms as $subcategory ) {
            $output .= '
     	<li>' . esc_html( $subcategory->name ) . '</li>';
        }
        $output .= '</ul>';
    
        return $output;
    }
    
    add_shortcode( 'av_display_subcategories', 'av_display_product_subcategories_shortcode' );
    

    Best regards,
    Ismael

    #1468336

    Wow, I’m so grateful – it works perfect!
    Thanks a lot, Stefan

    #1468395

    Hi,

    Great! Glad to know that the solution is working. Please feel free to open another thread if you have more questions about the theme.

    Have a nice day.

    Best regards,
    Ismael

Viewing 12 posts - 1 through 12 (of 12 total)
  • The topic ‘Dynamic WP Custom Fields’ is closed to new replies.