Skip to content

Instantly share code, notes, and snippets.

@alexmustin
Last active July 23, 2018 19:11
Show Gist options
  • Save alexmustin/4e56f093f4ccb87c5a4b41992fd5677f to your computer and use it in GitHub Desktop.
Save alexmustin/4e56f093f4ccb87c5a4b41992fd5677f to your computer and use it in GitHub Desktop.
WooCommerce - Add custom field to Checkout page
<?php
/* ADD 'WEBSITE' REQUIRED FIELD TO CHECKOUT PAGE
----------------------------------------------------------------------------- */
//* Check if a specific product ID is in the cart
function hm_product_is_in_cart( $ids ) {
// 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;
}
}
//* Check if a specific product category is in the cart
function hm_category_is_in_cart( $categories ) {
// Products currently in the cart
$cart_ids = array();
// Categories currently in the cart
$cart_categories = 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;
}
// Connect the products in the cart w/ their categories
foreach( $cart_ids as $id ) {
$products_categories = get_the_terms( $id, 'product_cat' );
// Loop through each product category and add it to our $cart_categories array
foreach ( $products_categories as $products_category ) {
$cart_categories[] = $products_category->slug;
}
}
// If one of the special categories are in the cart, return true.
if ( ! empty( array_intersect( $categories, $cart_categories ) ) ) {
return true;
} else {
return false;
}
}
//* Add the field to the Checkout screen
add_action( 'woocommerce_before_order_notes', 'add_website_url_field' );
function add_website_url_field( $checkout ) {
$categories = array( 'hosting-maintenance' );
if ( hm_category_is_in_cart( $categories ) ) {
echo '<div id="website_url_field" class="woocommerce-additional-fields__field-wrapper"><h3>' . __('Website') . '</h3>';
woocommerce_form_field( 'hm_website_url', array(
'type' => 'text',
'class' => array('hm-website-url form-row-wide'),
'label' => __('Website URL for Hosting & Maintenance'),
'placeholder' => __('http://yoursite.com'),
), $checkout->get_value( 'hm_website_url' ));
echo '</div>';
}
}
// PHP: Remove "(optional)" from 'Website URL' description
add_filter( 'woocommerce_form_field' , 'remove_checkout_optional_fields_label', 10, 4 );
function remove_checkout_optional_fields_label( $field, $key, $args, $value ) {
// Only on checkout page
if( ! ( is_checkout() && ! is_wc_endpoint_url() ) )
return $field;
$optional = '&nbsp;<span class="optional">(' . esc_html__( 'optional', 'woocommerce' ) . ')</span>';
$required = '&nbsp;<abbr class="required" title="required">*</abbr>';
$keys = array( 'hm_website_url' );
if( in_array($key, $keys) )
return str_replace( $optional, $required, $field );
else
return $field;
}
//* Process the new Checkout field
add_action('woocommerce_checkout_process', 'custom_checkout_field_process');
function custom_checkout_field_process() {
$categories = array( 'hosting-maintenance' );
if ( hm_category_is_in_cart( $categories ) ) {
// Check if set, if its not set add an error.
if ( ! $_POST['hm_website_url'] ) {
wc_add_notice( __( 'Please enter your Website URL for Hosting & Maintenance, even if the domain is not live yet.' ), 'error' );
}
}
}
//* Update the order meta with field value
add_action( 'woocommerce_checkout_update_order_meta', 'custom_website_url_field_update_order_meta' );
function custom_website_url_field_update_order_meta( $order_id ) {
if ( ! empty( $_POST['hm_website_url'] ) ) {
update_post_meta( $order_id, 'Website URL', sanitize_text_field( $_POST['hm_website_url'] ) );
}
}
//* Display field value on the order edit page
add_action( 'woocommerce_admin_order_data_after_billing_address', 'custom_website_url_field_display_admin_order_meta', 10, 1 );
function custom_website_url_field_display_admin_order_meta($order){
$websiteURL = get_post_meta( $order->id, 'Website URL', true );
if ( $websiteURL ) {
echo '<p><strong>'.__('Website URL').':</strong> ' . $websiteURL . '</p>';
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment