Support

Account

Home Forums Search Search Results for 'woocommerce'

Search Results for 'woocommerce'

reply

  • Hi,

    This piece of code may help you. To use it, add a location to your field group where Taxonomy equals your attribute name. Please note, this only work with “basic” fields, ie. not relying on javascript (gallery or image buttons do not work).

    $GLOBALS['wc_loop_variation_id'] = null;
    
    function is_field_group_for_variation($field_group, $variation_data, $variation_post) {
    	return (preg_match( '/Variation/i', $field_group['title'] ) == true);
    }
    
    add_action( 'woocommerce_product_after_variable_attributes', function( $loop_index, $variation_data, $variation_post ) {
    	$GLOBALS['wc_loop_variation_id'] = $variation_post->ID;
    
    	foreach ( acf_get_field_groups() as $field_group ) {
    		if ( is_field_group_for_variation( $field_group, $variation_data, $variation_post ) ) {
    			acf_render_fields( $variation_post->ID, acf_get_fields( $field_group ) );
    		}
    	}
    
    	$GLOBALS['wc_loop_variation_id'] = null;
    }, 10, 3 );
    
    add_action( 'woocommerce_save_product_variation', function( $variation_id, $loop_index ) {
    	if ( !isset( $_POST['acf_variation'][$variation_id] ) ) {
    		return;
    	}
    
    	$_POST['acf'] = $_POST['acf_variation'][$variation_id];
    
    	acf()->input->save_post( $variation_id );
    }, 10, 2 );
    
    add_filter( 'acf/prepare_field', function ( $field ) {
    	if ( !$GLOBALS['wc_loop_variation_id'] ) {
    		return $field;
    	}
    
    	$field['name'] = preg_replace( '/^acf\[/', 'acf_variation[' . $GLOBALS['wc_loop_variation_id'] . '][', $field['name'] );
    
    	return $field;
    }, 10, 1);
  • Okay I have a pretty crispy workaround, but it works.
    I am using ACF Pro 5.7.11

    In the old thread find the first reply by shaun where he has a code block. Here’s the link:
    https://support.advancedcustomfields.com/forums/topic/custom-fields-on-woocommerce-product-variations/

    Use that code in functions.php or wherever.

    In ACF setup whatever custom fields you need, but in the rule select product instead of product_variation because that option isn’t present.

    Go to the ACF Tools page and export the .json of your custom fields. Open that file with a text editor and scroll down to the “locations” section. Simply change the “value” from “product” to “product_variation” and then on that same tools page re-upload the file. You should see custom fields in your product variations drop downs now on variable products.

  • PHP version is NOT the issue on my site. I am running php 7.2, and the update crashes my admin.

    Relevant Woocommerce Status report:

  • Update
    I was able to get the categories to appear by using this code, but I would still like to find a way to group these categories together based on what ACF field (from the Select Field option) was chosen. With this code, it is appearing as:

    ACF Field A
    Woocommerce category name A

    ACF Field B
    Woocommerce cateogry name B

    ACF Field A
    Woocommerce category name C

    Where I would prefer it look like this:

    ACF Field A
    Woocommerce category name A
    Woocommerce category name C

    ACF Field B
    Woocommerce category name B

    
    <?php 
    
      $args = array(
        'taxonomy' => 'product_cat',
        'hide_empty' => 0
      );
      $c = get_categories($args);
      $c_keep = array();
      foreach($c as  $cat){
        if (get_field('associated_collection', 'category_'.$cat->term_id)) {
          $c_keep[] = $cat; // forgot [] here
        }
      }
    
      foreach($c_keep as $cat){
        echo '<h1>'.get_field('associated_collection', 'category_'.$cat->term_id).'</h1>';
        echo '<p>'.$cat->name.'</p>';
      }
    
    ?>
    

    Any ideas on what I’m missing?

  • WooCommerce Memberships is to blame for this. They have a function that removes ALL metaboxes from any other plugins.

    The issue only arose recently because ACF used to force metaboxes to appear, but now is more compliant with best practices.

    To fix this problem, SkyVerge (WooCommerce Memberships developers) have provided the following code snippet: https://github.com/skyverge/wc-plugins-snippets/blob/master/woocommerce-memberships/admin/acf-compat-add-field-groups.php

    It can be put in your functions.php file or implemented using a free plugin called “Code Snippets”. The function basically allows ACF metabozes/field groups to appear on WooCommerce Membership related post types (e.g. single user memberships and/or membership plans).

  • I am trying to add a new field in the checkout page but I am having an error. I am having this line as error

    if (!$_POST[‘customised_field_name’]) wc_add_notice(__(‘Please enter value.’) , ‘error’);

    I have completely checked the code but still, I am having an error. I have followed the WooCommerce checkout page tutorial and implement the same as discussed in the article. Can you help me to resolve this issue?

  • +1. If @elliot is concerned about security of storing one’s license key in the filesystem, here are some other ideas:

    Idea 1: Check site authorization remotely–no license key required after the first time. I think this would probably go hand-in-hand with letting folks authorize a site from advancedcustomfields.com (not just de-authorize). WPML does it that way. This also seems to be how Admin Columns Pro does it, since I never need to re-authorize that one after copying from one site to another, as long as I’ve authorized it once on that domain. If I remember correctly, I think WooCommerce extensions (and maybe WC itself) also takes that approach.

    Idea 2: Allow us to generate & hard-code site-specific auth tokens instead of the master license key. I don’t know what security issues @elliot is concerned about specifically (maybe that people’s license keys will start showing up on Github?) so maybe this is worthless, but if it helps get ideas going, perhaps there could be something similar to ACF’s current method of combining the license key and domain together, but instead of base64 it uses some kind of hashing algorithm. (On the other hand, perhaps doing that securely would entail the same logic/development required by idea 1 without any additional benefit?)

  • We managed to fix it using a different approach.  

    We re-added the group and fields in the ACF setup, being careful to name the fields the same as what they were named before. The fields then reappeared in the Woocommerce Product records, and the data reappeared in those records that previously had data in the fields.

    Not sure what made the field definitions disappear in the first place, but I am suspicious of the latest plugin upgrade.  

  • I have the same problem. I am using ACF to add a couple of custom fields to our Woocommerce product records. As of last week, we can no longer see the custom fields. They are still in the database, but are not accessible to edit.

    Any help on how to restore their view would be greatly appreciated, as this is causing a real problem with website maintenance.

  • How would I do this if I had to do it.

    I would create a true false field “Add New Member”

    Then I would create 3 fields Name, Slug and Description. These 3 fields would be conditional and only show if Add New Member is true, these fields would all be required.

    Then I would create an acf/save_post filter https://www.advancedcustomfields.com/resources/acf-save_post/. In this filter I would

    1. see if the add new member field was set
    2. if it is I would get the values from the other 3 fields
    3. I would use these values to create a new term https://codex.wordpress.org/Function_Reference/wp_insert_term
    4. Then I would add this term to the post https://codex.wordpress.org/Function_Reference/wp_set_post_terms
    5. And finally I would delete the values from the 3 ACF fields and update the add new member field to false

    But to be honest, I would not probably do this unless I had a client that insisted on it. I would tell them that before they add a new post for a member that does not exist that first they must add the member. Or I would tell them that they will need to go to the member when they are done with adding the post and add a description for the member after the fact. This is no different than adding a category, or (product category in woocommerce), the only thing that you can do is add the name and then you need to go to that category and add a description and other information if the site needs it later.

  • Hi,

    I am trying to update a custom field ( in the woocommerce order page). Once the field is saved, I then change the status email to complete and send the number into the order completion email. using this code:

    // define the woocommerce_email_before_order_table callback 
    function action_woocommerce_email_before_order_table( $order, $sent_to_admin, $plain_text, $email ) { 
        
        
        if (get_field('ACFFIELDNAME', $order->get_id())) { // Only show if field is filled
     
    ?>
            <h3>Your number: <?php the_field('ACFFIELDNAME', $order->get_id()); ?></h3>
                  
            <?php 
            
        }
    
    }; 
             
    
    // add the action 
    add_action('woocommerce_email_before_order_table', 'action_woocommerce_email_before_order_table', 10, 4 );
    
    
  • Hi,

    I created a custom field to add manually the tracking number in the woocommerce order page.

    The problem is that in order to work, I have to fill in the field, save the page, then I have to change the order to complete and resave the page.

    It’s like if the information entered in the field was not saved. Is there a way I can add a save button under my custom field? This could fix the issue.

    The information entered would be registered in the wordpress DB and then I could only switch the order status to complete, in order to send the tracking number to the client.

    Here is my code

    // define the woocommerce_email_before_order_table callback 
    function action_woocommerce_email_before_order_table( $order, $sent_to_admin, $plain_text, $email ) { 
        
        
        if (get_field('trackingnumberups', $order->get_id())) { // Only show if field is filled
     
    ?>
            <h3>Your tracking number: <?php the_field('trackingnumberups', $order->get_id()); ?></h3>
            You may track your order with the number above, here: <a style="color: #0073aa;" href="https://www.ups.com/tracking/tracking.html">UPS Tracking</a> 
             
            <?php 
            
        }
    
    }; 
             
    // add the action 
    add_action('woocommerce_email_before_order_table', 'action_woocommerce_email_before_order_table', 10, 4 ); 
  • Try to use this plugin https://wordpress.org/plugins/acf-yith-woocommerce-compare-support/
    Sorry, I know that it is a little bit late reply, but maybe it will be useful for somebody else.

  • $product_id has no value in your code

    
    function cw_change_product_price_display( $price, $product ) {
      $sell_price = get_field('sell_price', $product->ID); 
      $sell_duration = get_field('duration', $product->ID);
      $price .= " | SELL ₦$sell_price in $sell_duration";
      return $price;
    }
    add_filter( 'woocommerce_get_price_html', 'cw_change_product_price_display' 10, 2);
    add_filter( 'woocommerce_cart_item_price', 'cw_change_product_price_display' 10, 2);
    
  • Hi zaehlpixel,

    I use the same code than you for my woocommerce store.

    The problem is that I have to add the DHL tracking code, hit save, then mark the order as complete and save again.

    If I just change the status to order complete, add the tracking number, hit save, it won’t include the tracking number in the order completion email.

    Have you experienced the same issue?

  • @hube2
    Solved with your help, today I did the function I needed! Thanks!
    I write a piece, maybe someone can help.
    The function takes the title of a post-type related to products (woocommerce) and inserts this title as a woocommerce attribute when save_post action !

    
    add_action('acf/save_post', 'update_post_meta', 20); 
    	
    function update_post_meta( $post_id ) { 
    		
    $post_type = get_post_type($post_id);
    		
    global $product; 
    if ( 'product' == $post_type ) {
    					
    //TAKE RELATED INFO IN POST TYPE 
    $posts = get_field('serie_di_appartenenza');
    if( $posts ): 
    foreach( $posts as $p ):
    $scrittore=get_field("scrittore_appartenenza", $p->ID); 
    if( $posts ): 
    foreach( $scrittore as $s ): 
    $scrittore_ereditato= get_the_title( $s->ID );
    	
    //ADD INFO IN GLOBAL ATTRIBUTE WOOCOMMERCE				
    $term_taxonomy_ids = wp_set_object_terms( $post_id, $scrittore_ereditato, 'pa_scrittore-woo', true );
    					
    endforeach;
    endif; 
    endforeach;
    endif; 
    
    //ADD INFO IN CURRENT POST
    
    $data = Array(
    
    'pa_scrittore-woo'=>Array( 
    'name'=>'pa_scrittore-woo', 
    'value'=>$scrittore_ereditato,
    'is_visible' => '1',
    'is_variation' => '1',
    'is_taxonomy' => '1'
    ),
    );
    
    //UPDATE META					
    update_post_meta( $post_id, '_product_attributes',$data);
    					
    }
    }		
    
    
  • Thanks @dangercode. I wanted the exact same thing. I used the docs and cheated by looking at your version. Here’s mine, a little shorter mostly bc of the closure.

    add_filter( 'acf/location/rule_values/page_type', function ( $choices ) {
        $choices['woo_shop_page'] = 'WooCommerce Shop Page';
        return $choices;
    });
    
    add_filter( 'acf/location/rule_match/page_type', function ( $match, $rule, $options ) {
        if ( $rule['value'] == 'woo_shop_page' )
        {
            if ( $rule['operator'] == '==' )
                $match = ( $options['post_id'] == wc_get_page_id( 'shop' ) );
            if ( $rule['operator'] == '!=' )
                $match = ( $options['post_id'] != wc_get_page_id( 'shop' ) );
        }
        return $match;
    }, 10, 3 );
  • A little update on this.

    I’m writing a plugin that interfaces with WooCommerce. In the plugin I’m using ACF to attach custom repeater fields to a user which is then associated with a product. Anyhow, in the following code, get_students_1 returns the wrong list of students and get_students_2 returns the correct list. The two functions are identical.

    public function woocommerce_display_student_in_cart( $item_data, $cart_item ) 
    	{
    $this->get_students_1();
    $this->get_students_2();
    
    ...
    }
    	private function get_students_1()
    	{
    		print '<BR>---------STUDENTS 1--------<BR>';
    		
    		$user = 'user_'.$this->current_user->ID;
    		
    		
    		if( have_rows('student-list', $user) )
    		{
    			print "user has students<BR>";
    			
    			while( have_rows('student-list',$user ) ): the_row();
    
    				$currentName =  get_sub_field('student_name');
    		
    				print "** student $currentName <BR>";
    
    			endwhile;	
    		} 
    		else 
    		{
    			print "user has no students<BR>";
    		}
    	}
    	
    	private function get_students_2()
    	{
    		print '<BR>---------STUDENTS 2--------<BR>';
    		
    		$user = 'user_'.$this->current_user->ID;
    		
    		if( have_rows('student-list', $user) )
    		{
    			print "user has students<BR>";
    			
    			while( have_rows('student-list',$user ) ): the_row();
    	
    				$currentName =  get_sub_field('student_name');
    		
    				print "** student $currentName<BR>";
    	
    			endwhile;	
    			 
    		}
    		else 
    		{
    			print "user has no students<BR>";
    		}
    	}

    In fact, if I just call $this->get_students_1() twice, the first time the list is wrong and the second time it is correct.

    This is seriously confusing. Any ideas?

  • I already wrote earlier. I put a clean version of WordPress on a test site. I installed the Storefront theme. Installed plugins – Woocommerce, ACF and ACF for WooCommerce. Created custom fields and placed them in the my-account of the user WooCommerce.

    I do not have third-party plugins on the test server, which do not work well.

    The only question is the plugin “ACF for WooCommerce”, which saves to the database “field_5ba168bfdcb58”. Why can not this plugin save “user_phone”?

  • There is a plugin “Loushou: ACF for WooCommerce”, which is not updated for two years. Here, the database correctly stores the fields as ‘user_phone’ and ‘user_age’.

    Can problems with the plugin “ACF for WooCommerce”? The database also has the string “acf_user_settings a: 1: {s: 15:” show_field_keys “; s: 1:” 1 “;}”

  • It seems that there are no such filters. I put a clean version of WordPress on a test site. Installed plugins – Woocommerce, ACF and ACF for WooCommerce. Created custom fields and placed them in the my-account of the user WooCommerce.

    In the database, the fields are still stored as field_5ba168bfdcb58 (user_phone) and field_5ba16910dcb59 (user_age). Therefore, I no longer know what to do.

  • As it turned out, this plugin “ACF for Woocommerce” saves the field data as meta_key: “field_5b7e4f388fd11” and meta_value: “+79998006655”. Tell me how to correctly display the fields in my code?

    get_field( 'field_5b7e4f388fd11', "user_{$user_id}" )

    This for some reason does not work. Emails come empty fields ((

  • Thanks again for your help on this, John. I didn’t get it working in the end and instead reverted to using WooCommerce hooks to append fields to the registration form.

    I’ve not ruled out revisiting your proposed solution at a later date though as I’m using an ACF form on the user’s account page and would ideally like to be using the same one on the register form, rather than maintaining two sets of fields. If I do eventually get it to work I’ll update this thread with my results.

  • If not use the “ACF for WooCommerce” plugin, then how do put custom fields in my account page of WooCommerce?

  • Whoa – after more testing, I found the validation error only happens if I try to change the parent page. I can edit other fields on the page and it validates as I expect it to.

    I also found the same issue on another site on a WooCommerce product. The validation error happens on a required, but hidden and empty WYSIWYG field, and only when changing the Product Category. I can make changes to other fields and the product validates, even with the hidden, empty, required field.

Viewing 25 results - 626 through 650 (of 889 total)