在Woocommerce中,我使用自定义字段来计算产品的价格,基于此线程代码:在Woocommerce 3中将产品自定义字段显示为订单商品.
// Add a custom field before single add to cart
add_action('woocommerce_before_add_to_cart_button', 'custom_product_price_field', 5);
function custom_product_price_field() {
echo '<div class="custom-text text">
<h3>Rental</h3>
<label>Start Date:</label>
<input type="date" name="rental_date" value="" class="rental_date" />
<label>Period Rental:</label>
<select name="custom_price" class="custom_price">
<option value="" selected="selected">choosen period</option>
<option value="2">2 days</option>
<option value="4">4 days</option>
</select>
</div>';
}
// Get custom field value, calculate new item price, save it as custom cart item data
add_filter('woocommerce_add_cart_item_data', 'add_custom_field_data', 20, 3);
function add_custom_field_data($cart_item_data, $product_id, $variation_id) {
if (isset($_POST['rental_date']) && !empty($_POST['rental_date'])) …Run Code Online (Sandbox Code Playgroud) 在选择交付方式并将这些字段放入 中后,我需要选择产品的交付时间和日期woocommerce_after_shipping_rate。
照我看来。客户选择送货方式,例如“快递”。
之后,会出现两个复选框:
尽快
交货日期。
如果客户选择“交货日期”,则会出现一个新字段,其中包含交货日期和时间。这里就是这样的
根据“在 WooCommerce Checkout 自定义文本字段中启用日期选择器”答案代码,我为交货日期和时间“尽快”和“交货日期”创建了其他字段。我从这里下载了 DateTimePicker 。
这是我的代码:
// Register main datetimepicker jQuery plugin script
add_action( 'wp_enqueue_scripts', 'enabling_date_time_picker' );
function enabling_date_time_picker() {
// Only on front-end and checkout page
if( is_admin() || ! is_checkout() ) return;
// Load the datetimepicker jQuery-ui plugin script
wp_enqueue_style( 'datetimepicker', get_stylesheet_directory_uri() . '/assets/css/jquery.datetimepicker.min.css', array());
wp_enqueue_script('datetimepicker', get_stylesheet_directory_uri() . '/js/jquery.datetimepicker.full.min.js', array());
}
// Call datetimepicker functionality in your custom text field …Run Code Online (Sandbox Code Playgroud) 基于“选择 WooCommerce 交付方式后选择日期和时间”回答代码,显示自定义取件字段和交付日期,以下代码在订单编辑页面上显示这些字段的交付数据。
这是我的代码:
// View fields in Edit Order Page
add_action( 'woocommerce_admin_order_data_after_billing_address', 'my_custom_fields_order_meta', 10, 1 );
function my_custom_fields_order_meta($order){
$delivery_option = $order->get_meta('_delivery_option');
if( $delivery_option == 'date' ) {
$delivery_datetime = $order->get_meta('_delivery_datetime');
echo '<p><strong>'.__('Delivery').':</strong> ' . get_post_meta( $order->id, '_delivery_option', true ) . '</p>';
echo '<p><strong>'.__('Delivery Date').':</strong> ' . get_post_meta( $order->id, '_delivery_datetime', true ) . '</p>';
}
}
Run Code Online (Sandbox Code Playgroud)
不幸的是,只有客户选择的交货日期才能正确显示,并且没有显示“尽快”单选按钮的选项。
显然,我做错了什么。
我还想在感谢页面和电子邮件中显示这些字段值。
任何帮助表示赞赏。
在 Woocommerce 中,我使用自定义字段来计算产品的价格,基于此代码 -在 Woocommerce 中将自定义字段添加到自定义产品计算价格。感谢 LoicTheAztec 的帮助。
// Add a custom field before single add to cart
add_action( 'woocommerce_before_add_to_cart_button', 'custom_product_price_field', 5 );
function custom_product_price_field(){
echo '<div class="custom-text text">
<h3>Rental</h3>
<label>Start Date:</label>
<input type="date" name="rental_date" value="" class="rental_date" />
<label>Period Rental:</label>
<select name="custom_price" class="custom_price">
<option value="30" selected="selected">2 days</option>
<option value="35">4 days</option>
</select>
</div>';
}
// Get custom field value, calculate new item price, save it as custom cart item data
add_filter('woocommerce_add_cart_item_data', 'add_custom_field_data', 20, 2 );
function add_custom_field_data( $cart_item_data, $product_id …Run Code Online (Sandbox Code Playgroud) 我有一个代码,显示产品编辑页面上的复选框。当我单击此复选框时,单个产品页面上将显示一个选择框。
这是我的代码:
// Display Checkbox Field
add_action('woocommerce_product_options_general_product_data', 'roast_custom_field_add');
function roast_custom_field_add(){
global $post;
// Checkbox
woocommerce_wp_checkbox(
array(
'id' => '_roast_checkbox',
'label' => __('Roast Level', 'woocommerce' ),
'description' => __( 'Enable roast level!', 'woocommerce' )
)
);
}
// Save Checkbox Field
add_action('woocommerce_process_product_meta', 'roast_custom_field_save');
function roast_custom_field_save($post_id){
// Custom Product Checkbox Field
$roast_checkbox = isset( $_POST['_roast_checkbox'] ) ? 'yes' : 'no';
update_post_meta($post_id, '_roast_checkbox', esc_attr( $roast_checkbox ));
}
// Display Select Box
add_action( 'woocommerce_before_add_to_cart_button', 'add_roast_custom_field', 0 );
function add_roast_custom_field() {
global $post;
// If …Run Code Online (Sandbox Code Playgroud) 在我的在线商店中,有两种标准运输方式 - 统一费率和免费送货。我添加了一个用于远程交付的插件。
因此,当客户在下订单时填写“城市”和“地址”字段时,必须添加新的送货方式。但在我选择统一费率或免费送货之前,新的送货是不可见的。
据我了解,我没有根据字段的填写自动更新运输方式。
我找到并编辑了这段代码:
add_action('wp_footer', 'woocommerce_custom_update_checkout', 50);
function woocommerce_custom_update_checkout() {
if (is_checkout()) {
?>
<script type="text/javascript">
jQuery( document ).ready(function( $ ) {
$('#billing_address_1').click(function(){
jQuery('body').trigger('update_checkout', { update_shipping_method: true });
});
});
</script>
<?php
}
}
Run Code Online (Sandbox Code Playgroud)
但直到我第二次单击填充字段时,交付方式才会更新。
我想用 AJAX 连接。如何编辑代码以便使用 AJAX 的结果立即可见,而无需再次单击填充的字段?