碰巧的是,我昨天刚刚在博客上写了这件事,想知道如何add a subscribe to mailing list checkbox. 关键点是:
查看WooCommerce tutorial
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();