首页 文章

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

提问于
浏览
1

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

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

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

任何帮助,将不胜感激

2 回答

  • -3

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

  • 0

    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’] ) );
        }
    }
    

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

    希望能帮助到你 .

相关问题