在WooCommerce的“快速查看”窗口中显示自定义表单
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了在WooCommerce的“快速查看”窗口中显示自定义表单相关的知识,希望对你有一定的参考价值。
我使用该代码,在产品编辑页面上显示了复选框“ Roast Level”。当经理单击此复选框时,单个产品页面上会出现一个选择框,允许客户选择“烤肉等级”。
选择产品并将其添加到购物车时,所选值将显示在购物车本身中。此值还会显示在结帐页面,“谢谢”页面,订单,电子邮件通知以及管理面板中的订单编辑页面上。
这里是代码:
// 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 $product;
// If is single product page and have the "roast_checkbox" enabled we display the field
if ( is_product() && $product->get_meta( '_roast_checkbox' ) === 'yes' )
echo '<div class="roast_select">';
$select = woocommerce_form_field( 'roast_custom_options', array(
'type' => 'select',
'class' => array('my-field-class form-row-wide'),
'label' => __('Roast Level'),
'required' => false,
'return' => false,
'options' => array(
'' => 'Please select',
'Blue' => 'Blue',
'Rare' => 'Rare',
'Medium Rare' => 'Medium Rare',
'Medium' => 'Medium',
'Medium Well' => 'Medium Well',
'Well Done' => 'Well Done'
)
), '' );
echo $select;
echo '</div>';
/*---------------------------------------------------------------
* Add as custom cart item data
---------------------------------------------------------------*/
add_filter( 'woocommerce_add_cart_item_data', 'add_custom_cart_item_data', 10, 21 );
function add_custom_cart_item_data($cart_item_data, $product_id, $variation_id )
if( isset( $_POST['roast_custom_options'] ) )
$cart_item_data['roast_option'] = wc_clean( $_POST['roast_custom_options'] );
return $cart_item_data;
/*---------------------------------------------------------------
* Add custom fields values under cart item name in cart
---------------------------------------------------------------*/
add_filter( 'woocommerce_cart_item_name', 'roast_custom_field', 10, 21 );
function roast_custom_field( $item_name, $cart_item, $cart_item_key )
if( ! is_cart() )
return $item_name;
if( isset($cart_item['roast_option']) )
$item_name .= '<br /><div class="my-custom-class"><strong>' . __("Roast Level", "woocommerce") . ':</strong> ' . $cart_item['roast_option'] . '</div>';
return $item_name;
/*---------------------------------------------------------------
* Display roast custom fields values under item name in checkout
---------------------------------------------------------------*/
add_filter( 'woocommerce_checkout_cart_item_quantity', 'roast_custom_checkout_cart_item_name', 10, 21 );
function roast_custom_checkout_cart_item_name( $item_qty, $cart_item, $cart_item_key )
if( isset($cart_item['roast_option']) )
$item_qty .= '<br /><div class="my-custom-class"><strong>' . __("Roast Level", "woocommerce") . ':</strong> ' . $cart_item['roast_option'] . 'гр.</div>';
return $item_qty;
/*---------------------------------------------------------------
* Save chosen slelect field value to each order item as custom meta data and display it everywhere
---------------------------------------------------------------*/
add_action('woocommerce_checkout_create_order_line_item', 'save_order_item_product_fitting_color', 10, 21 );
function save_order_item_product_fitting_color( $item, $cart_item_key, $values, $order )
if( isset($values['roast_option']) )
$key = __('Roast Level', 'woocommerce');
$value = $values['roast_option'];
$item->update_meta_data( $key, $value ,$item->get_id());
add_action('wp_footer','add_footer_script');
function add_footer_script()
?>
<script>
jQuery('#roast_custom_options').on('change',function()
var roast_level = jQuery(this).val();
/*console.log(roast_level); */
var button = jQuery(this).closest('form').find('.add_to_cart_button'); console.log(button);
jQuery(button).attr('data-roast_custom_options',roast_level);
);
</script>
<?php
我需要在快速查看窗口的content-product-quick-view.php文件中添加“ Roast Level”选择表格。
添加代码时出现错误。表单不显示,或者“添加到购物车”按钮消失。
global $product;
// If is single product page and have the "roast_checkbox" enabled we display the field
if ( is_product() && $product->get_meta( '_roast_checkbox' ) === 'yes' )
echo '<div class="roast_select">';
$select = woocommerce_form_field( 'roast_custom_options', array(
'type' => 'select',
'class' => array('my-field-class form-row-wide'),
'label' => __('Roast Level'),
'required' => false,
'return' => false,
'options' => array(
'' => 'Please select',
'Blue' => 'Blue',
'Rare' => 'Rare',
'Medium Rare' => 'Medium Rare',
'Medium' => 'Medium',
'Medium Well' => 'Medium Well',
'Well Done' => 'Well Done'
)
), '' );
echo $select;
echo '</div>';
结果,我无法将此代码放入php文件。也许这里需要添加其他代码。我请求您的帮助。
要在产品快速编辑上管理自定义字段值,需要执行3个步骤:
1。显示字段
使用woocommerce_product_quick_edit_end
操作钩在产品快速编辑表单上显示复选框。
2。保存字段值
可以使用woocommerce_product_quick_edit_save
操作钩子在Ajax调用上保存表单字段值。
3。填充自定义字段
实际上,没有任何标准方法可以这样做。我使用产品的“名称”列将值放在此处的隐藏字段中,并在用户单击Quick Edit
链接(按钮!)时使用JS读取值。
代码中提供了一些其他信息和注释:
add_action( 'woocommerce_product_quick_edit_end', 'tst_product_quick_edit_add_roast_field' );
add_action( 'woocommerce_product_quick_edit_save', 'tst_product_quick_edit_save_roast_field' );
add_action( 'manage_product_posts_custom_column', 'tst_add_product_roast_column', 10, 2);
add_action( 'admin_print_footer_scripts', 'tst_product_admin_table_enqueue_roast_assets' );
/**
* Displays a checkbox for product roast level on the quick edit form
*/
function tst_product_quick_edit_add_roast_field()
?>
<div class="inline-edit-group roast_field">
<label class="roast_level_field">
<span class="title"><?php esc_html_e( 'Roast Level', 'woocommerce' ); ?></span>
<span class="input-text-wrap">
<input type="checkbox" name="_roast_checkbox" value="1">
</span>
</label>
</div>
<?php
;
/**
* Saves roast level checkbox value when the update button is pressed on product quick edit form
*
* @param WC_Product $product
*/
function tst_product_quick_edit_save_roast_field( $product )
if ( $product->is_type('simple') )
$id = $product->get_id();
$roast_checkbox = 1 == ! empty( $_REQUEST['_roast_checkbox'] ) ? 'yes' : 'no';
update_post_meta( $id, '_roast_checkbox', $roast_checkbox );
/**
* Gets roast checkbox value from db and prints it on the `name` column as a hidden field.
* We would be able to read the value by JS to populate the roast checkbox on quick edit forms
*
* @param string $column
* @param int $post_id
*/
function tst_add_product_roast_column( $column, $post_id )
if ( 'name' !== $column )
return;
$roast_value = get_post_meta( $post_id,'_roast_checkbox',true);
$roast_value = 'yes' === $roast_value ? '1' : '0';
?>
<input hidden class="roast-checkbox-value" value="<?= $roast_value ?>">
<?php
/**
* Enqueue a JS script to populate the roast checkbox on product quick edit form with the stored value.
*/
function tst_product_admin_table_enqueue_roast_assets()
$screen = get_current_screen();
if ( ( 'edit' != $screen->base && '' != $screen->action) || !isset($_REQUEST['post_type']) || 'product' !== $_REQUEST['post_type'] )
return;
?>
<script>
jQuery(document).ready( function($)
$('#the-list .editinline').live("click", function()
/**
* Reads roast level metadata for the current product
* and loads the value to the roast checkbox on quick edit form
*/
let quickEditLink = $(this);
let post_id = quickEditLink.closest('tr').attr('id');
let roastCheckboxValue = quickEditLink.closest('td.name').find('.roast-checkbox-value').val();
let productID = post_id.replace("post-", "");
let roastCheckbox = $('#edit-' + productID + ' input[name="_roast_checkbox"]');
let productType = $('#woocommerce_inline_' + productID).find('.product_type').text();
/**
* Only roast checkbox for simple products and hide for the other product types.
*/
if ( 'simple' === productType )
roastCheckbox.closest('.roast_field').show();
if ( '1' === roastCheckboxValue )
roastCheckbox.prop('checked', true);
else
roastCheckbox.prop('checked', false);
else
roastCheckbox.prop('checked', false);
roastCheckbox.closest('.roast_field').hide();
);
);
</script>
<?php
经过测试,可以正常工作!
以上是关于在WooCommerce的“快速查看”窗口中显示自定义表单的主要内容,如果未能解决你的问题,请参考以下文章
Woocommerce 如何在我的帐户页面上重定向自定义端点
在 WooCommerce 挂钩中显示高级自定义字段 (ACF) 值