Support » Plugin: WooCommerce » [Plugin: WooCommerce – excelling eCommerce] Custom Fields with comma-separated values in WordPress a

  • daledude


    Firstly, just to get it out of the way, I’m an amateur in PHP.

    I’m trying to get WooCommerce, using the WooCommerce Product CSV Import Suite, to use my CSV column “meta:additional_colors” as a comma-separated custom field and output its values matched with the appropriate thumbnail.

    So in the spreadsheet, under the column “meta:additional_colors”, it’s listed like “Providence Bisque, Providence Fog, Providence Cream, Providence Platinum”, etc. When the CSV Import Suite imports it, it becomes a single custom field called “additional_colors” with those comma-separated values.

    Custom Fields screenshot

    I want to then take this custom field, and insert it among the product thumbnail code found in the WooCommerce template product-thumbnails.php. I’m doing this because they don’t currently have a way to import the product thumbnail metadata (alt/title attributes) in the CSV Import Suite, and we need to be able to give the thumbnails specific names instead of the default Providence 1, Providence 2, etc.

    Here is the contents of my product-thumbnails.php page so far, there are two places I attempted to get additional_colors to work:

    <div class="thumbnails"><h2>Additional Colors</h2><ul class="additional_colors"><?php
        $attachments = get_posts( array(
            'post_type'     => 'attachment',
            'numberposts'   => -1,
            'post_status'   => null,
            'post_parent'   => $post->ID,
            'post__not_in'  => array( get_post_thumbnail_id() ),
            'post_mime_type'=> 'image',
            'orderby'       => 'menu_order',
            'order'         => 'ASC'
        ) );
        if ($attachments) {
            $loop = 0;
            $columns = apply_filters( 'woocommerce_product_thumbnails_columns', 3 );
            $additional_colors = explode(',',get_post_meta($post->ID, 'additional_colors', true));
            foreach ( $attachments as $key => $attachment ) {
                if ( get_post_meta( $attachment->ID, '_woocommerce_exclude_image', true ) == 1 )
                $classes = array( 'zoom' );
        if ( $loop == 0 || $loop % $columns == 0 )
        $classes[] = 'first';
    if ( ( $loop + 1 ) % $columns == 0 )
    $classes[] = 'last';
    printf( '<li><a href="%s" title="%s" rel="thumbnails" class="%s">%s</a> <a href="%s" title="%s" rel="thumbnails" class="%s">%s</a></li>', wp_get_attachment_url( $attachment->ID ), esc_attr( $attachment->post_title ), implode(' ', $classes), wp_get_attachment_image( $attachment->ID, apply_filters( 'single_product_small_thumbnail_size', 'shop_thumbnail' ) ), wp_get_attachment_url( $attachment->ID ), esc_attr( $attachment->post_title ), implode(' ', $classes), explode(',',$additional_colors) // I added this additional_colors part  );

    This is what the resulting page looks like:

    What the thumbnail arrays look like so far

    This is as far as I can get. I did find this bit of code, which I plugged my keys into, which seems to do what I’m trying to do, but I can’t figure out how to integrate it in with the WooCommerce code above:

    $additional_colors = explode(',',get_post_meta($post->ID, 'additional_colors', true));
         if($additional_colors) {
             foreach ( $additional_colors as $additional_color ) { ?>
        <p><a href="<?php echo wp_get_attachment_url( $attachment->ID ); ?>"><?php echo $additional_color; ?></a></p>
        <?php }

    Can anyone help me figure it out? I’ve tried to understand how arrays work, but i can’t seem to wrap my head around it. Thanks!

  • The topic ‘[Plugin: WooCommerce – excelling eCommerce] Custom Fields with comma-separated values in WordPress a’ is closed to new replies.