Skip to content

Instantly share code, notes, and snippets.

@JeroenSormani
Last active May 1, 2019 00:50
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 JeroenSormani/e3108b66c53485e6a8c9 to your computer and use it in GitHub Desktop.
Save JeroenSormani/e3108b66c53485e6a8c9 to your computer and use it in GitHub Desktop.
WAS Shipping fields conditions
<?php
/* Match shipping fields */
/**
* WAS all checkout fields
*/
function was_conditions_add_shipping_fields( $conditions ) {
$shipping_fields = WC()->checkout()->checkout_fields['shipping'];
foreach ( $shipping_fields as $key => $values ) :
if ( isset( $values['label'] ) ) :
$conditions['Shipping Fields'][ $key ] = $values['label'];
endif;
endforeach;
return $conditions;
}
add_filter( 'was_conditions', 'was_conditions_add_shipping_fields', 10, 1 );
/**
* Add shipping fields value fields
*/
function was_values_add_shipping_fields( $values, $condition ) {
$shipping_fields = WC()->checkout()->checkout_fields['shipping'];
foreach ( $shipping_fields as $key => $field_values ) :
if ( $condition == $key ) :
if ( isset( $field_values['type'] ) && $field_values['type'] == 'select' ) :
$values['field'] = 'select';
$values['options'] = $field_values['options'];
else :
$values['field'] = 'text';
endif;
endif;
endforeach;
return $values;
}
add_filter( 'was_values', 'was_values_add_shipping_fields', 10, 2 );
/**
* Must match given shipping field.
*
* @param bool $match
* @param string $operator
* @param mixed $value
* @param array $package
* @return bool
*/
function was_match_condition_shipping_fields( $match, $operator, $value, $package ) {
if ( ! isset( $_POST['post_data'] ) ) :
return $match;
endif;
parse_str( $_POST['post_data'], $post_data );
$condition = str_replace( 'was_match_condition_', '', current_filter() );
if ( ! isset( $post_data[ $condition ] ) ) :
return $match;
endif;
$posted_value = $post_data[ $condition ];
if ( '==' == $operator ) :
$match = ( $posted_value == $value );
elseif ( '!=' == $operator ) :
$match = ( $posted_value != $value );
elseif ( '>=' == $operator ) :
$match = ( $posted_value >= $value );
elseif ( '<=' == $operator ) :
$match = ( $posted_value <= $value );
endif;
return $match;
}
// Add all shipping fields matching functions
function was_add_shipping_field_condition_matches() {
if ( ! is_callable( array( WC(), 'checkout' ) ) ) :
return;
endif;
$shipping_fields = WC()->checkout()->checkout_fields['shipping'];
foreach ( $shipping_fields as $key => $field_values ) :
add_action( 'was_match_condition_' . $key, 'was_match_condition_shipping_fields', 10, 4 );
endforeach;
}
add_action( 'init', 'was_add_shipping_field_condition_matches' );
@renzocj
Copy link

renzocj commented May 1, 2019

Hi Jeroen, I was trying your code but this line of code is giving me NULL's.

$shipping_fields = WC()->checkout()->checkout_fields['shipping'];

I was trying with this but still not having the array, any ideas?

$newObj = new WC_Checkout();
$shipping_fields = $newObj->get_checkout_fields($fieldset = 'shipping');

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment