- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我已将此代码添加到functions.php
// Hook in
add_filter( 'woocommerce_checkout_fields' , 'custom_override_checkout_fields' );
add_filter( 'woocommerce_ship_to_different_address_checked', '__return_false' );
add_action( 'woocommerce_before_order_notes', 'competitor_details' );
add_action('woocommerce_checkout_process', 'my_custom_checkout_field_process');
// Our hooked in function - $fields is passed via the filter!
function custom_override_checkout_fields( $fields ) {
unset($fields['order']['order_comments']);
return $fields;
}
function competitor_details( $checkout ) {
echo '<div id="competitor_details"><h3>' . __('Competitor\'s Details') . '</h3>';
woocommerce_form_field( 'competitor_first_name',array('label' => 'First name', 'type' => 'text', 'required' => 'true')
, $checkout->get_value( 'my_field_name' ));
woocommerce_form_field( 'competitor_last_name',array('label' => 'Last name', 'type' => 'text', 'required' => 'true')
, $checkout->get_value( 'my_field_name' ));
echo '<div id="competitor_details"><h4>' . __('Date of birth') . '</h4>';
woocommerce_form_field( 'competitor_dob_day',array('label' => 'Day', 'type' => 'select', 'required' => 'true', 'options' => array(''=>'','1'=>'1','2'=>'2','3'=>'3','4'=>'4','5'=>'5','6'=>'6','7'=>'7','8'=>'8','9'=>'9','10'=>'10','11'=>'11','12'=>'12','13'=>'13','14'=>'14','15'=>'15','16'=>'16','17'=>'17','18'=>'18','19'=>'19','20'=>'20','21'=>'21','22'=>'22','23'=>'23','24'=>'24','25'=>'25','26'=>'26','27'=>'27','28'=>'28','29'=>'29','30'=>'30','31'=>'31'))
, $checkout->get_value( 'my_field_name' ));
woocommerce_form_field( 'competitor_dob_month',array('label' => 'Month', 'type' => 'select', 'required' => 'true', 'options' => array(''=>'','Jan'=>'Jan','Feb'=>'Feb','Mar'=>'Mar','Apr'=>'Apr','May'=>'May','Jun'=>'Jun','Jul'=>'Jul','Aug'=>'Aug','Sep'=>'Sep','Oct'=>'Oct','Nov'=>'Nov','Dec'=>'Dec'))
, $checkout->get_value( 'my_field_name' ));
woocommerce_form_field( 'competitor_dob_year',array('label' => 'Year', 'type' => 'text', 'required' => 'true')
, $checkout->get_value( 'my_field_name' ));
echo '<div id="consent_to_rules"><h4>' . __('I have read the Rules and agree to abide by them') . '</h4>';
woocommerce_form_field( 'consent_to_rules',array('label' => 'YES, I agree', 'type' => 'checkbox', 'required' => 'true')
, $checkout->get_value( 'my_field_name' ));
echo '</div>';
}
function my_custom_checkout_field_process() {
// Check if set, if its not set add an error.
if ( ! $_POST['competitor_first_name'] )
wc_add_notice( __( 'Please enter Competitors First Name' ), 'error' );
if ( ! $_POST['competitor_last_name'] )
wc_add_notice( __( 'Please enter Competitors Last Name' ), 'error' );
if ( ! $_POST['competitor_dob_day'] )
wc_add_notice( __( 'Please enter Competitors date of birth' ), 'error' );
if ( ! $_POST['competitor_dob_month'] )
wc_add_notice( __( 'Please enter Competitors date of birth' ), 'error' );
if ( ! $_POST['competitor_dob_year'] )
wc_add_notice( __( 'Please enter Competitors date of birth' ), 'error' );
if ( ! $_POST['consent_to_rules'] )
wc_add_notice( __( 'Please agree that you have read the Rules' ), 'error' );
}
最佳答案
首先,我重写了您当前的代码,因为它有一些小错误
// Unset order comments field
function filter_woocommerce_checkout_fields( $fields ) {
unset( $fields['order']['order_comments'] );
return $fields;
}
add_filter( 'woocommerce_checkout_fields' , 'filter_woocommerce_checkout_fields', 10, 1 );
// NOT checked by default
add_filter( 'woocommerce_ship_to_different_address_checked', '__return_false' );
// Add custom fields
function action_woocommerce_before_order_notes( $checkout ) {
echo '<h3>' . __( 'Competitor\'s Details', 'woocommerce' ) . '</h3>';
// First name
woocommerce_form_field( 'competitor_first_name', array(
'label' => __( 'First name', 'woocommerce' ),
'type' => 'text',
'required' => 'true'
), $checkout->get_value( 'competitor_first_name' ) );
// Last name
woocommerce_form_field( 'competitor_last_name', array(
'label' => __( 'Last name', 'woocommerce' ),
'type' => 'text',
'required' => 'true'
), $checkout->get_value( 'competitor_last_name' ) );
echo '<h4>' . __( 'Date of birth', 'woocommerce' ) . '</h4>';
// Options day
$options_day = array( '' => __( 'Please select a day', 'woocommerce' ) );
$from = 1;
$to = 31;
$options_day_combine = $options_day + array_combine( range( $from, $to ), range( $from, $to ) );
// Dob day
woocommerce_form_field( 'competitor_dob_day', array(
'label' => __( 'Day', 'woocommerce' ),
'type' => 'select',
'required' => 'true',
'options' => $options_day_combine
), $checkout->get_value( 'competitor_dob_day' ) );
// Dob month
woocommerce_form_field( 'competitor_dob_month', array(
'label' => __( 'Month', 'woocommerce' ),
'type' => 'select',
'required' => 'true',
'options' => array(
'' => __( 'Please select a month', 'woocommerce' ),
'Jan' => __( 'Jan', 'woocommerce' ),
'Feb' => __( 'Feb', 'woocommerce' ),
'Mar' => __( 'Mar', 'woocommerce' ),
'Apr' => __( 'Apr', 'woocommerce' ),
'May' => __( 'May', 'woocommerce' ),
'Jun' => __( 'Jun', 'woocommerce' ),
'Jul' => __( 'Jul', 'woocommerce' ),
'Aug' => __( 'Aug', 'woocommerce' ),
'Sep' => __( 'Sep', 'woocommerce' ),
'Oct' => __( 'Oct', 'woocommerce' ),
'Nov' => __( 'Nov', 'woocommerce' ),
'Dec' => __( 'Dec', 'woocommerce' ),
)
), $checkout->get_value( 'competitor_dob_month' ) );
// Options year
$options_year = array( '' => __( 'Please select a year', 'woocommerce' ) );
$from = 2021;
$to = 1910;
$options_year_combine = $options_year + array_combine( range( $from, $to ), range( $from, $to ) );
// Dob year
woocommerce_form_field( 'competitor_dob_year', array(
'label' => __( 'Year', 'woocommerce' ),
'type' => 'select',
'required' => 'true',
'options' => $options_year_combine
), $checkout->get_value( 'competitor_dob_year' ) );
echo '<h4>' . __( 'I have read the Rules and agree to abide by them', 'woocommerce' ) . '</h4>';
// Rules
woocommerce_form_field( 'consent_to_rules', array(
'label' => __( 'Yes, I agree', 'woocommerce' ),
'type' => 'checkbox',
'required' => 'true'
), $checkout->get_value( 'consent_to_rules' ) );
}
add_action( 'woocommerce_before_order_notes', 'action_woocommerce_before_order_notes', 10, 1 );
// Validate custom fields
function action_woocommerce_checkout_process() {
// Isset and empty, add an error
if ( isset( $_POST['competitor_first_name'] ) && empty( $_POST['competitor_first_name'] ) ) {
wc_add_notice( __( 'Please enter Competitors First Name', 'woocommerce' ), 'error' );
}
if ( isset( $_POST['competitor_last_name'] ) && empty( $_POST['competitor_last_name'] ) ) {
wc_add_notice( __( 'Please enter Competitors Last Name', 'woocommerce' ), 'error' );
}
if ( isset( $_POST['competitor_dob_day'] ) && empty( $_POST['competitor_dob_day'] ) ) {
wc_add_notice( __( 'Please enter Competitors date of birth - day', 'woocommerce' ), 'error' );
}
if ( isset( $_POST['competitor_dob_month'] ) && empty( $_POST['competitor_dob_month'] ) ) {
wc_add_notice( __( 'Please enter Competitors date of birth - month', 'woocommerce' ), 'error' );
}
if ( isset( $_POST['competitor_dob_year'] ) && empty( $_POST['competitor_dob_year'] ) ) {
wc_add_notice( __( 'Please enter Competitors date of birth - year', 'woocommerce' ), 'error' );
}
if ( ! isset( $_POST['consent_to_rules'] ) ) {
wc_add_notice( __( 'Please agree that you have read the Rules', 'woocommerce' ), 'error' );
}
}
add_action( 'woocommerce_checkout_process', 'action_woocommerce_checkout_process', 10, 0 );
woocommerce_checkout_create_order
和
woocommerce_admin_order_data_after_billing_address
Action Hook 。
// Save fields
function action_woocommerce_checkout_create_order( $order, $data ) {
// Isset and NOT empty, save
if ( isset( $_POST['competitor_first_name'] ) && ! empty( $_POST['competitor_first_name'] ) ) {
// Update meta data
$order->update_meta_data( 'competitor_first_name', sanitize_text_field( $_POST['competitor_first_name'] ) );
}
if ( isset( $_POST['competitor_last_name'] ) && ! empty( $_POST['competitor_last_name'] ) ) {
// Update meta data
$order->update_meta_data( 'competitor_last_name', sanitize_text_field( $_POST['competitor_last_name'] ) );
}
if ( isset( $_POST['competitor_dob_day'] ) && ! empty( $_POST['competitor_dob_day'] ) ) {
// Update meta data
$order->update_meta_data( 'competitor_dob_day', sanitize_text_field( $_POST['competitor_dob_day'] ) );
}
if ( isset( $_POST['competitor_dob_month'] ) && ! empty( $_POST['competitor_dob_month'] ) ) {
// Update meta data
$order->update_meta_data( 'competitor_dob_month', sanitize_text_field( $_POST['competitor_dob_month'] ) );
}
if ( isset( $_POST['competitor_dob_year'] ) && ! empty( $_POST['competitor_dob_year'] ) ) {
// Update meta data
$order->update_meta_data( 'competitor_dob_year', sanitize_text_field( $_POST['competitor_dob_year'] ) );
}
if ( isset( $_POST['consent_to_rules'] ) ) {
// Update meta data
$order->update_meta_data( 'consent_to_rules', sanitize_text_field( $_POST['consent_to_rules'] ) );
}
}
add_action( 'woocommerce_checkout_create_order', 'action_woocommerce_checkout_create_order', 10, 2 );
// Display the custom 'select' field value on admin order pages after billing adress
function action_woocommerce_admin_order_data_after_billing_address( $order ) {
// Get meta
$competitor_first_name = $order->get_meta( 'competitor_first_name' );
// NOT empty
if ( ! empty ( $competitor_first_name ) ) {
echo '<p><strong>' . __( 'Competitor first name', 'woocommerce' ) . ':</strong> ' . $competitor_first_name . '</p>';
}
// Get meta
$competitor_last_name = $order->get_meta( 'competitor_last_name' );
// NOT empty
if ( ! empty ( $competitor_last_name ) ) {
echo '<p><strong>' . __( 'Competitor last name', 'woocommerce' ) . ':</strong> ' . $competitor_last_name . '</p>';
}
// Get meta
$competitor_dob_day = $order->get_meta( 'competitor_dob_day' );
// NOT empty
if ( ! empty ( $competitor_dob_day ) ) {
echo '<p><strong>' . __( 'Competitor dob day', 'woocommerce' ) . ':</strong> ' . $competitor_dob_day . '</p>';
}
// Get meta
$competitor_dob_month = $order->get_meta( 'competitor_dob_month' );
// NOT empty
if ( ! empty ( $competitor_dob_month ) ) {
echo '<p><strong>' . __( 'Competitor dob month', 'woocommerce' ) . ':</strong> ' . $competitor_dob_month . '</p>';
}
// Get meta
$competitor_dob_year = $order->get_meta( 'competitor_dob_year' );
// NOT empty
if ( ! empty ( $competitor_dob_year ) ) {
echo '<p><strong>' . __( 'Competitor dob year', 'woocommerce' ) . ':</strong> ' . $competitor_dob_year . '</p>';
}
// Get meta
$consent_to_rules = $order->get_meta( 'consent_to_rules' );
// NOT empty
if ( ! empty ( $consent_to_rules ) ) {
echo '<p><strong>' . __( 'Yes, I agree', 'woocommerce' ) . '</strong></p>';
}
}
add_action( 'woocommerce_admin_order_data_after_billing_address', 'action_woocommerce_admin_order_data_after_billing_address', 10, 1 );
关于wordpress - 在 WooCommerce 管理订单编辑页面中显示自定义 DOB 字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67871158/
[编辑:在 functions.php 中添加代码并省略代码 WC 文件中的更改时,它实际上起作用了。重要提示:它仅在 ONE 属性存在时有效。但是,当有 2 个属性(例如尺寸和颜色)时,它就不起作用
我使用以下方法获取购物车中产品的版本ID。 $cartdetails = $woocommerce->cart->get_cart(); foreach($cartdetails as $ca
我目前使用 $order = new WC_Order($order_id); $order->update_status('completed', 'Order has been delivered
您好,我无法从 woocommerce 订单送货地址获取,我收到错误: fatal error :在 .. 中的非对象上调用成员函数 get_formatted_address() 我正在使用代码:
我不是在谈论向产品添加属性,而是我想添加属性本身.. 您可以从“产品”->“属性”下的 UI 执行此操作,但从代码中它是如何完成的(例如:要调用哪些函数或要更新的表)? 最佳答案 属性只是一个自定义分
是否可以使产品变化来改变价格? 例如,我有一个基本产品及其变体: 数量 -10 -20 -50 颜色 - 蓝色 -红色 项目 -是的 -没有 我有数量的价格,不管是什么颜色。但是,如果您选择“项目-否
有没有办法将其他收件人添加到 woocommerce 发票邮件中。我尝试使用“woocommerce_email_headers” Hook ,但它不起作用: add_filter( 'woocomm
我不是在谈论向产品添加属性,而是我想添加属性本身.. 您可以从“产品”->“属性”下的 UI 执行此操作,但从代码中它是如何完成的(例如:要调用哪些函数或要更新的表)? 最佳答案 属性只是一个自定义分
只是想看看是否有办法为我的送货选项添加工具提示或弹出窗口,以便在客户悬停或点击(通过移动设备)时更详细地向客户解释此选项的含义? 最佳答案 简单的解决方案是通过将以下内容添加到您的 functions
如何获取 woocommerce 中的类别列表?使用这段代码,我得到了 wordpress 类别列表: function gaga_lite_category_lists(){ $catego
我一直试图在 WooCommerce 上禁用产品价格四舍五入,但没有成功。即使我在 WooCommerce > Settings > General > Number of decimals 上将 d
我一直在尝试定制 woocommerce 中的相关产品。我使用 woocommerce_output_related_products_args 添加 meta_query 选项,但它们没有效果,这是
由于 WooCommerce 2.1 页面(例如订单接收)已被删除并替换为 WC 端点。我的结帐页面有一个自定义页面模板 (page-checkout.php),现在所有结帐端点也都在使用这个自定义页
我正在向 WooCommerce 的结帐页面添加额外的字段, 我已经添加了基本的文本字段,但我想要一个带有几个选项的下拉列表或选择框, 这是我到目前为止所做的,但我在某处犯了错误 $fields['b
我试图在我的 invoice.php 中拼出 WooCommerce 订单总额模板文件可以达到订单总金额。 首先我试过: $total = $order->get_total(); - format
我需要在购物车页面和结帐页面中显示的价格(小计、总计...)后添加文字提及。 我知道如何在单个页面和商店页面上执行此操作,但不知道如何在其他页面上执行此操作。我唯一找到的就是这段代码,但它不起作用。
在 Woocommerce 中,在管理产品列表中,我应该需要为特定属性过滤产品。 根据“Add a filter dropdown for product tags in woocommerce ad
Woocommerce 有一个带有“woocommerce”类的 div 我想添加另一个类或删除该类。那是哪个文件? 最佳答案 虽然 WooCommerce 没有提供任何支持的方法来实现这一点
您好,目前我想获取 woocommerce 中的税率名称。我有什么办法可以得到这张图中圈出的值:/image/pyGAH.png 我当前的代码: $items = $order->get_items(
在 Woocommerce 中,我尝试显示可变产品的销售日期。 我找到了a code ,如果我将此代码准确地发布在functions.php中,它不会执行任何操作。我尝试修改它,因为我只需要在起始页上
我是一名优秀的程序员,十分优秀!