Viewing 15 replies - 1 through 15 (of 16 total)
  • Caleb Burks

    (@icaleb)

    Automattic Happiness Engineer

    I used this code: https://docs.woocommerce.com/document/tutorial-customising-checkout-fields-using-actions-and-filters/#section-3, and added a custom field to checkout. Then I tested with OPC, and the field shows up.

    So there must be something wrong with your custom code. Perhaps start by adding a visible field or the one listed in the example.

    Thread Starter snooper

    (@snooper)

    Thanks Caleb

    here is my code:

    /*************************************************************/
    
    function order_origin( $checkout ) {
    
    woocommerce_form_field( 'order_link_origin', array(
            'type'          => 'hidden',
            'value'   => get_the_title() ,
            ), $checkout->get_value( 'order_link_origin' ));
    
    }
    
    add_action('woocommerce_after_order_notes', 'order_origin' );
    
    /**
     * Display field value on the order edit page
     */
    add_action( 'woocommerce_admin_order_data_after_order_notes', 'order_origin_display_admin_order_meta', 10, 1 );
    
    function order_origin_display_admin_order_meta($order){
        echo '<p><strong>'.__('Order Origin').':</strong> ' . get_post_meta( $order->id, 'order_link_origin', true ) . '</p>';
    }
    Caleb Burks

    (@icaleb)

    Automattic Happiness Engineer

    Type “hidden” isn’t a supported option I believe. You can just assign a css class and hide it. And then ‘value’ isn’t an option either, but you can use ‘default’.

    Thread Starter snooper

    (@snooper)

    interesting – thanks, will give it a go!

    Thread Starter snooper

    (@snooper)

    Great – it works! Thanks!

    Thread Starter snooper

    (@snooper)

    Update:

    I am not seeing the new fields in the order details.

    Is there maybe something wrong with that filter code?

    /**
     * Display field value on the order edit page
     */
    add_action( 'woocommerce_admin_order_data_after_order_notes', 'order_origin_display_admin_order_meta', 10, 1 );
    
    function order_origin_display_admin_order_meta($order){
        echo '<p><strong>'.__('Order Origin').':</strong> ' . get_post_meta( $order->id, 'order_link_origin', true ) . '</p>';
    }
    Plugin Contributor Mike Jolley (a11n)

    (@mikejolley)

    I don’t think you’re storing it (order_link_origin). I assume the meta is empty when you view the order?

    Thread Starter snooper

    (@snooper)

    right now I don’t even see the field label on the order page

    What am I doing wrong?

    Caleb Burks

    (@icaleb)

    Automattic Happiness Engineer

    Where did you get the ‘woocommerce_admin_order_data_after_order_notes’ hook from?

    ‘woocommerce_admin_order_data_after_shipping_address’ will add your custom text to the admin page here: https://github.com/woothemes/woocommerce/blob/efd390e9510d168ae730b05504225aa21b2a99cc/includes/admin/meta-boxes/class-wc-meta-box-order-data.php#L399

    Thread Starter snooper

    (@snooper)

    Thanks Caleb.
    I see the label now, but still no content for the field value.

    How can I check whether the issue is that its not being saved, or not being displayed properly?

    Caleb Burks

    (@icaleb)

    Automattic Happiness Engineer

    You aren’t setting the value correctly in the woocommerce_form_field method: https://github.com/woothemes/woocommerce/blob/cfc31dd311d7f05d7f73ec6d66717c150f70ca00/includes/wc-template-functions.php#L1845

    The last parameter is the value, and that needs to be get_the_title()

    Thread Starter snooper

    (@snooper)

    Thanks, this is my current code, which is not working (i had changed as per a previous comment):

    function order_origin( $checkout ) {
    	woocommerce_form_field( 'order_link_origin', array(
           		'type'          => 'text',
           	 	'default'   => get_the_title() ,
            	), $checkout->get_value( 'order_link_origin' ));
    }
    
    add_action('woocommerce_after_order_notes', 'order_origin' );
    Caleb Burks

    (@icaleb)

    Automattic Happiness Engineer

    You are still setting the value to $checkout->get_value( 'order_link_origin' ). The meta key for order_link_origin is not set at this point in the process. Per the above post: “The last parameter is the value, and that needs to be get_the_title()”.

    Thread Starter snooper

    (@snooper)

    Thanks Caleb
    sorry, it’s not clear to me.
    Are you saying that the code needs to be like this:

    function order_origin( $checkout ) {
    	woocommerce_form_field( 'order_link_origin', array(
           		'type'          => 'text',
           	 	//'default'   => get_the_title() ,
            	), $checkout->get_the_title();
    }
    Caleb Burks

    (@icaleb)

    Automattic Happiness Engineer

    Wait, it was right before – my bad. You just aren’t saving it.

    function order_origin_create_field( $checkout ) {
    	woocommerce_form_field( 'order_link_origin', array(
           		'type'    => 'text',
           		'default' => get_the_title(),
            	), $checkout->get_value( 'order_link_origin' ) );
    }
    add_action( 'woocommerce_after_order_notes', 'order_origin_create_field' );
    
    function order_origin_update_order_meta( $order_id ) {
        if ( ! empty( $_POST['order_link_origin'] ) ) {
            update_post_meta( $order_id, 'order_link_origin', sanitize_text_field( $_POST['order_link_origin'] ) );
        }
    }
    add_action( 'woocommerce_checkout_update_order_meta', 'order_origin_update_order_meta' );
    
    function order_origin_display_admin_order_meta( $order ){
        echo '<p><strong>Order Origin:</strong> ' . get_post_meta( $order->id, 'order_link_origin', true ) . '</p>';
    }
    add_action( 'woocommerce_admin_order_data_after_shipping_address', 'order_origin_display_admin_order_meta', 10 );

    Read all of this section: https://docs.woocommerce.com/document/tutorial-customising-checkout-fields-using-actions-and-filters/#section-5

Viewing 15 replies - 1 through 15 (of 16 total)
  • The topic ‘One Page checkout plugin with custom checkout fields’ is closed to new replies.