Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I am using WooCommerce on one of the site. I need to add one more field at the time of checkout to ask buyer wheteher to issue Invoice on his name or the name of his business?

Any advise how to do that? Or something like this is already there in Woo Commerce? Or what could be the simplest way to do that?

share|improve this question

3 Answers 3

up vote 2 down vote accepted

As it happens, I just blogged about this yesterday, for how to add a subscribe to mailing list checkbox. The key points are:

  • look at the WooCommerce tutorial
  • woocommerce_checkout_fields is a filter hook that allows you to add or modify what fields appear on the checkout form
  • woocommerce_checkout_update_order_meta is an action hook that allows you to save your new fields
  • woocommerce_email_order_meta_keys is a filter hook that allows you to add your new fields to the confirmation emails
  • the name you pick to save the post meta will be its label in the email

Here's the code I used:

class WooSubscribeCheckbox {

    // add hooks into WooCommerce
    public static function run() {
        add_filter('woocommerce_checkout_fields',
            array(__CLASS__, 'filterWooCheckoutFields'));

        add_action('woocommerce_checkout_update_order_meta',
            array(__CLASS__, 'actionWooCheckoutUpdateOrderMeta'));

        add_filter('woocommerce_email_order_meta_keys',
            array(__CLASS__, 'filterWooEmailOrderMetaKeys'));
    }

    /**
    * add custom fields to WooCommerce checkout
    * @param array fields
    * @return array
    */
    public static function filterWooCheckoutFields($fields) {
        global $woocommerce;

        // add field at end of billing fields section
        $fields['billing']['our_mailing_subscribe'] = array(
            'type' => 'checkbox',
            'label' => 'Subscribe to mailing list?',
            'placeholder' => 'Subscribe to mailing list',
            'required' => false,
            'class' => array(),
            'label_class' => array(),
        );

        return $fields;
    }

    /**
    * save custom order fields
    * @param int $order_id
    */
    public static function actionWooCheckoutUpdateOrderMeta($order_id) {
        if (isset($_POST['our_mailing_subscribe'])) {
            update_post_meta($order_id, 'Subscribe to mailing list',
                stripslashes($_POST['our_mailing_subscribe']));
        }
    }

    /**
    * add our custom fields to WooCommerce order emails
    * @param array $keys
    * @return array
    */
    public static function filterWooEmailOrderMetaKeys($keys) {
        $keys[] = 'Subscribe to mailing list';

        return $keys;
    }

}

WooSubscribeCheckbox::run();
share|improve this answer
    
+1 much better than a RTFM, but you already had it coded ;) - thx for aussieSing clarification! –  brasofilo Dec 31 '12 at 23:10
    
Thanks, and Happy New Year -- it's already over 10 hours old here, feels hotter. I hope it's going well for you! cheers, Ross –  webaware Dec 31 '12 at 23:19
    
Thanks, Ross, happy NY too! Snow haven't arrived here yet, greetings from Spain, Rodolfo –  brasofilo Dec 31 '12 at 23:36
    
Thanks, this helped me to do what I needed. Thanks again. –  Krunal Jan 1 '13 at 5:19

The plugin is packed with hooks and provides an extensive documentation.

From that link: Tutorial – Customising checkout fields using hooks and filters.

How are checkout fields loaded into WooCommerce?

The fields for the checkout (billing and shipping) come from the countries class (class-wc-countries.php) and the get_address_fields function- this is so locale settings are applied to the fields letting WooCommerce enable/disable fields based on the users location.

Before returning the fields, WC will run the fields through a filter. This allows them to be edited by third party plugins and themes (and your own custom code).

my emphasis

share|improve this answer
    
Pity that we cannot edit those docs... It is "CustomiZing" and it should be "using actions and filters", or only "using hooks". (just tweeted the author about this) –  brasofilo Dec 31 '12 at 12:58
    
CustomiSing is how we say it Down Under, I think at least one of those guys is either Aussie or Kiwi. –  webaware Dec 31 '12 at 22:21

If you don't want to code it yourself. There is a nice plugin available here: http://terrytsang.com/shop/shop/woocommerce-custom-checkout-options/

share|improve this answer
    
An answer should be more than just a link to an external site. Please add a solution. –  toscho Dec 31 '12 at 14:31

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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