is_woocommerce() : check is woocommerce
is_cart() : check is cart
is_checkout() : check is checkout page
1. Add News Letter Check Box in woo commerce checkout page
<?php
class
WooSubscribeCheckbox {
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'
));
}
public
static
function
filterWooCheckoutFields(
$fields
) {
global
$woocommerce
;
$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
;
}
public
static
function
actionWooCheckoutUpdateOrderMeta(
$order_id
) {
$subscribe
= isset(
$_POST
[
'our_mailing_subscribe'
]) ?
'yes'
:
'no'
;
update_post_meta(
$order_id
,
'Subscribe to mailing list'
,
$subscribe
);
}
public
static
function
filterWooEmailOrderMetaKeys(
$keys
) {
$keys
[] =
'Subscribe to mailing list'
;
return
$keys
;
}
}
WooSubscribeCheckbox::run();
Edit: how to auto-tick the checkbox:
add_filter(
'woocommerce_checkout_get_value'
,
function
(
$value
,
$input
) {
if
(
$input
==
'our_mailing_subscribe'
) {
$value
=
'yes'
;
}
return
$value
;
}, 10, 2);
For More information check out this link
https://snippets.webaware.com.au/snippets/subscribe-to-mailing-list-in-woocommerce-checkout/
2. Add select (Drop Down) in woo commerce Checkout Page
/**
* 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>'.__('My Field').'</h3>';
woocommerce_form_field( 'my_field_name', array(
'type' => 'select',
'class' => array('my-field-class form-row-wide'),
'required' => true,
'label' => __('Fill in this field'),
'placeholder' => __('Enter something'),
'options' => array(
'choice1' => __('choice1', 'woocommerce' ),
'choice2' => __('choice2', 'woocommerce' )
)
), $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['my_field_name'])
$woocommerce->add_error( __('Please enter something into this new shiny field.') );
}
/**
* 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['my_field_name']) update_post_meta( $order_id, 'My Field', esc_attr($_POST['my_field_name']));
}
For more Details
https://wordpress.org/support/topic/checkout-custom-fields-with-select-type
3. Add multi Select in woocommerce checkout page
add_filter( 'woocommerce_form_field_multiselect', 'custom_multiselect_handler', 10, 4 );
function custom_multiselect_handler( $field, $key, $args, $value ) {
$options = '';
if ( ! empty( $args['options'] ) ) {
foreach ( $args['options'] as $option_key => $option_text ) {
$options .= '<option value="' . $option_key . '" '. selected( $value, $option_key, false ) . '>' . $option_text .'</option>';
}
$field = '<p class="form-row ' . implode( ' ', $args['class'] ) .'" id="' . $key . '_field">
<label for="' . $key . '" class="' . implode( ' ', $args['label_class'] ) .'">' . $args['label']. $required . '</label>
<select name="' . $key . '" id="' . $key . '" class="select" multiple="multiple">
' . $options . '
</select>
</p>' . $after;
}
return $field;
}
add_action('woocommerce_after_order_notes', 'my_custom_checkout_field');
function my_custom_checkout_field( $checkout ) {
echo '<div id="my_custom_checkout_field"><h3>'.__('My Field').'</h3>';
woocommerce_form_field( 'my_field_name', array(
'type' => 'multiselect',
'class' => array('my-field-class form-row-wide'),
'label' => __('Fill in this field'),
'placeholder' => __('Enter something'),
'options' => array(
'Buick' => __('Buick', 'woocommerce' ),
'Ford' => __('Ford', 'woocommerce' )
)
), $checkout->get_value( 'my_field_name' ));
echo '</div>';
}
More Info Here
http://stackoverflow.com/questions/13684533/woocommerce-custom-fields-multiselect
This comment has been removed by a blog administrator.
ReplyDelete