如何根据Woocommerce_form_field()选择运行一个函数?

发布于 2025-01-30 09:29:55 字数 1096 浏览 5 评论 0原文

我正在从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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

薄荷梦 2025-02-06 09:29:55

当涉及简单类型的产品时,Ajax本身并不是必需的,只有jQuery可以做到。因此,这取决于您的特定要求。

所以你得到:

function action_woocommerce_before_add_to_cart_button() {
    // Some random options
    $random_options = array( 2 => 'abc', 3 => 'def', 4 => '(ghi)', 5 => 'jkl' );
   
    // Add additional items to array 
    $options = array( 
        0 => __( 'Select an option...', 'woocommerce' ),
        1 => __( 'No Compressor & Bracket ', 'woocommerce') 
    ) + $random_options;
        
    // Add select field
    woocommerce_form_field( 'compressor-options', array(
        'type'          => 'select',
        'label'         => __( 'Add Compressor & Bracket', 'woocommerce' ),
        'required'      => false,
        'options'       => $options,
    ),'' );
    echo '<p>Number of Compressor & Bracket options: ' . count( $options ) . '</p>';

    ?>
    <script type="text/javascript">
    jQuery( function($) {
        // On change
        $( document ).on( 'change', '#compressor-options', function () {
            var suffix = $( 'option:selected', this ).text();

            // Append
            $( '.my-product-sku .sku-suffix' ).text( suffix );
        });
    });
    </script>
    <?php
}
add_action( 'woocommerce_before_add_to_cart_button', 'action_woocommerce_before_add_to_cart_button' );

function action_woocommerce_after_add_to_cart_button() {
    global $product;
    
    // Is a WC product
    if ( is_a( $product, 'WC_Product' ) ) {
        echo '<p class="my-product-sku">' . $product->get_sku() . ': ' . '<span class="sku-suffix"></p>';
    }
}
add_action( 'woocommerce_after_add_to_cart_button', 'action_woocommerce_after_add_to_cart_button', 10 );

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:

function action_woocommerce_before_add_to_cart_button() {
    // Some random options
    $random_options = array( 2 => 'abc', 3 => 'def', 4 => '(ghi)', 5 => 'jkl' );
   
    // Add additional items to array 
    $options = array( 
        0 => __( 'Select an option...', 'woocommerce' ),
        1 => __( 'No Compressor & Bracket ', 'woocommerce') 
    ) + $random_options;
        
    // Add select field
    woocommerce_form_field( 'compressor-options', array(
        'type'          => 'select',
        'label'         => __( 'Add Compressor & Bracket', 'woocommerce' ),
        'required'      => false,
        'options'       => $options,
    ),'' );
    echo '<p>Number of Compressor & Bracket options: ' . count( $options ) . '</p>';

    ?>
    <script type="text/javascript">
    jQuery( function($) {
        // On change
        $( document ).on( 'change', '#compressor-options', function () {
            var suffix = $( 'option:selected', this ).text();

            // Append
            $( '.my-product-sku .sku-suffix' ).text( suffix );
        });
    });
    </script>
    <?php
}
add_action( 'woocommerce_before_add_to_cart_button', 'action_woocommerce_before_add_to_cart_button' );

function action_woocommerce_after_add_to_cart_button() {
    global $product;
    
    // Is a WC product
    if ( is_a( $product, 'WC_Product' ) ) {
        echo '<p class="my-product-sku">' . $product->get_sku() . ': ' . '<span class="sku-suffix"></p>';
    }
}
add_action( 'woocommerce_after_add_to_cart_button', 'action_woocommerce_after_add_to_cart_button', 10 );
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文