Created
August 1, 2019 17:02
-
-
Save CrispDev/8cf27fb0a32cfc3760479bb51586bc63 to your computer and use it in GitHub Desktop.
Autoship Customization // Toggle an Orders Autoship Scheduled Items based on Shipping Address in Checkout
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Checks if the supplied address is a valid autoship address. | |
* @param array $address | |
*/ | |
function xx_autoship_valid_shipping_address( $address ){ | |
// Add Any Address Checks to filter here ( i.e. valid city, state, zip, country ) | |
return apply_filters( 'xx_autoship_valid_shipping_address', 'US' == $address['shipping_country'], $address ); | |
} | |
/** | |
* Toggle the Autoship Settings for any item in cart based on toggle flag. | |
* also store original options to apply after toggle. | |
* | |
* @param bool $toggle_off If true remove autoship details. | |
*/ | |
function xx_toggle_autoship_schedules_items_in_cart( $toggle_off = true ){ | |
$adjusted = false; | |
foreach ( WC()->cart->cart_contents as $cart_item_id => $cart_item ) { | |
if ( $toggle_off ){ | |
$cart_item['original_autoship_frequency'] = NULL; | |
$cart_item['original_autoship_frequency_type'] = NULL; | |
$cart_item['original_autoship_price'] = NULL; | |
// Remove Autoship Schedule & store in case of change | |
if ( isset( $cart_item['autoship_frequency_type'] ) && !empty( $cart_item['autoship_frequency_type'] ) ){ | |
$cart_item['original_autoship_frequency'] = $cart_item['autoship_frequency']; | |
$cart_item['original_autoship_frequency_type'] = $cart_item['autoship_frequency_type']; | |
// The instance of the WC_Product Object | |
$product = $cart_item['data']; | |
$product_id = $product->get_id(); // The product ID | |
// Get the original Autoship Price | |
$cart_item['original_autoship_price'] = | |
autoship_get_product_checkout_price( $product_id, $cart_item['autoship_frequency_type'], $cart_item['autoship_frequency'] ); | |
// Reset Autoship Options | |
$cart_item['autoship_frequency'] = NULL; | |
$cart_item['autoship_frequency_type'] = NULL; | |
// Get the Product Price | |
$price = $product->get_price(); | |
// Set the new cart item price | |
$product->set_price( $price ); | |
// Update Cart Item | |
WC()->cart->cart_contents[$cart_item_id] = $cart_item; | |
$adjusted = true; | |
} | |
// Only toggle on for toggle off'd items - prevents initial page load toggle | |
} else if ( isset( $cart_item['original_autoship_frequency_type'] ) && !empty( $cart_item['original_autoship_frequency_type'] ) ){ | |
$cart_item['autoship_frequency'] = $cart_item['original_autoship_frequency']; | |
$cart_item['autoship_frequency_type'] = $cart_item['original_autoship_frequency_type']; | |
// The instance of the WC_Product Object | |
$product = $cart_item['data']; | |
$product_id = $product->get_id(); // The product ID | |
$cart_item['autoship_price'] = | |
autoship_get_product_checkout_price( $product_id, $cart_item['autoship_frequency_type'], $cart_item['autoship_frequency'] ); | |
// Reset Originals | |
$cart_item['original_autoship_frequency'] = NULL; | |
$cart_item['original_autoship_frequency_type'] = NULL; | |
// Set the new cart item price | |
$product->set_price( $cart_item['autoship_price'] ); | |
// Update the Cart item | |
WC()->cart->cart_contents[$cart_item_id] = $cart_item; | |
$adjusted = true; | |
} | |
} | |
// Update Cart and Totals | |
WC()->cart->set_session(); | |
WC()->cart->calculate_totals(); | |
// If adjustment was made display notice | |
if ( $adjusted ) { | |
$notice = array( | |
'message' => $toggle_off ? | |
__( 'Sorry but Autoship is not available for the selected Shipping country. Please select a different Shipping address.' ) : | |
__( 'Autoship is available for the selected Shipping country. Your order has been update.' ), | |
'status' => $toggle_off ? | |
'error' : 'success', | |
); | |
$notice = apply_filters( 'xx_toggle_autoship_schedules_items_in_cart_notice', $notice, $toggle_off ); | |
wc_add_notice( $notice['message'], $notice['status']); | |
} | |
} | |
/** | |
* Adjust Autoship Scheduled Items based on Shipping County | |
* If the plugin entered unit of measure does not match a valid type we ignore. | |
* valid shipping countries can be modified via @hook xx_autoship_valid_shipping_countries | |
* @param array $data The checkout posted data. | |
*/ | |
function xx_autoship_check_shipping_address( $data ){ | |
if ( !is_checkout() ) | |
return; | |
// Get the shipping address values from POST since this hook happens prior to | |
// WC()->customer-set_props in update_order_review() | |
$shipping_address = array(); | |
if ( wc_ship_to_billing_address_only() ) { | |
$shipping_address = array( | |
'shipping_country' => isset( $_POST['country'] ) ? wc_clean( wp_unslash( $_POST['country'] ) ) : null, | |
'shipping_state' => isset( $_POST['state'] ) ? wc_clean( wp_unslash( $_POST['state'] ) ) : null, | |
'shipping_postcode' => isset( $_POST['postcode'] ) ? wc_clean( wp_unslash( $_POST['postcode'] ) ) : null, | |
'shipping_city' => isset( $_POST['city'] ) ? wc_clean( wp_unslash( $_POST['city'] ) ) : null, | |
'shipping_address_1' => isset( $_POST['address'] ) ? wc_clean( wp_unslash( $_POST['address'] ) ) : null, | |
'shipping_address_2' => isset( $_POST['address_2'] ) ? wc_clean( wp_unslash( $_POST['address_2'] ) ) : null, | |
); | |
} else { | |
$shipping_address = array( | |
'shipping_country' => isset( $_POST['s_country'] ) ? wc_clean( wp_unslash( $_POST['s_country'] ) ) : null, | |
'shipping_state' => isset( $_POST['s_state'] ) ? wc_clean( wp_unslash( $_POST['s_state'] ) ) : null, | |
'shipping_postcode' => isset( $_POST['s_postcode'] ) ? wc_clean( wp_unslash( $_POST['s_postcode'] ) ) : null, | |
'shipping_city' => isset( $_POST['s_city'] ) ? wc_clean( wp_unslash( $_POST['s_city'] ) ) : null, | |
'shipping_address_1' => isset( $_POST['s_address'] ) ? wc_clean( wp_unslash( $_POST['s_address'] ) ) : null, | |
'shipping_address_2' => isset( $_POST['s_address_2'] ) ? wc_clean( wp_unslash( $_POST['s_address_2'] ) ) : null, | |
); | |
} | |
// Check if shipping address is valid | |
$toggle_off = ! xx_autoship_valid_shipping_address( $shipping_address ); | |
// Toggle Autoship Items based on validation | |
xx_toggle_autoship_schedules_items_in_cart( $toggle_off ); | |
} | |
add_action( 'woocommerce_checkout_update_order_review', 'xx_autoship_check_shipping_address' ); |
Author
CrispDev
commented
Aug 1, 2019
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment