Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
    Posts
  • #16021

    Hey Abundance-

    I’ve added this coding to my functions.php file:

    /**

    * Add the field to the checkout

    **/

    add_action(‘woocommerce_after_order_notes’, ‘my_custom_checkout_field’);

    function my_custom_checkout_field( $checkout ) {

    echo ‘<div id=”my_custom_checkout_field”><h3>’.__(‘Delivery Date’).'</h3>';

    woocommerce_form_field( ‘delivery_date_name’, array(

    ‘type’ => ‘text’, ‘class’ => array(‘delivery-date-class form-row-wide’),

    ‘label’ => __(‘Eg. Tues, July 22.’),

    ‘placeholder’ => __(‘Date of Delivery’),

    ), $checkout->get_value( ‘my_field_name’ ));

    echo ‘</div>';

    }

    /**

    * Process the checkout

    **/add_action(‘woocommerce_checkout_process’, ‘my_custom_checkout_field_process’);

    function my_custom_checkout_field_process() {

    global $woocommerce;

    // Check if set, if its not set add an error.

    if (!$_POST)

    $woocommerce->add_error( __(‘Please enter delivry date.’) );

    }

    /** * Update the order meta with field value

    **/

    add_action(‘woocommerce_checkout_update_order_meta’, ‘my_custom_checkout_field_update_order_meta’);

    function my_custom_checkout_field_update_order_meta( $order_id ) {

    if ($_POST) update_post_meta( $order_id, ‘Delivery Date’, esc_attr($_POST));

    }

    WORKS GREAT! I can see my new custom field reads Delivery Date. The problem happens when I want to add another custom field to read Recipeint Phone. I add the following code right underneath the above:

    /**

    * Add the field to the checkout

    **/

    add_action(‘woocommerce_after_order_notes’, ‘my_custom_checkout_field’);

    function my_custom_checkout_field( $checkout ) {

    echo ‘<div id=”my_custom_checkout_field”><h3>’.__(‘Recipeint Phone’).'</h3>';

    woocommerce_form_field( ‘recipient_phone_name’, array(

    ‘type’ => ‘text’, ‘class’ => array(‘recipient-phone-class form-row-wide’),

    ‘label’ => __(‘Home & or Cell’),

    ‘placeholder’ => __(‘Both numbers would be great!’),

    ), $checkout->get_value( ‘recipeint_phone_name’ ));

    echo ‘</div>';

    }

    /**

    * Process the checkout

    **/add_action(‘woocommerce_checkout_process’, ‘my_custom_checkout_field_process’);

    function my_custom_checkout_field_process() {

    global $woocommerce;

    // Check if set, if its not set add an error.

    if (!$_POST)

    $woocommerce->add_error( __(‘Please enter phone number.’) );

    }

    /** * Update the order meta with field value

    **/

    add_action(‘woocommerce_checkout_update_order_meta’, ‘my_custom_checkout_field_update_order_meta’);

    function my_custom_checkout_field_update_order_meta( $order_id ) {

    if ($_POST) update_post_meta( $order_id, ‘Recipient Phone’, esc_attr($_POST));

    }

    My site just goes blank. Can someone tell me if there’s something wrong with the second part of this coding?

    Minnoe

    #89835

    You use the same function name several times. (my_custom_checkout_field). Use following code instead:

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

    function minnoe_ordernotes_custom_checkout_field( $checkout ) {
    echo '<div id="my_custom_checkout_field"><h3>'.__('Delivery Date').'</h3>';
    woocommerce_form_field( 'delivery_date_name', array(
    'type' => 'text', 'class' => array('delivery-date-class form-row-wide'),
    'label' => __('Eg. Tues, July 22.'),
    'placeholder' => __('Date of Delivery'),
    ), $checkout->get_value( 'my_field_name' ));
    echo '</div>';
    }
    /**
    * Process the checkout
    **/add_action('woocommerce_checkout_process', 'minnoe_delivery_custom_checkout_field_process');
    function minnoe_delivery_custom_checkout_field_process() {
    global $woocommerce;
    // Check if set, if its not set add an error.
    if (!$_POST['delivery_date_name'])
    $woocommerce->add_error( __('Please enter delivry date.') );
    }
    /** * Update the order meta with field value
    **/
    add_action('woocommerce_checkout_update_order_meta', 'minnoe_delivery_custom_checkout_field_update_order_meta');
    function minnoe_delivery_custom_checkout_field_update_order_meta( $order_id ) {
    if ($_POST['delivery_date_name']) update_post_meta( $order_id, 'Delivery Date', esc_attr($_POST['delivery_date_name']));
    }

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

    function minnoe_notes_custom_checkout_field( $checkout ) {
    echo '<div id="my_custom_checkout_field"><h3>'.__('Recipeint Phone').'</h3>';
    woocommerce_form_field( 'recipient_phone_name', array(
    'type' => 'text', 'class' => array('recipient-phone-class form-row-wide'),
    'label' => __('Home & or Cell'),
    'placeholder' => __('Both numbers would be great!'),
    ), $checkout->get_value( 'recipeint_phone_name' ));
    echo '</div>';
    }
    /**
    * Process the checkout
    **/add_action('woocommerce_checkout_process', 'minnoe_custom_checkout_field_process');
    function minnoe_custom_checkout_field_process() {
    global $woocommerce;
    // Check if set, if its not set add an error.
    if (!$_POST['recipeint_phone_name'])
    $woocommerce->add_error( __('Please enter phone number.') );
    }
    /** * Update the order meta with field value
    **/
    add_action('woocommerce_checkout_update_order_meta', 'minnoe_custom_checkout_field_update_order_meta');
    function minnoe_custom_checkout_field_update_order_meta( $order_id ) {
    if ($_POST['delivery_date_name']) update_post_meta( $order_id, 'Recipient Phone', esc_attr($_POST['my_field_name']));
    }

    #89836

    Thank you Peter. You’ve brought me closer to my ecommerce riches!

    Minnoe

    #89837

    Hey,

    Glad Peter could help.

    Regards,

    Ismael

Viewing 4 posts - 1 through 4 (of 4 total)

The topic ‘Coding Question’ is closed to new replies.