Skip to content

Instantly share code, notes, and snippets.

@hchouhan
Created August 15, 2018 09:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hchouhan/b17eec893aeb848b0dac1dfd71d1c740 to your computer and use it in GitHub Desktop.
Save hchouhan/b17eec893aeb848b0dac1dfd71d1c740 to your computer and use it in GitHub Desktop.
<?php
// =============================================================================
// FUNCTIONS.PHP
// -----------------------------------------------------------------------------
// Overwrite or add your own custom functions to Pro in this file.
// =============================================================================
// =============================================================================
// TABLE OF CONTENTS
// -----------------------------------------------------------------------------
// 01. Enqueue Parent Stylesheet
// 02. Additional Functions
// =============================================================================
// Enqueue Parent Stylesheet
// =============================================================================
add_filter( 'x_enqueue_parent_stylesheet', '__return_true' );
// Additional Functions
// =============================================================================
// remove_action( 'woocommerce_cart_collaterals', 'woocommerce_cross_sell_display' );
// add_action( 'woocommerce_after_cart', 'woocommerce_cross_sell_display' );
/**
* Add Continue Shopping Button on Cart Page
* Add to theme functions.php file or Code Snippets plugin
*/
// add_action( 'woocommerce_after_cart', 'woo_add_continue_shopping_button_to_cart' );
// function woo_add_continue_shopping_button_to_cart() {
// $shop_page_url = get_permalink( wc_get_page_id( 'shop' ) );
// echo ' <div class="align-center mar-top-20"><a href="'.$shop_page_url.'">Continue Shopping</a></div> ';
// }
/* This detaches payment module from order review module in checkout */
remove_action( 'woocommerce_checkout_order_review', 'woocommerce_checkout_payment', 20 );
add_action( 'woocommerce_checkout_after_customer_details', 'woocommerce_checkout_payment', 20 );
/* This force-changes name of labels and placeholders in checkout */
add_filter('woocommerce_default_address_fields', 'override_address_fields');
function override_address_fields( $address_fields ) {
$address_fields['address_1']['label'] = 'Address 1';
$address_fields['city']['placeholder'] = 'City';
return $address_fields;
}
// This removes Order Notes from checkout field in Woocommerce
add_filter( 'woocommerce_checkout_fields' , 'alter_woocommerce_checkout_fields' );
function alter_woocommerce_checkout_fields( $fields ) {
unset($fields['order']['order_comments']);
return $fields;
}
/**
* Hide shipping rates when free shipping is available.
* Updated to support WooCommerce 2.6 Shipping Zones.
*
* @param array $rates Array of rates found for the package.
* @return array
*/
function my_hide_shipping_when_free_is_available( $rates ) {
$free = array();
foreach ( $rates as $rate_id => $rate ) {
if ( 'free_shipping' === $rate->method_id ) {
$free[ $rate_id ] = $rate;
break;
}
}
return ! empty( $free ) ? $free : $rates;
}
add_filter( 'woocommerce_package_rates', 'my_hide_shipping_when_free_is_available', 100 );
/* Set's shipping address checkbox to off as default. That is, says ship to same address as billing */
add_filter( 'woocommerce_ship_to_different_address_checked', '__return_false' );
/* This changes checkout's button to all capitals LOG IN */
add_filter( 'woocommerce_order_button_text', 'woo_custom_order_button_text' );
function woo_custom_order_button_text() {
return __( 'PLACE ORDER', 'woocommerce' );
}
/* This adds clickwrap to bottom of checkout form */
// add_action( 'woocommerce_review_order_before_submit', 'bbloomer_privacy_message_below_checkout_button' );
// function bbloomer_privacy_message_below_checkout_button() {
// echo '<div class="hl-privacy-disclaimer hl-align-center hl-mar-top-20"><p>By checking out, I agree to the <a href="../terms-of-use/" target="_blank">Terms of Use</a> and acknowledge that I have read the <a href="../privacy-policy/" target="_blank">Privacy Policy.</p></div>';
// }
/* This flushes the CSS rules. */
function flush_rules(){
flush_rewrite_rules();
}
add_action('init','flush_rules');
/* This makes the user's email address their username */
add_filter( 'woocommerce_new_customer_data', function( $data ) {
$data['user_login'] = $data['user_email'];
return $data;
} );
/* Remove on-hold as part of payment process path */
// add_filter( 'woocommerce_payment_complete_order_status', 'disable_auto_order_status', 10, 2 );
// function disable_auto_order_status( $order_statuses, $order ) {
// $return array( 'pending', 'failed', 'cancelled' );
// }
/* This limits the states the user can select from to bill and ship */
add_filter( 'woocommerce_states', 'wc_sell_only_states' );
function wc_sell_only_states( $states ) {
$states['US'] = array(
'AL' => __( 'Alabama', 'woocommerce' ),
// 'AK' => __( 'Alaska', 'woocommerce' ),
'AZ' => __( 'Arizona', 'woocommerce' ),
'AR' => __( 'Arkansas', 'woocommerce' ),
'CA' => __( 'California', 'woocommerce' ),
'CO' => __( 'Colorado', 'woocommerce' ),
'CT' => __( 'Connecticut', 'woocommerce' ),
'DE' => __( 'Delaware', 'woocommerce' ),
'DC' => __( 'District Of Columbia', 'woocommerce' ),
'FL' => __( 'Florida', 'woocommerce' ),
'GA' => _x( 'Georgia', 'US state of Georgia', 'woocommerce' ),
// 'HI' => __( 'Hawaii', 'woocommerce' ),
'ID' => __( 'Idaho', 'woocommerce' ),
'IL' => __( 'Illinois', 'woocommerce' ),
'IN' => __( 'Indiana', 'woocommerce' ),
'IA' => __( 'Iowa', 'woocommerce' ),
'KS' => __( 'Kansas', 'woocommerce' ),
'KY' => __( 'Kentucky', 'woocommerce' ),
'LA' => __( 'Louisiana', 'woocommerce' ),
'ME' => __( 'Maine', 'woocommerce' ),
'MD' => __( 'Maryland', 'woocommerce' ),
'MA' => __( 'Massachusetts', 'woocommerce' ),
'MI' => __( 'Michigan', 'woocommerce' ),
'MN' => __( 'Minnesota', 'woocommerce' ),
'MS' => __( 'Mississippi', 'woocommerce' ),
'MO' => __( 'Missouri', 'woocommerce' ),
'MT' => __( 'Montana', 'woocommerce' ),
'NE' => __( 'Nebraska', 'woocommerce' ),
'NV' => __( 'Nevada', 'woocommerce' ),
'NH' => __( 'New Hampshire', 'woocommerce' ),
'NJ' => __( 'New Jersey', 'woocommerce' ),
'NM' => __( 'New Mexico', 'woocommerce' ),
'NY' => __( 'New York', 'woocommerce' ),
'NC' => __( 'North Carolina', 'woocommerce' ),
'ND' => __( 'North Dakota', 'woocommerce' ),
'OH' => __( 'Ohio', 'woocommerce' ),
'OK' => __( 'Oklahoma', 'woocommerce' ),
'OR' => __( 'Oregon', 'woocommerce' ),
'PA' => __( 'Pennsylvania', 'woocommerce' ),
'RI' => __( 'Rhode Island', 'woocommerce' ),
'SC' => __( 'South Carolina', 'woocommerce' ),
'SD' => __( 'South Dakota', 'woocommerce' ),
'TN' => __( 'Tennessee', 'woocommerce' ),
'TX' => __( 'Texas', 'woocommerce' ),
'UT' => __( 'Utah', 'woocommerce' ),
'VT' => __( 'Vermont', 'woocommerce' ),
'VA' => __( 'Virginia', 'woocommerce' ),
'WA' => __( 'Washington', 'woocommerce' ),
'WV' => __( 'West Virginia', 'woocommerce' ),
'WI' => __( 'Wisconsin', 'woocommerce' ),
'WY' => __( 'Wyoming', 'woocommerce' ),
// 'AA' => __( 'Armed Forces (AA)', 'woocommerce' ),
// 'AE' => __( 'Armed Forces (AE)', 'woocommerce' ),
// 'AP' => __( 'Armed Forces (AP)', 'woocommerce' ),
// 'AS' => __( 'American Samoa', 'woocommerce' ),
// 'GU' => __( 'Guam', 'woocommerce' ),
// 'MP' => __( 'Northern Mariana Islands', 'woocommerce' ),
// 'PR' => __( 'Puerto Rico', 'woocommerce' ),
// 'UM' => __( 'US Minor Outlying Islands', 'woocommerce' ),
// 'VI' => __( 'US Virgin Islands', 'woocommerce' ),
);
return $states;
}
/* Makes "create an account" default as checked */
// add_filter('woocommerce_create_account_default_checked' , function ($checked){
// return true;
// });
/* This changes the header on the Order Confirm page */
add_filter('cs_match_header_assignment', 'custom_thankyou_header');
function custom_thankyou_header($match) {
if (is_page(154) ) {
$match = 90; // the post ID for your header
}
return $match;
}
/* This changes the footer on the Order Confirm page */
add_filter('cs_match_footer_assignment', 'custom_thankyou_footer');
function custom_thankyou_footer($match) {
if (is_page(154) ) {
$match = 213; // the post ID for your header
}
return $match;
}
/* This changes the number sequence of orders */
add_filter( 'woocommerce_order_number', 'change_woocommerce_order_number' );
function change_woocommerce_order_number( $order_id ) {
$prefix = 'HE20';
$suffix = '';
$new_order_id = $prefix . $order_id . $suffix;
return $new_order_id;
}
add_filter( 'mc4wp_integration_woocommerce_data', function( $data ) {
// Grab MailChimp field values from the current request
$data['FNAME'] = sanitize_text_field( $_POST['billing_first_name'] );
$data['LNAME'] = sanitize_text_field( $_POST['billing_last_name'] );
// Return the fields so the plugin knows to send them to MailChimp
return $data;
});
/* Auto-checks the clickwrap at the bottom of cart. Deactivated */
// add_filter( 'woocommerce_terms_is_checked_default', '__return_true' );
/**
* @snippet Add First & Last Name to My Account Register Form - WooCommerce
* @how-to Watch tutorial @ https://businessbloomer.com/?p=19055
* @sourcecode https://businessbloomer.com/?p=21974
* @author Rodolfo Melogli
* @credits Claudio SM Web
* @compatible WC 3.4
*/
///////////////////////////////
// 1. ADD FIELDS
add_action( 'woocommerce_register_form_start', 'bbloomer_add_name_woo_account_registration' );
function bbloomer_add_name_woo_account_registration() {
?>
<p class="form-row form-row-first">
<label for="reg_billing_first_name"><?php _e( 'First name', 'woocommerce' ); ?> <span class="required">*</span></label>
<input type="text" class="input-text" name="billing_first_name" id="reg_billing_first_name" value="<?php if ( ! empty( $_POST['billing_first_name'] ) ) esc_attr_e( $_POST['billing_first_name'] ); ?>" />
</p>
<p class="form-row form-row-last">
<label for="reg_billing_last_name"><?php _e( 'Last name', 'woocommerce' ); ?> <span class="required">*</span></label>
<input type="text" class="input-text" name="billing_last_name" id="reg_billing_last_name" value="<?php if ( ! empty( $_POST['billing_last_name'] ) ) esc_attr_e( $_POST['billing_last_name'] ); ?>" />
</p>
<div class="clear"></div>
<?php
}
///////////////////////////////
// 2. VALIDATE FIELDS
add_filter( 'woocommerce_registration_errors', 'bbloomer_validate_name_fields', 10, 3 );
function bbloomer_validate_name_fields( $errors, $username, $email ) {
if ( isset( $_POST['billing_first_name'] ) && empty( $_POST['billing_first_name'] ) ) {
$errors->add( 'billing_first_name_error', __( '<strong>Error</strong>: First name is required!', 'woocommerce' ) );
}
if ( isset( $_POST['billing_last_name'] ) && empty( $_POST['billing_last_name'] ) ) {
$errors->add( 'billing_last_name_error', __( '<strong>Error</strong>: Last name is required!.', 'woocommerce' ) );
}
return $errors;
}
///////////////////////////////
// 3. SAVE FIELDS
add_action( 'woocommerce_created_customer', 'bbloomer_save_name_fields' );
function bbloomer_save_name_fields( $customer_id ) {
if ( isset( $_POST['billing_first_name'] ) ) {
update_user_meta( $customer_id, 'billing_first_name', sanitize_text_field( $_POST['billing_first_name'] ) );
update_user_meta( $customer_id, 'first_name', sanitize_text_field($_POST['billing_first_name']) );
update_user_meta( $customer_id, 'FNAME', sanitize_text_field($_POST['billing_first_name']) );
}
if ( isset( $_POST['billing_last_name'] ) ) {
update_user_meta( $customer_id, 'billing_last_name', sanitize_text_field( $_POST['billing_last_name'] ) );
update_user_meta( $customer_id, 'last_name', sanitize_text_field($_POST['billing_last_name']) );
}
add_filter( 'mc4wp_integration_woocommerce_data', function( $data ) {
// Grab MailChimp field values from the current request
$data['FNAME'] = sanitize_text_field( $_POST['billing_first_name'] );
$data['LNAME'] = sanitize_text_field( $_POST['billing_last_name'] );
// Return the fields so the plugin knows to send them to MailChimp
return $data;
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment