0

Please help!! I need one of my woocommerce checkout fields to display only on the condition that product x (product 179, as you'll see below) is not in the user's cart. As it is, I can get this to work properly using the default woocommerce fields (derived from [billing],[shipping],[account], and [order]... but I need this to work for a custom checkout field that I created as shown here:

/**
* Add the field to the checkout (the script)
*/
add_action( 'woocommerce_after_order_notes', 'the_script' );

function the_script( $checkout ) {

echo '<div id="the_script"><h2>' . __('') . '</h2>';

woocommerce_form_field( 'user_script', array(
    'type'          => 'textarea',
    'class'         => array('extracheckoutinfo form-row-wide'),
    'label'         => __('What is the script?'),
    'placeholder'   => __('You can skip this section if you added the "Create the Script" extra to your purchase'),
    'required'      => true
    ), $checkout->get_value( 'user_script' ));

echo '</div>';

}



/**
* Process the checkout (the script)
*/
add_action('woocommerce_checkout_process', 'the_script_field_process');

function the_script_field_process() {
// Check if set, if its not set add an error.
if ( ! $_POST['user_script'] )
    wc_add_notice( __( 'Provide<strong>the script you want to use.</strong>' ), 'error' );
 }

 /**
 * Update the order meta with field value (the script)
 */

add_action( 'woocommerce_checkout_update_order_meta',     'the_script_field_update_order_meta' );

 function call_script_field_update_order_meta( $order_id ) {
if ( ! empty( $_POST['user_script'] ) ) {
    update_post_meta( $order_id, 'The Script', sanitize_text_field( $_POST['user_script'] ) );
}
}



/**
* Display field value on the order edit page (the  script)
*/
add_action( 'woocommerce_admin_order_data_after_billing_address', 'the_script_field_display_admin_order_meta', 10, 1 );

function the_script_field_display_admin_order_meta($order){
echo '<p><strong>'.__('The Script').':</strong> ' . get_post_meta( $order->id, 'The Script', true ) . '</p>';
}



/**
* Check if a specific product ID is in the cart
*/
function wc_ninja_product_is_in_the_cart() {
// Add your special product IDs here
$ids = array( '179' );;

// Products currently in the cart
$cart_ids = array();

// Find each product in the cart and add it to the $cart_ids array
foreach( WC()->cart->get_cart() as $cart_item_key => $values ) {
    $cart_product = $values['data'];
    $cart_ids[]   = $cart_product->id;
}

// If one of the special products are in the cart, return true.
if ( ! empty( array_intersect( $ids, $cart_ids ) ) ) {
    return true;
} else {
    return false;
}
}


/**
* Conditionally remove a checkout field based on products in the cart
*/
function wc_ninja_remove_checkout_field( $fields ) {
if ( wc_ninja_product_is_in_the_cart() ) {
    unset( $fields['billing']['billing_first_name'] );
}

return $fields;
}
add_filter( 'woocommerce_checkout_fields' , 'wc_ninja_remove_checkout_field' );

So basically, I think I need to change unset( $fields['billing']['billing_first_name'] ); to something like unset( $fields['the_script']['user_script'] );

but of course it's not working that way. I can't figure out how to access the custom field I created to do this. Been trying for hours without luck!!! Someone please help

0

Ok, I figured it out (finally)

Instead of using the code above to add a 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'] );

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.