• Resolved kosmicbird

    (@kosmicbird)


    I’m trying to set up a conditional checkout rule.

    Example: When product x is in the cart, do not show checkout field x.

    I have already added the appropriate code I need to do this. The problem is that I can’t get it to work because I can’t figure out how to target the custom checkout fields I created. Thus far, I can only get it to work with the default checkout fields.

    For instance, to target “First Name” and make it disappear, part of the code I need to use is:

    unset( $fields['billing']['billing_first_name] );

    If I wanted to target other fields, I could also use ‘account’, ‘order’, and ‘shipping’ along with the appropriate selector for what I’m doing. This works just fine.

    But how do I target the fields I created myself? As an example, here is the code for a custom field I added:

    add_action( 'woocommerce_after_order_notes', 'call_script' );
    
    function call_script( $checkout ) {
    
    echo '<div id="call_script"><h2>' . __('') . '</h2>';
    
    woocommerce_form_field  ( 'the_script', array(
        'type'    =>    'textarea',
        'class'   => array('extracheckoutinfo form-row-wide'),
        'label'   =>  __('What is the script?'),
        'placeholder'  =>   __('You can skin this section if you added the "script" extra to your purchase'),
        'required'    =>   true ),
    
    $checkout->get_value ( 'the_script' ));
    
    echo '</div>';
    
    }

    But if I try to use something like

    unset( $fields['call_script']['the_script] );

    in my code to apply conditional checkout rules, it does not work. So how do I target the custom field I created? I would really appreciate any help on this.

Viewing 1 replies (of 1 total)
  • Thread Starter kosmicbird

    (@kosmicbird)

    Problem resolved. Instead of using the code above to add my new custom field, I changed it to this:

    add_filter( 'woocommerce_checkout_fields', 'custom_override_checkout_fields' );
    
    function custom_override_checkout_fields ( $fields ) {
    
    $fields['order']['the_script'] = array (
    
    woocommerce_form_field  ( 'the_script', array(
        'type'    =>    'textarea',
        'class'   => array('extracheckoutinfo form-row-wide'),
        'label'   =>  __('What is the script?'),
        'placeholder'  =>   __('You can skin this section if you added the "script" extra to your purchase'),
        'required'    =>   true
    
    );
    
    return $fields
    
    }

    Which now allows me to target my custom field like this:

    unset ($fields['order']['the_script'] );

Viewing 1 replies (of 1 total)
  • The topic ‘Targeting Custom Checkout Fields for Conditional Checkout’ is closed to new replies.