3

I am using plugins called Advanced Custom Fields (ACF) and WooCommerce. I want to create a text and image field for WooCommerce product variation. I created fields in ACF and assigned them to "Post Type" > "product_variation".

enter image description here

But for I don't see these fields under product > Variations. I searched and it looks like I have to write a custom code to accommodate these fields. I tried searching the problem and most of the suggestions and tutorials I founds are about creating custom fields via code and not using ACF which is not helping me as the fields have to be using ACF and that is because I am using Visual Composer to pull these ACF fields on front end.

enter image description here

bad_coder
  • 11,289
  • 20
  • 44
  • 72
Syed Sajid
  • 1,380
  • 5
  • 20
  • 34
  • ideally you would look at the code yourself and see if there is a filter to work from, if not you might have to overwrite the template, unhook the action etc....start by searching the html to find the function that controls it. – David May 16 '16 at 23:56
  • @David Yeah definitely that's the plan, I will do it if I can't find much help here. I just don't want to re-invent the wheel. Perhaps it worth a little wait? – Syed Sajid May 17 '16 at 00:01
  • maybe, but id say its too specific. Nway, no cost in looking yourself, if you are going to use the plugin, you might as well get to know it :) If you had the code used, it would be easier to get help. – David May 17 '16 at 00:14
  • Have you tried : https://wordpress.org/plugins/wc-fields-factory/, it has an option to add custom fields on variations level – Sark May 17 '16 at 08:44
  • Ah WC-Fields-Factory isn't ACF unfortunately, I want to extend ACF. – Syed Sajid May 17 '16 at 19:13

3 Answers3

7

It's not acf but you just have to add some code to your "function.php" of your child theme under /public_html/wp-content/themes/yourtheme-child/function.php.

Please have a look to this tutorial http://www.remicorson.com/mastering-woocommerce-products-custom-fields/

For instance, in my code I've added 2 fields for the RRP and another one for personal use (Price per Pair):

        /* Adds RRP or PPP* price after each product price throughout the shop for user != Customer & Guest
    .Not displayed in cart as it's not per var and we don't need to.
    PPP = Price Per Pair (for product composite/bundle display)
    ================================================== */

// Display Fields
add_action( 'woocommerce_product_options_general_product_data', 'woo_add_custom_general_fields' );

// Save Fields
add_action( 'woocommerce_process_product_meta', 'woo_add_custom_general_fields_save' );

function woo_add_custom_general_fields() {

    global $woocommerce, $post;

    // Text Field creation
woocommerce_wp_text_input( 
    array( 
        'id'          => '_rrpprice', 
        'label'       => __( 'RRP price ($)', 'woocommerce' ), 
        'placeholder' => 'e.g. 499',
        'desc_tip'    => 'true',
        'description' => __( 'Enter the RRP .', 'woocommerce' )
    )
);
woocommerce_wp_text_input( 
    array(  
        'id'          => '_pppprice', 
        'label'       => __( 'Price Per Pair*', 'woocommerce' ),
        'placeholder' => 'e.g. 122',
        'desc_tip'    => 'true',
        'description' => __( 'Enter the PPP (Price Per Pair) if Bundle/composite .', 'woocommerce' ) 
    )
);  
}
function woo_add_custom_general_fields_save( $post_id ){

    // TextField save
    $woocommerce_rrpprice = $_POST['_rrpprice'];
    update_post_meta( $post_id, '_rrpprice', esc_html( $woocommerce_rrpprice ) );
    if( !empty( $woocommerce_rrpprice ) )

    // TextField save
    $woocommerce_pppprice = $_POST['_pppprice'];
    if( !empty( $woocommerce_pppprice ) )
    update_post_meta( $post_id, '_pppprice', esc_html( $woocommerce_pppprice ) );
}

// Display Custom Field Value

if ( is_user_logged_in() && !(current_user_can('customer'))) {
    function sv_change_product_price_display( $price ) {
        $product = wc_get_product( get_the_ID() );
        $priceRRP = get_post_meta( get_the_ID(), '_rrpprice', true );
        $pricePPP = get_post_meta( get_the_ID(), '_pppprice', true );
          if ( (is_shop() || is_product()) && !is_cart() ) { //double belt check
            if($product->is_type( 'variable' )){
                $price .= ' + GST<br /><span class="rrp-price">RRP: $' . $priceRRP .'</span>';
            }else{

                $price = ' <span class="rrp-price"><b>$' . $pricePPP .' + GST </b></span>' . '<br /><span class="rrp-price">RRP: $' . $priceRRP .'</span>';
            }
          }
        return $price;          
    }
    add_filter( 'woocommerce_get_price_html', 'sv_change_product_price_display' );
    add_filter( 'woocommerce_cart_item_price', 'sv_change_product_price_display' );
}

If you have any question please feel free to ask.

bkseen
  • 389
  • 1
  • 3
  • 19
  • This adds the field(s) to the Product level, under Product Data > General, not to variations. – Nathan Aug 16 '21 at 19:32
2

As far as I know the Advanced Custom Fields plugin only allows you to add custom fields for editors. These fields cannot be used to build the front-end except by putting hands in the code.

If you want to add custom fields for your products (so the customers can do some personalization), you have to use another plugin. I personally use WC Fields Factory, it let you define additional data for your products, like size, color, image, etc. Just use the new Field Factory menu.

Of course there should be other ones I haven't tried.

Yann39
  • 14,285
  • 11
  • 56
  • 84
  • Of course on the front end I will display the data. I just want to show the fields in backend editor (that is product variations) Well to be honest using only ACF is my client's requirement and nothing much I can do with it. I guess I have to write a code to display ACF Fields under products variations tab. Anyways thanks for the help so far – Syed Sajid May 17 '16 at 19:12
  • OK sorry for misunderstanding your question. I have tried it and I can't manage to display the fields in the product variation page too, it seems it is out of scope of this plugin. Maybe topics like http://stackoverflow.com/questions/30009220/woocommerce-add-custom-fields-to-product-variations or http://stackoverflow.com/questions/22420311/custom-field-in-woocommerce-variations may help you. – Yann39 May 18 '16 at 06:45
  • That's fine, thank you for the resource links. I already started writing code for this problem. I will post it as soon as I am done with it. I hope it will help someone else as well. Perhaps time to give back ;) – Syed Sajid May 18 '16 at 16:54
  • Syed, did you find a way to add advanced custom fields to WooCommerce product variations? If yes, could you please share the code? – Daniel Nov 24 '16 at 06:15
2

I faced this problem myself and given how frequently WooCommerce changes I was hesitant to shim some extra fields in with code. I did figure out a minimal code workaround which may or may not fit your needs. Essentially it's "proxying" the variation's custom fields in a regular post and accessing those fields by putting that post ID in the product variation's SKU field.

I started by setting up a post category "custom-fields" - which I hide via a redirect to the similarly named product. I set up my custom fields for the post category: custom-fields and then I added a post e.g. "widget x red" and I input the custom fields for the red variation. Once the post saved I added the post ID to the product variation SKU field. Customizing the CSS I hid the SKU field and editing the child theme woocommerce/single-product.php I added a little PHP to retrieve the custom fields.

$product = wc_get_product($post->ID);
$pv = new WC_Product_Variable($product->get_id());
$variations = $pv->get_available_variations();  
$custom_fields=[];
if(!empty($variations)){
    foreach($variations as $variation){
        $vid = $variation['variation_id'];
        $sku = $variation['sku'];
        if($vid && $sku){
            $fields = get_fields($sku);
            if($fields){
                $custom_fields[$vid] = $fields;
            }
        }
    }
}

After which I had the custom fields indexed by variation ID

Andy Gee
  • 3,149
  • 2
  • 29
  • 44