如何根据Woocommerce_form_field()选择运行一个函数?
我正在从woocommerce_form_field()
中填充下拉菜单,如下所示:
// Add additional items to array
$options = array(
0 => __( 'Select an option...', 'woocommerce' ),
1 => __( 'No Compressor & Bracket ', 'woocommerce')
) + $options;
// Add select field
woocommerce_form_field( 'compressor-options', array(
'type' => 'select',
'label' => __( 'Add Compressor & Bracket', $domain ),
'required' => false,
'options' => $options,
),'' );
echo '<p>Number of Compressor & Bracket options: ' . count( $options ) . '</p>';
我想根据用户的选择来更改主产品的SKU。我目前不知道如何这样做。
我有更改SKU的功能,但是如果用户进行选择,则不会接受。
function change_current_sku() {
global $product;
if ( ! isset( $POST['compressor-options'] ) ) {
echo '<div><br></div>';
echo $product->get_sku() . ' (EX)';
}
}
add_action( 'woocommerce_after_add_to_cart_button', 'change_current_sku' );
有没有一种方法可以在WooCommerce_form_field上拥有onChange()
?
I am populating a dropdown from a woocommerce_form_field()
as shown below:
// Add additional items to array
$options = array(
0 => __( 'Select an option...', 'woocommerce' ),
1 => __( 'No Compressor & Bracket ', 'woocommerce')
) + $options;
// Add select field
woocommerce_form_field( 'compressor-options', array(
'type' => 'select',
'label' => __( 'Add Compressor & Bracket', $domain ),
'required' => false,
'options' => $options,
),'' );
echo '<p>Number of Compressor & Bracket options: ' . count( $options ) . '</p>';
I would like to change the SKU of the main product depending on the selection made by the user. I am currently unaware of how to do so.
I have a function to change the SKU, but it does not take in if the user makes a selection.
function change_current_sku() {
global $product;
if ( ! isset( $POST['compressor-options'] ) ) {
echo '<div><br></div>';
echo $product->get_sku() . ' (EX)';
}
}
add_action( 'woocommerce_after_add_to_cart_button', 'change_current_sku' );
Is there a way to have an onchange()
on the woocommerce_form_field?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当涉及简单类型的产品时,Ajax本身并不是必需的,只有jQuery可以做到。因此,这取决于您的特定要求。
所以你得到:
When it comes to simple type products only, then ajax per se is not necessary and just jQuery will do. So it depends on your specific requirements.
So you get: