• Hi,
    I was wondering if the following was possible somehow..
    We wish to list ingredients in our WooCommerce store.
    I have set up a custom post type called “Ingredients” in WordPress (using: https://wordpress.org/plugins/custom-post-type-ui/) and have created some ingredients! I would like a tab on WooCommerce Products to say “Ingredients” and be able to have a tick-box system to tick which ingredients are in a product, on the ticked ones, I would like the featured image and title of said ingredient(s) to be in the tab on the front end, hyperlinked back to the ingredient itself. The idea is the tick boxes are to be pulled from the global list of all “post_type=ingredients”.

    Can anyone think of a way to do this?
    We’re looking for something similar to this: https://www.pevonia.co.uk/buy/index.php?route=product/product&path=238_58_234_66&product_id=302 (click on the “Main Ingredients” tab)

    Many Thanks

    • This topic was modified 5 years, 9 months ago by dsm1.
Viewing 6 replies - 1 through 6 (of 6 total)
  • Plugin Contributor James Koster

    (@jameskoster)

    Hi there,

    This essentially sounds like you’re replicating our Attributes feature. Any reason not to just use that? The benefit there is that folks will be able to sort your products by attribute using the layered navigation widget.

    You can read more about attributes here.

    You could then use a plugin like this to assign images to the attributes. You’ll probably need to do some additional customisation to get those images to appear on the frontend, but most of the leg-work will be done.

    As for editing the product tabs, take a look at this document which outlines how to do that.

    James
    Designer @ Automattic

    Thread Starter dsm1

    (@dsm1)

    Hi James,

    Thank you for your response, I did look at doing this with attributes, but we concluded that this isn’t best for us…

    It’s also not quite what we’re looking to do, we’re looking at having the post type of Ingredients to be it’s own type of area on the site, so it won’t purley be used for product tabs, the other use of having an Ingredient Library is working as we would expect, but we wish to create a product tab which has all of this post type listed as tick boxes to then check for it to appear in the front end with the images as expected.

    Additionally, we use atrtibuts for product sizes and colours, so I’d rather keep ingredients out of this, that said, if the attributes are browsable on the front end, then I can give this a go, although I couldn’t find in your attached documentation exactly how to remove “Additional Information” from displaying the other attribute types (size & colour), renaming it to “Ingredients” would do exactly what we would want but obviousy we don’t want size and colour to appear there! Unless there is a way to give each attribute its own tab?

    I’ll run some tests on the staging site and see how far I can get with it as-is and let you know.

    Kind Regards
    Dan

    Thread Starter dsm1

    (@dsm1)

    Hi @jameskoster,

    I’ve looked at doing this using WooCommerce Attributes and it’s too limited!
    I need a better way to add images into Attributes, is this not anything I can feature request for core?

    I’ll explain how far I got…
    I tried several plugins to add the image, whilst it adds, it is impossible to echo in the front end, as when referencing the attribute image, nothing returns, as this isn’t attribute related, considering I’m trying to return in a loop, this was never going to work I regret to say…

    This: https://wordpress.org/plugins/woo-product-attribute-tab/ works wonders in doing the tab display I wanted, I tried to use functions.php as you described, but I got that tiny bit further with this plugin, although, I did remove “Additional Information” with the functions snippets.

    I think being able to embed my custom post types into a tab, or at least attach those to attributes would help here, those custom post types allow me to insert a title, description & featured image, tying that in with Attributes would mean we can go to Attributes in a product then select “Ingredients” and pull the data from the “ingredients” post_type.

    Does this make sense?

    As I say, this is exactly what we’re looking for, found under “Main Ingredients” on this website: https://www.pevonia.co.uk/buy/index.php?route=product/product&path=238_58_234_66&product_id=302

    Kind Regards
    Dan

    Plugin Contributor James Koster

    (@jameskoster)

    Oh I see, you want the ingredients attached to a product to link to a section of content that describes that ingredient?

    I thought you wanted it to link to a filtered selection of products which contain that ingredient. That’s why I suggested attributes because that’s exactly what they do 🙂

    So in this case, you basically want to attach a post(s) from the ingredients post type to each of your products. And on the frontend those posts are revealed and linked to the full content.

    I’m afraid I can’t really think of any plugin that does this comprehensively. We have our tab manager extension which would allow you to quickly / easily create the custom tab that you need, but you’d need to insert the link to the relevant ingredients manually per product. That would work fine until you changed an ingredient name, or removed an ingredient.

    I think you’ll need to reach out to a developer to build this for you as a custom tab. Fortunately, I don’t think it would be a huge job at all. Take a look here for help with WooCommerce customizations.

    Thread Starter dsm1

    (@dsm1)

    Hi James,

    Thanks for getting back, yes, this is basically what we wish to do, the difficulty being getting the option to appear in the back end of the site for our marketing guys to easily chose ingredients,
    That said, I’ve played more with Attributes and I am able to get these to display in a tab of their own, provided we can get the description outputted and an image attached (and outputted) then this is all we need! I will then use an accordian for the description on the same page rather than mix with the post_types…
    Since opening this ticket, we have decided to have “Ingredients” listing all ingredients in a product and “Main Ingredients” listing the top 5 key ingredients, which implies that we will need to have differnt ways to store these ingredients, so using attributes would allow us to list these both in the same way.

    So far, my functions.php looks like this, although, I would like to output the ingredients individually without being in the same <p> tag:

    /**
     * Remove product data tabs
     */
    add_filter( 'woocommerce_product_tabs', 'woo_remove_product_tabs', 98 );
    
    function woo_remove_product_tabs( $tabs ) {
    
    //    unset( $tabs['description'] );          // Remove the description tab
    //    unset( $tabs['reviews'] );          // Remove the reviews tab
        unset( $tabs['additional_information'] );   // Remove the additional information tab
    
        return $tabs;
    }
    function woo_new_product_tab_content() {
    
        // The new tab content
    
        echo '<h2>List of Ingredients in this product</h2>';
        echo '<p>Here\'s your new product tab.</p>';
        global $product;
    //     $ingredient = $product->get_attribute('pa_ingredients');
    //     $product = wc_get_product( $product_id );
    $ingredients_list = $product->get_attribute( 'ingredients' );
     //   print $ingredients_list;
    
    $meta_image = get_wp_term_image($term_id); 
    
    $has_row    = false;
    $attributes = $product->get_attributes();
     
    ob_start();
     
    ?>
    <div class="product_attributes">
     
        <?php foreach ( $attributes as $attribute ) :
     
            if ( empty( $attribute['is_visible'] ) || ( $attribute['is_taxonomy'] && ! taxonomy_exists( $attribute['name'] ) ) ) 
                continue;
     
            $values = wc_get_product_terms( $product->get_id(), $attribute['name'], array( 'fields' => 'names' ) );
            $att_val = apply_filters( 'woocommerce_attribute', wpautop( wptexturize( implode( '<br>', $values ) ) ), $attribute, $values );
     
            if( empty( $att_val ) )
                continue;
     
            $has_row = true;
            ?>
     
        <div class="col">
            <div class="att_label"><?php //echo wc_attribute_label( $attribute['name'] ); ?></div>
            <div class="att_value"><?php $meta_image = get_wp_term_image($term->term_id); ?><?php echo $att_val; ?></div><!-- .att_value -->
        </div><!-- .col -->
     
        <?php endforeach; ?>
     
    </div><!-- .product_attributes -->
    <?php
    if ( $has_row ) {
        echo ob_get_clean();
    } else {
        ob_end_clean();
    }
    }
    Thread Starter dsm1

    (@dsm1)

    Hi James,

    I’m drilling this down a bit, would be good to get something here on this topic for others to use later,

    This line is creating issues:
    echo apply_filters( 'woocommerce_attribute', wpautop( wptexturize( implode( ', ', $values ) ) ), $attribute, $values );

    I get the attributes (in my case, Ingredients) out of the site, all comma seperated in the <p>Water, Flour</p> – like that…

    I would like to get a loop like this (note, the below code isn’t real to my knowledge):

    loop
    <li><img src="$attribute_img_url;"><caption>$attribute;</caption><p>$attribute_desc;</p></li>
    end_loop

    I am then adding this into a tab by using functions.php.

    Thanks for your assistance so far James.

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Is it possible…’ is closed to new replies.