我刚刚使用以下代码创建了一个包含订单审核的页面
<form name="checkout" method="post" class="checkout woocommerce-checkout" action="<?php echo esc_url( $get_checkout_url ); ?>" enctype="multipart/form-data">
<h3 id="order_review_heading"><?php _e( 'Your order', 'woocommerce' ); ?></h3>
<?php do_action( 'woocommerce_checkout_before_order_review' ); ?>
<div id="order_review" class="woocommerce-checkout-review-order">
<?php do_action( 'woocommerce_checkout_order_review' ); ?>
</div>
<?php do_action( 'woocommerce_checkout_after_order_review' ); ?></div>
</form>
<?php do_action( 'woocommerce_after_checkout_form', $checkout ); ?>
</div>
我在function.php
中设置了默认值,如下所示:
function overridefields($fields)
{
global $wpdb;
session_start();
//print_r($_SESSION);
//$fields['billing']['billing_delivery_date'] = array('label'=>'Delivery Date','Placeholder'=>'Date','required'=>'false','readonly'=>'true');
$select_r = $wpdb->get_results("select * from `register` where `userid`='18'");
//print_r($select_r);
$fields['billing']['billing_first_name']['default']=$_SESSION['f_name'];
$fields['billing']['billing_last_name']['default']=$_SESSION['l_name'];
$fields['billing']['billing_address_1']['default']=$_SESSION['address'];
$fields['billing']['billing_address_2']['default']='';
$fields['billing']['billing_city']['default']=$_SESSION['city'];
//$fields['billing']['billing_state']['default']=$select_r[0]->state;
$fields['billing']['billing_email']['default']=$_SESSION['email'];
$fields['billing']['billing_phone']['default']=$_SESSION['phone'];
$fields['billing']['billing_postcode']['default']=$_SESSION['pincode'];
//$fields['billing']['billing_delivery_date']['default']=$_SESSION['delivdate'];
return $fields;
}
add_filter('woocommerce_checkout_fields','overridefields');
但是当我点击结帐按钮然后我收到了错误
Country is a required field.
First Name is a required field.
Last Name is a required field.
Address is a required field.
Town / City is a required field.
State / County is a required field.
Postcode / Zip is a required field.
Email Address is a required field.
Phone is a required field.
如何在woocommerce结帐页面设置默认值
答案 0 :(得分:7)
我发现这段代码可能很有用:
<?php
add_filter( 'woocommerce_checkout_fields' , 'default_values_checkout_fields' );
function default_values_checkout_fields( $fields ) {
// You can use this for postcode, address, company, first name, last name and such.
$fields['billing']['billing_city']['default'] = 'SomeCity';
$fields['shipping']['shipping_city']['default'] = 'SomeCity';
return $fields;
}
?>
然后使用CSS隐藏输入。
<强>参考强>
答案 1 :(得分:0)
在字段定义中使用default
和custom_attributes
将自定义属性应用于帐单/送货地址和帐户字段:
https://github.com/woocommerce/woocommerce/blob/master/includes/wc-template-functions.php#L2510-L2514
add_filter('woocommerce_checkout_fields', 'my_woocommerce_checkout_fields');
add_filter('woocommerce_billing_fields', 'my_woocommerce_billing_fields');
function my_woocommerce_checkout_fields($fields) {
$fields['billing'] = my_woocommerce_billing_fields($fields['billing']);
return $fields;
}
function my_woocommerce_billing_fields($fields) {
// Note: Default value is only used if the user account does
// not have any value for the meta field yet. (Empty value is
// also a value.) Ensure that the value is set correctly.
$fields['billing_city']['default'] = 'SomeCity';
// Disable the field in the form.
// (Note: Does not prevent different values from being posted.)
$fields['billing_city']['custom_attributes']['readonly'] = TRUE;
$fields['billing_city']['custom_attributes']['disabled'] = TRUE;
return $fields;
}