Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save somewherewarm-snippets/c5699340f2b7d9010b2a33dfc05cb1d0 to your computer and use it in GitHub Desktop.
Save somewherewarm-snippets/c5699340f2b7d9010b2a33dfc05cb1d0 to your computer and use it in GitHub Desktop.
<?php
/**
* WC_CSP_Condition_Day_of_Week class -- This is an example of a custom condition.
*
* @author WooCommerce
* @package WooCommerce Conditional Shipping and Payments
*/
// Exit if accessed directly.
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/**
* Day of Week Condition.
*
* @class WC_CSP_Condition_Day_of_Week
*/
class WC_CSP_Condition_Day_of_Week extends WC_CSP_Package_Condition {
/**
* Constructor.
*/
public function __construct() {
$this->id = 'day_of_week';
$this->title = __( 'Day of Week', 'woocommerce-conditional-shipping-and-payments' );
// Here you can define in which Restrictions you'd like to add this custom condition.
$this->supported_global_restrictions = array( 'shipping_methods', 'shipping_countries' );
$this->supported_product_restrictions = array( 'shipping_methods', 'shipping_countries' );
}
/**
* Return condition field-specific resolution message which is combined along with others into a single restriction "resolution message".
*
* @param array $data Condition field data.
* @param array $args Optional arguments passed by restriction.
* @return string|false
*/
public function get_condition_resolution( $data, $args ) {
if ( empty( $data[ 'value' ] ) ) {
return false;
}
$message = false;
$weekdays = $this->get_weekdays();
$restricted_days = array();
foreach( $data[ 'value' ] as $index ) {
$restricted_days[] = $weekdays[ $index ];
}
if ( $this->modifier_is( $data[ 'modifier' ], array( 'is' ) ) ) {
$message = sprintf( __( 'make sure that you place your order on a day other than: %s', 'woocommerce-conditional-shipping-and-payments' ), implode( ',', $restricted_days ) );
} elseif ( $this->modifier_is( $data[ 'modifier' ], array( 'is-not' ) ) ) {
$message = sprintf( __( 'make sure that you place your order on one of these days: %s', 'woocommerce-conditional-shipping-and-payments' ), implode( ',', $restricted_days ) );
}
return $message;
}
/**
* Evaluate if the condition is in effect or not.
*
* @param array $data Condition field data.
* @param array $args Optional arguments passed by restriction.
* @return boolean
*/
public function check_condition( $data, $args ) {
if ( empty( $data[ 'value' ] ) ) {
return false;
}
$current_day = date( 'w' );
if ( 'is' === $data[ 'modifier' ] && in_array( $current_day, $data[ 'value' ] ) ) {
return true;
} elseif( 'is-not' === $data[ 'modifier' ] && ! in_array( $current_day, $data[ 'value' ] ) ) {
return true;
}
return false;
}
/**
* Validate, process and return condition fields.
*
* @param array $posted_condition_data
* @return array
*/
public function process_admin_fields( $posted_condition_data ) {
$processed_condition_data = array();
if ( isset( $posted_condition_data[ 'value' ] ) ) {
$processed_condition_data[ 'condition_id' ] = $this->id;
$processed_condition_data[ 'modifier' ] = stripslashes( $posted_condition_data[ 'modifier' ] );
$processed_condition_data[ 'value' ] = array_map( 'wc_clean', $posted_condition_data[ 'value' ] );
}
return $processed_condition_data;
}
/**
* Get day of week condition content for global restrictions.
*
* @param int $index
* @param int $condition_index
* @param array $condition_data
* @return str
*/
public function get_admin_fields_html( $index, $condition_index, $condition_data ) {
$modifier = '';
if ( ! empty( $condition_data[ 'modifier' ] ) ) {
$modifier = $condition_data[ 'modifier' ];
}
$weekdays = $this->get_weekdays();
$selected_days = isset( $condition_data[ 'value' ] ) ? $condition_data[ 'value' ] : array();
?>
<input type="hidden" name="restriction[<?php echo $index; ?>][conditions][<?php echo $condition_index; ?>][condition_id]" value="<?php echo $this->id; ?>" />
<div class="condition_row_inner">
<div class="condition_modifier">
<div class="sw-enhanced-select">
<select name="restriction[<?php echo $index; ?>][conditions][<?php echo $condition_index; ?>][modifier]">
<option value="is" <?php selected( $modifier, 'is', true ) ?>><?php echo __( 'is', 'woocommerce-conditional-shipping-and-payments' ); ?></option>
<option value="is-not" <?php selected( $modifier, 'is-not', true ) ?>><?php echo __( 'is not', 'woocommerce-conditional-shipping-and-payments' ); ?></option>
</select>
</div>
</div>
<div class="condition_value">
<select name="restriction[<?php echo $index; ?>][conditions][<?php echo $condition_index; ?>][value][]" class="multiselect sw-select2" multiple="multiple" data-placeholder="<?php _e( 'Select day of week&hellip;', 'woocommerce-conditional-shipping-and-payments' ); ?>">
<?php
foreach ( $weekdays as $value => $label ) {
echo '<option value="' . esc_attr( $value ) . '" ' . selected( in_array( $value, $selected_days ), true, false ).'>' . esc_html( $label ) . '</option>';
}
?>
</select>
</div>
</div><?php
}
/**
* Returns an array with the days of week.
* @return array
*/
public function get_weekdays() {
return array( 'Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thrusday', 'Friday', 'Saturday' );
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment