Support

Account

Forum Replies Created

  • Nobody?

    I use the WordPress Cusomizer and I’m using a shortcode to collect te data.

    [yl_product_relations]

  • I removed ->ID and that was a step in the right direction. I now do see the relations but STILL ONLY from the LATEST PRODUCT (see screeny 2).

    Current view

  • I was missing this line:

    $exception_date = getDate($exception_dates);

    As it turned out, the exceptions weren’t real dates after all.

  • Close this topic…. irrelevant after investigation.

  • Thanks for explaining John,

    I thought it had to be something like that…

    I am going for a different approach then.

  • 
    add_filter( 'woocommerce_product_get_price', 'yl_get_dish_price', 20, 2 );
    add_filter( 'woocommerce_product_get_regular_price', 'yl_get_dish_price', 20, 2 );
    function yl_get_dish_price( $price, $product ) {
        if ( $product->is_type('dish') ) {
            $price = get_field( 'price', $product->get_id() );
        }
        return $price;
    }
    
  • The following code adds the CPT (dish in my case) to the WooCommerce cart but the price is incorrect. It always displays โ‚ฌ 10,00

    
    class YL_Dish_Product extends WC_Product  {
    
        protected $post_type = 'dish';
    
        public function get_type() {
            return 'dish';
        }
    
        public function __construct( $product = 0 ) {
            $this->supports[]   = 'ajax_add_to_cart';
    
            parent::__construct( $product );
    
        }
        // maybe overwrite other functions from WC_Product
    
    }
    
    class YL_Data_Store_CPT extends WC_Product_Data_Store_CPT {
    
        public function read( &$product ) { // this is required
            $product->set_defaults();
            $post_object = get_post( $product->get_id() );
    
            if ( ! $product->get_id() || ! $post_object || 'dish' !== $post_object->post_type ) {
    
                throw new Exception( __( 'Invalid product.', 'woocommerce' ) );
            }
    
            $product->set_props(
                array(
                    'name'              => $post_object->post_title,
                    'slug'              => $post_object->post_name,
                    'date_created'      => 0 < $post_object->post_date_gmt ? wc_string_to_timestamp( $post_object->post_date_gmt ) : null,
                    'date_modified'     => 0 < $post_object->post_modified_gmt ? wc_string_to_timestamp( $post_object->post_modified_gmt ) : null,
                    'status'            => $post_object->post_status,
                    'description'       => $post_object->post_content,
                    'short_description' => $post_object->post_excerpt,
                    'parent_id'         => $post_object->post_parent,
                    'menu_order'        => $post_object->menu_order,
                    'reviews_allowed'   => 'open' === $post_object->comment_status,
                )
            );
    
            $this->read_attributes( $product );
            $this->read_downloads( $product );
            $this->read_visibility( $product );
            $this->read_product_data( $product );
            $this->read_extra_data( $product );
            $product->set_object_read( true );
        }
    
        // maybe overwrite other functions from WC_Product_Data_Store_CPT
    
    }
    
    class YL_WC_Order_Item_Product extends WC_Order_Item_Product {
        public function set_product_id( $value ) {
            if ( $value > 0 && 'dish' !== get_post_type( absint( $value ) ) ) {
                $this->error( 'order_item_product_invalid_product_id', __( 'Invalid product ID', 'woocommerce' ) );
            }
            $this->set_prop( 'product_id', absint( $value ) );
        }
    
    }
    
    function YL_woocommerce_data_stores( $stores ) {
        // the search is made for product-$post_type so note the required 'product-' in key name
        $stores['product-dish'] = 'YL_Data_Store_CPT';
        return $stores;
    }
    add_filter( 'woocommerce_data_stores', 'YL_woocommerce_data_stores' , 11, 1 );
    
    function YL_woo_product_class( $class_name ,  $product_type ,  $product_id ) {
        if ($product_type == 'dish')
            $class_name = 'YL_Dish_Product';
        return $class_name; 
    }
    add_filter('woocommerce_product_class','YL_woo_product_class',25,3 );
    
    function my_woocommerce_product_get_price( $price, $product ) {
    
        if ($product->get_type() == 'dish' ) {
            $price = 10;  // or get price how ever you see fit     
        }
        return $price;
    }
    add_filter('woocommerce_get_price','my_woocommerce_product_get_price',20,2);
    add_filter('woocommerce_product_get_price', 'my_woocommerce_product_get_price', 10, 2 );
    
    // required function for allowing posty_type to be added; maybe not the best but it works
    function YL_woo_product_type($false,$product_id) { 
        if ($false === false) { // don't know why, but this is how woo does it
            global $post;
            // maybe redo it someday?!
            if (is_object($post) && !empty($post)) { // post is set
                if ($post->post_type == 'dish' && $post->ID == $product_id) 
                    return 'dish';
                else {
                    $product = get_post( $product_id );
                    if (is_object($product) && !is_wp_error($product)) { // post not set but it's a dish
                        if ($product->post_type == 'dish') 
                            return 'dish';
                    } // end if 
                }    
    
            } else if(wp_doing_ajax()) { // has post set (usefull when adding using ajax)
                $product_post = get_post( $product_id );
                if ($product_post->post_type == 'dish') 
                    return 'dish';
            } else { 
                $product = get_post( $product_id );
                if (is_object($product) && !is_wp_error($product)) { // post not set but it's a dish
                    if ($product->post_type == 'dish') 
                        return 'dish';
                } // end if 
    
            } // end if  // end if 
    
        } // end if 
        return false;
    }
    add_filter('woocommerce_product_type_query','YL_woo_product_type',12,2 );
    
    function YL_woocommerce_checkout_create_order_line_item_object($item, $cart_item_key, $values, $order) {
    
        $product                    = $values['data'];
        if ($product->get_type() == 'dish') {
            return new YL_WC_Order_Item_Product();
        } // end if 
        return $item ;
    }   
    add_filter( 'woocommerce_checkout_create_order_line_item_object', 'YL_woocommerce_checkout_create_order_line_item_object', 20, 4 );
    
    function cod_woocommerce_checkout_create_order_line_item($item,$cart_item_key,$values,$order) {
        if ($values['data']->get_type() == 'dish') {
            $item->update_meta_data( '_dish', 'yes' ); // add a way to recognize custom post type in ordered items
            return;
        } // end if 
    
    }
    add_action( 'woocommerce_checkout_create_order_line_item', 'cod_woocommerce_checkout_create_order_line_item', 20, 4 );
    
    function YL_woocommerce_get_order_item_classname($classname, $item_type, $id) {
        global $wpdb;
        $is_IA = $wpdb->get_var("SELECT meta_value FROM {$wpdb->prefix}woocommerce_order_itemmeta WHERE order_item_id = {$id} AND meta_key = '_dish'");
    
        if ('yes' === $is_IA) { // load the new class if the item is our custom post
            $classname = 'YL_WC_Order_Item_Product';
        } // end if 
        return $classname;
    }
    add_filter( 'woocommerce_get_order_item_classname', 'YL_woocommerce_get_order_item_classname', 20, 3 );
    

    Any idea how to get the price field right? This doesn’t work:

    
    add_filter('woocommerce_get_price', 'yl_get_dish_price', 20,2);
    function yl_get_dish_price($price,$post) {
    	if ($post->post_type === 'dish') {
    		$price = get_field('price', $post->ID);
    	}
    	return $price;
    }
    
  • Tnx. I was a little afraid it would be done with javascript.

    I tried to figure out how (many times) with this tutorial:
    https://www.advancedcustomfields.com/resources/adding-custom-javascript-fields

    I know how to call javascript to a field but I don’t know what code I should use to add it…

    Would it be something like this?

    
    // Customization to datepicker and timepicker
    function yl_add_label_class_to_acf_fields() {
    
    	?>
    	<script type="text/javascript">
    	(function($) {
    
    		// Check ACF
    		if(typeof acf === 'undefined')
    			return;
    		// Date picker & Google Maps compatibility
    		$('.acf-label').addClass('uk-form-label');
    
    	})(jQuery);	
    	</script>
    
    	<?php
    
    }
    
    add_action('acf/input/admin_footer', 'yl_add_label_class_to_acf_fields');
    
  • This also does not work:

    
    function my_acf_init() {
        acf_update_setting('remove_wp_meta_box', false);
    }
    
    add_action('acf/init', 'my_acf_init');
    
  • Anyone?

    I also tried to give it a priority like this:

    add_filter('acf/settings/remove_wp_meta_box', '__return_false', 20);

    But that also did not work ๐Ÿ™

  • @pekka I’m trying to accomplish pretty much the same thing.

    I have a repeater field with 2 subfields (1 of them is a Post Object field) and I want to pre populate them when creating a new post.

    What was the complete code you used?

    PS. Do I need the code within ‘load-post-new.php’ too?

    tnx

  • Just to be clear…

    I know how to get all these values within a template file, simply because there I can use it like this:

    
            $measurement_client_object					        	= get_field_object('measurement_client');
            $measurement_client						    	        = get_field('measurement_client');
            $measurement_client_name                    	    	= get_the_title($measurement_client->ID);
            $measurement_client_street_and_number	               	= get_field('company_street_and_number', $measurement_client->ID);
            $measurement_client_postal_code			               	= get_field('company_postal_code', $measurement_client->ID);
            $measurement_client_city				              	= get_field('company_city', $measurement_client->ID);
            $measurement_client_phone_number		              	= get_field('company_phone_number', $measurement_client->ID);
            $measurement_client_website				             	= get_field('company_website', $measurement_client->ID);
            $measurement_client_email_addresses                     = get_field('company_email_addresses', $measurement_client->ID);
            $measurement_client_contacts			    	        = get_field('company_contacts', $measurement_client->ID);
            $measurement_client_locations			    	        = get_field('company_locations', $measurement_client->ID);
            $measurement_client_internal_note		    	        = get_field('company_internal_note', $measurement_client->ID);
    

    But within my function.php I can’t get the values like that and I believe I have to use $_POST[โ€˜acfโ€™].

    I call the code to send out WP emails.

    Here’s my code (option ‘basic’ is not working yet, option ‘extra’ does work, option ‘all’ is easily created when I have option ‘basic’ working):

    
    // Get submitted values.
    $values				= $_POST['acf'];
    $measurement_status	= $_POST['acf']['field_5e1475a714c93'];
    $measurement_mail	= $_POST['acf']['field_5f71d9ee8536c'];
    
    if ( $measurement_status == 'completed' && $measurement_mail ) {
    
        $measurement_mail_report_recipients = $_POST['acf']['field_5f6d7d925b2fc'];
    
        if ( $measurement_mail_report_recipients == 'basic' ) {
    
            $measurement_client_id					= intval($_POST['acf']['field_5e147914518a6']);
            $measurement_client						= $_POST['acf']['field_5e147914518a6'];
            $measurement_client_email_addresses		= $_POST['acf']['field_5e14527c1945b']->$measurement_client->$measurement_client_id;
    
            if ( $measurement_client_email_addresses ) {
                $list = array();
                foreach( $measurement_client_email_addresses as $measurement_client_email_address ) {
                    $list[] = $measurement_client_email_address['field_5e1452c41945c'];
                }
                $to = implode(',', $list);
            }
    
        } elseif ( $measurement_mail_report_recipients == 'extra' ) {
    
            $measurement_mail_extra_recipients = $_POST['acf']['field_5f71d4eaaf381'];
    
            if ( $measurement_mail_extra_recipients ) {
                $list = array();
                foreach( $measurement_mail_extra_recipients as $measurement_mail_extra_recipient ) {
                    $list[] = $measurement_mail_extra_recipient['field_5f71d55aaf382'];
                }
                $to = implode(',', $list);
            }
    
        } elseif ( $measurement_mail_report_recipients == 'all' ) {
    
            $to = 'dennisdallau@hotmail.com';
    
        }
    
        // Get subject, message and header for mail and send
    
    }
    

    Within option ‘basic’ I want to retrieve email addresses from the CLIENT group and I don’t know how because these values are within another group ๐Ÿ™

  • My English is not to bad but when I have to express myself technically it sometimes gets a little bit off. Let me try to make it clearer by explaining my situation.

    I have a group: CLIENT
    – Name field (Post title)
    – Address field (text)
    – Phone field (number)
    – Email addresses field (repeater)

    I have a group: MEASUREMENT
    – Client field (Post object from CLIENT group)
    – Measurement field (number)
    – Some other unimportant fields

    When I add a measurement post (and within that post I have selected the Client via the Post Object field) I want to be able to mail the measurement details to the email addresses from the Clients ‘Email addresses field (repeater)’ but that field is NOT in this Group but in the CLIENT group.

    I know how to get sub field values from a repeater field in the SAME GROUP but I don’t know how to get sub field values from a repeater field in ANOTHER GROUP (via a Post Object field) using $_POST[‘acf’].

    With get_field I know I can get the values like this:
    get_field('company_email_addresses', $measurement_client->ID);

    But how does this work with $_POST[‘acf’]?

  • Thanks for your reply.

    I guess I’m not getting it completely..? I tried this:

    
    $measurement_client_id = intval($_POST['acf']['field_5e147914518a6']);
    $measurement_client = get_post($measurement_client_id);
    $measurement_client_email_addresses = get_field('field_5e14527c1945b', $measurement_client_id);
    
    $measurement_client_id = intval($_POST['acf']['field_5e147914518a6']);
    $measurement_client = get_post($measurement_client_id);
    $measurement_client_email_addresses = get_field('field_5e14527c1945b', $measurement_client->ID);
    
    $measurement_client_id = intval($_POST['acf']['field_5e147914518a6']);
    $measurement_client = get_post($measurement_client_id);
    $measurement_client_email_addresses = get_field('company_email_addresses', $measurement_client_id);
    

    The CLIENT field is an Post Object field within the Measurement group and I want to mail the measurement report to the clients emailaddress (a repeater field within the Client group).

    My logic in above code examples is as follows:
    – I know the Client (Post Object field within the Measurement post)
    – I now know the Client Post ID
    – So I could get the email address repeater field within the Client post right?

    No luck. Am I missing something here?

    Tnx John

  • Here’s the code I was looking for:

    
    			$values	= $_POST['acf'];
    			$measurement_mail_extra_recipients = $_POST['acf']['field_5f71d4eaaf381'];
    
    			if ( $measurement_mail_extra_recipients ) {
    				$list = array();
    				foreach( $measurement_mail_extra_recipients as $measurement_mail_extra_recipient ) {
    					$list[] = $measurement_mail_extra_recipient['field_5f71d55aaf382', $measurement_client];
    				}
    				$to = implode(',', $list);
    			}
    
  • Or even just ADDING these classes would also do the trick….

  • I’ve tried to play around with:

    function my_acf_render_field( $field ) {
        echo 'test';
    }
    
    // Apply to all fields.
    add_action('acf/render_field', 'my_acf_render_field', 5);
    
    // Apply to image fields.
    // add_action('acf/render_field/type=image', 'my_acf_render_field');
    
    // Apply to fields named "hero_text".
    // add_action('acf/render_field/name=hero_text', 'my_acf_render_field');
    
    // Apply to field with key "field_123abcf".
    // add_action('acf/render_field/key=field_123abcf', 'my_acf_render_field');

    and your example does have some effect but only adds html before (or after) the regular html…

    NOTE: I can’t just rewrite the whole html output because I have some select fields that are flexible populated.

  • That’s exactly what I need!!!!!!!!!

    I only guess my conditional logic is not thought thru correctly… Because the date-conditions are all there (WOOOHOOOOOOO ๐Ÿ˜‰ but the fields react different than what I was exception.

    OPTION PAGE has the following fields

    BOOKING EXCEPTIONS FIELD (repeater) with 2 sub fields:

    DATE FIELD = booking_setting_exceptions_date
    SESSION FIELD = booking_setting_exceptions_session

    FRONTEND FORM has the following fields

    DATE FIELD = field_5ed4178dd63d7
    SESSION FIELD = field_5ed4181bd63dc

    So what I want is that when the DATE and SESSION fields (from option page) match with the DATE and SESSION fields (from user input in frontend form) the field booking_time_session_1 should be hidden.

    In simple words: Within the option page I want to be able to disable specific dates with specific time sessions.

    // Apply conditions to fields
    add_filter('acf/prepare_field/name=booking_time_session_1', 'yl_check_booking_setting_exceptions');
    function yl_check_booking_setting_exceptions($field){
    	$conditions = array();
    	if (have_rows('booking_setting_exceptions', 'booking_settings')) {
    		while (have_rows('booking_setting_exceptions', 'booking_settings')) {
    			the_row();
    			$date = date_i18n('Ymd', strtotime(get_sub_field('booking_setting_exceptions_date', 'booking_settings')));
    			$session = get_sub_field('booking_setting_exceptions_date', 'booking_settings');
    			if (empty($date)) {
    				// no date, skip this row
    				continue;
    			}
    			// Add the condition to the field
    			$conditions[] =
    				array(
    					array(
    					'field'   => 'field_5ed4178dd63d7', DATE field in the form
    					'operator'  => '!=', // If Value is different, then show the field
    					'value'   => $date, // DATE option page value
    				),
    				array(
    					'field'   => 'field_5ed4181bd63dc', // SESSION field in the form
    					'operator'  => '!=', // If Value is different, then show the field
    					'value'   => $session, // SESSION option page value
    				)
    			);
    		} // end while have_rows
    	} // end if have_rows
    	$field['conditional_logic'] = $conditions;
    	// Return
    	return $field;
    }
  • So that’s it?

    I got everything right except the last piece of the puzzle and there’s nobody willing to help me trough?

  • I think I should FOREACH a part of the array. ? Is that a correct thinking?

    Although this does not work, could you help me please? This is the last part in a long journey to develop my own booking system:

    // Apply conditions to fields
    add_filter('acf/prepare_field/name=bookings_field_time_session_1', 'yl_check_booking_exeptions_session_1');
    function yl_check_booking_exeptions_session_1( $field ) {
    
        // Retrieve option values. Date value should be like: 20200611 (unformatted)
        $rows = get_field('bookings_settings_disabled_exceptions', 'bookings');
    
    	if( $rows ) {
    
            foreach ( $rows as $row ) {
                $option_date = get_sub_field('bookings_settings_disabled_date', 'bookings', false);
                $date = date_i18n('Ymd', strtotime($option_date));
                $session = get_sub_field('bookings_settings_disabled_session', 'bookings', false);
    
                $arrays =
                    array(
                        array(
                            'field'     => 'field_5ed4181bd63dc', // Time field session 1 in the form
                            'operator'  => '!=', // If Value is different, then show the field
                            'value'     => $session, // Compare against option page value
                        ),
                        array(
                            'field'     => 'field_5ed4178dd63d7', // Datepicker fiels in the form
                            'operator'  => '!=', // If Value is different, then show the field
                            'value'     => $date, // Compare against option page value
                        )
                    );
                }
    
                if ( $session == '1' ) {
                    $field['conditional_logic'] = array(
                        $arrays
                    );
                }
            }
    
            // Return
            return $field;
    
        }
    
    }
  • Thanks for looking into my code.

    But it still only outputs 1 condition (session / date combi) although I have 3 entries in the option page… and the other entries don’t have any effect

    This is the condition output I get for for the field:

    data-conditions="[[{"field":"field_5ed4181bd63dc","operator":"==","value":"1"},{"field":"field_5ed4178dd63d7","operator":"!=","value":"20200627"}],[]]"

    The whole code is this:

    // Apply conditions to fields
    add_filter('acf/prepare_field/name=bookings_field_time_session_1', 'yl_check_booking_exeptions_session_1');
    function yl_check_booking_exeptions_session_1( $field ) {
    
        // Retrieve option values. Date value should be like: 20200611 (unformatted)
    	if( have_rows('bookings_settings_disabled_exceptions', 'bookings') ) {
    		while ( have_rows('bookings_settings_disabled_exceptions', 'bookings') ) {
    			the_row();
    			$option_date = get_sub_field('bookings_settings_disabled_date', 'bookings', false);
    			$date = date_i18n('Ymd', strtotime($option_date));
    			$session = get_sub_field('bookings_settings_disabled_session', 'bookings', false);
    
    			if ( $session == '1' ) {
    				// Add the condition to the field
    				$field['conditional_logic'] = array(
    				  // the first nested array holds OR conditions
    				  // there would be a separate nested array for each OR condition
    				  array(
    					// the second nesting is AND conditions
    					array(
    					  'field'     => 'field_5ed4181bd63dc', // Time field session 1 in the form
    					  'operator'  => '!=', // If Value is different, then show the field
    					  'value'     => $session, // Compare against option page value
    					),
    					array(
    					  'field'     => 'field_5ed4178dd63d7', // Datepicker fiels in the form
    					  'operator'  => '!=', // If Value is different, then show the field
    					  'value'     => $date, // Compare against option page value
    					)
    				  ),
    				  array(
    					// this is an example, if there was an OR condition those would go here
    				  )
    				);
    			}
    
    		}
    
    	}
    
    	// Return
    	return $field;
    
    }
  • Now I don’t see anything so the code is gone but the output becomes blank.

    Just to mention again: The shortcodes are added to an ACF textarea field and that field is used as email BODY. So I do not use the regular content field from the post itself.

    Any other ideas? I saw your example code online asiel and many people say it works so I guess it does but only for the regular content field and not for ACF fields?

    I also tried to activate shortcode within ACF field by adding this to functions.php but again the same blank result:

    add_filter('acf/format_value/type=textarea', 'do_shortcode');
    add_filter('acf/format_value/type=text', 'do_shortcode');

    Anything?

Viewing 25 posts - 1 through 25 (of 29 total)