在Woocommerce购物车页面上添加自定义输入字段

问题描述 投票:1回答:2

我一直在开发一个WooCommerce网站,我有一个任务,我卡住了。我需要在购物车项目表中添加一个额外的自定义输入字段。

例如,如果一个人订购'2000 youtube views'包,那么在项目名称的正下方,我希望用户输入他的Youtube视频URL。

我知道我可以在产品页面上添加自定义输入字段,只需将它们显示在购物车页面上即可。但我想将用户输入数据带到购物车页面。每个购物车项目都有一个自定义输入字段。到目前为止,我已经研究了很多但是我的解决方案没有任何成功。我发现只需将自定义输入字段数据打印到产品页面上添加的购物车页面上即可。

任何帮助,将不胜感激

php wordpress woocommerce shopping-cart custom-fields
2个回答
0
投票

我也遇到过这个问题。我已经解决了这个问题。像你一样添加购物车页面上的字段,但是使用jQuery接收这些值并使用GET请求动态传输它们(将它们添加到提交按钮),$ _GET将它们填入隐藏输入类型的结帐页面中。


-3
投票

30秒的谷歌搜索我发现了这个教程。

http://www.themelocation.com/how-to-add-custom-field-to-woocommerce-checkout-page/

这是片段。

/**
 * Add the field to the checkout page
 */

add_action( 'woocommerce_after_order_notes', 'some_custom_checkout_field' );

functionsome_custom_checkout_field( $checkout ) {

    echo '<div id="some_custom_checkout_field"><h2>' . __('Heading') . '</h2>';

    woocommerce_form_field( 'some_field_name', array(
        'type'         => 'text',
        'class'         => array('my-field-class form-row-wide'),
        'label'         => __('Additional Field'),
        'placeholder'   => __('Some hint'),
        'required'     => true,
    ), $checkout->get_value( 'some_field_name' ));

    echo '</div>';
}

/**
 * Process the checkout
 */
add_action(‘woocommerce_checkout_process’, ‘some_custom_checkout_field_process’);
functionsome_custom_checkout_field_process() {
    // if the field is set, if not then show an error message.
    if ( ! $_POST[‘some_field_name’] ){
        wc_add_notice( __( ‘Please enter value.’ ), ‘error’ );
    }
}

/**
 * Update the order meta with field value
 */
add_action( ‘woocommerce_checkout_update_order_meta’, ‘some_custom_checkout_field_update_order_meta’ );
function some_custom_checkout_field_update_order_meta( $order_id ) {
    if ( ! empty( $_POST[‘some_field_name’] ) ) {
        update_post_meta( $order_id, ‘Some Field’, sanitize_text_field( $_POST[‘some_field_name’] ) );
    }
}

我之前已经实现了类似的东西,应该可以工作,我自己没有测试过上面的代码。

希望能帮助到你。

© www.soinside.com 2019 - 2024. All rights reserved.