我完全是一个尝试完成这项任务的乞丐。我从查询中得到一个id列表1,2,3,4 ...然后我需要SUM一个名为“total”的列的值,其中id包含列出的id之一。
// Add "Customer type" checkout field (For testing) - To be removed if you got it
add_filter( 'woocommerce_checkout_fields', 'add_custom_checkout_fields', 20, 1 );
function add_custom_checkout_fields( $fields ) {
// Get the "Customer type" if user is logged in
if(is_user_logged_in() )
$value = get_user_meta( get_current_user_id(), 'customer_type', true );
$fields['billing']['customer_type'] = array(
'type' => 'select',
'label' => __('Customer type', 'woocommerce'),
'options' => array(
'' => __('Please, select your type'),
'individual' => __('Individual'),
'business' => __('Business'),
),
'required' => true, // required
'class' => array('form-row-wide'),
'clear' => true,
);
// Set the "Customer type" if is not empty (from user meta data)
if( ! empty($value) )
$fields['billing']['customer_type']['default'] = $value;
return $fields;
}
// Enabling Eu Vat for ('DK' and 'FI') when cart amount is up to 500
add_filter( 'woocommerce_eu_vat_number_country_codes', 'woo_custom_eu_vat_number_country_codes', 20, 1 );
function woo_custom_eu_vat_number_country_codes( $vat_countries ) {
// HERE below your settings
$countries = array( 'DK', 'FI' );
$min_amount = 500;
$cart_items_amount = WC()->cart->cart_contents_total;
// Avoiding errors on admin and on other pages
if( is_admin() || WC()->cart->is_empty() )
return $countries;
// Show EU VAT field for cart amount up to 500 & users in Denmark and Finland
return $cart_items_amount >= $min_amount ? $countries : array();
}
add_action( 'wp_footer', 'custom_checkout_jquery_script', 30, 1 );
function custom_checkout_jquery_script( $checkout ) {
if( !is_checkout() ) return; // Only checkout
?>
<script type="text/javascript">
(function($){
var a = 'select[name=customer_type]',
b = 'business',
i = 'individual',
bc = '#billing_company_field',
lbc = 'label[for=billing_company]',
lr = lbc + ' > .required',
r = '<abbr class="required" title="required">*</abbr>',
vat = '#vat_number_field';
// On start (once DOM is loaded)
$('label[for=vat_number]').append(r); // Mark Eu Vat required
// Hide EU VAT if not business and other needed things
if( b != $(a).val() && $(vat).length ) {
$(vat).fadeOut('fast'); // Hide EU Vat field
// If is an individual we hide company field
if( i == $(a).val())
$(bc).fadeOut(); // Hide company
// Mark company field as required
} else if( b == $(a).val() && $(vat).length ) {
$(lbc).append(r); // Company required
}
// On "Customer Type" live event
$('form.checkout').on('change', a, function(e){
e.preventDefault();
// Show EU VAT and company For "business" with other needed things
if( b == $(a).val() ){
if( $(vat).length )
$(vat).fadeIn(); // Show EU Vat field
$(lbc).append(r); // Company required
$(bc).fadeIn(); // Show Company
} else if( i == $(a).val()) { // For "individual"
if( $(vat).length )
$(vat).fadeOut(); // Hide EU Vat field
$(lr).remove(); // Remove Company required
$(bc).fadeOut(); // Hide Company
} else { // Nothing selected
if( $(vat).length )
$(vat).fadeOut(); // Hide EU Vat field
$(lr).remove(); // Remove Company required
$(bc).fadeIn(); // Show Company
}
});
})(jQuery);
</script>
<?php
}
// Update Order and User meta data for "Customer Type"
add_action('woocommerce_checkout_create_order', 'before_checkout_create_order', 20, 2);
function before_checkout_create_order( $order, $data ) {
// Set customer type in the order and in user_data
if( ! empty($_POST['customer_type']) ){
// Update Order meta data for 'customer_type'
$order->update_meta_data( '_customer_type', sanitize_key( $_POST['customer_type'] ) );
// Update User meta data for 'customer_type'
if( $order->get_user_id() > 0 )
update_user_meta( $order->get_user_id(), 'customer_type', sanitize_key( $_POST['customer_type'] ) );
}
}