【问题标题】:WooCommerce function.php - how to save coupon code to a custom field?WooCommerce function.php - 如何将优惠券代码保存到自定义字段?
【发布时间】:2016-02-18 01:54:06
【问题描述】:

How to add Custom Product Fields in WooCommerce

我按照上面的指南将下面的代码添加到我的functions.php 中。那工作得很好。我的问题是如何修改此代码以保存输入的优惠券代码 $_POST['coupon_code'] (在结帐期间)并将其保存到自定义字段?我被困在这一点上,因为我的总体目标是测试优惠券代码并用向客户推广该优惠券代码的销售人员填写自定义字段。

感谢您的帮助!

/**
 * Add the field to the checkout
 */
add_action( 'woocommerce_after_order_notes', 'my_custom_checkout_field' );

function my_custom_checkout_field( $checkout ) {

    echo '<div id="my_custom_checkout_field"><h2>' . __('My Field') . '</h2>';

    woocommerce_form_field( 'my_field_name', array(
        'type'          => 'text',
        'class'         => array('my-field-class form-row-wide'),
        'label'         => __('Fill in this field'),
        'placeholder'   => __('Enter something'),
        ), $checkout->get_value( 'my_field_name' ));

    echo '</div>';

}

/**
 * Update the order meta with field value
 */
add_action( 'woocommerce_checkout_update_order_meta', 'my_custom_checkout_field_update_order_meta' );

function my_custom_checkout_field_update_order_meta( $order_id ) {
    if ( ! empty( $_POST['my_field_name'] ) ) {
        update_post_meta( $order_id, 'My Field', sanitize_text_field( $_POST['my_field_name'] ) );
    }
}

/**
 * Display field value on the order edit page
 */
add_action( 'woocommerce_admin_order_data_after_billing_address', 'my_custom_checkout_field_display_admin_order_meta', 10, 1 );

function my_custom_checkout_field_display_admin_order_meta($order){
    echo '<p><strong>'.__('My Field').':</strong> ' . get_post_meta( $order->id, 'My Field', true ) . '</p>';
}

【问题讨论】:

    标签: php wordpress woocommerce


    【解决方案1】:

    如果您仍在寻找答案,我设法在我自己的问题的帮助下为您的代码增添了趣味,因为我处于类似的情况。

    Fill out custom field with used coupon code WooCommerce

    您的代码和我的编辑已更改为实际将优惠券保存为自定义值的位置。您的代码存在的问题之一是您如何尝试使用“我的字段”访问自定义字段。我已将这些替换为该字段的实际名称。

    /**
     * Add the field to the checkout
     */
    add_action( 'woocommerce_after_order_notes', 'my_custom_checkout_field' );
    
    function my_custom_checkout_field( $checkout ) {
    
        echo '<div id="my_custom_checkout_field"><h2>' . __('My Field') . '</h2>';
    
        woocommerce_form_field( 'my_field_name', array(
            'type'          => 'text',
            'class'         => array('my-field-class form-row-wide'),
            'label'         => __('Fill in this field'),
            'placeholder'   => __('Enter something'),
            ), $checkout->get_value( 'my_field_name' ));
    
        echo '</div>';
    
    }
    
    /**
     * Update the order meta with field value
     */
    add_action( 'woocommerce_checkout_update_order_meta', 
    'my_custom_checkout_field_update_order_meta' );
    
    function my_custom_checkout_field_update_order_meta( $order_id, $posted ) {
      if ( isset($_POST['my_field_name']) && empty( $_POST['my_field_name'])) {
        $order = new WC_Order( $order_id );
          foreach( $order->get_used_coupons() as $coupon) {
                    $coupons .= $coupon.', ';
                    }
        update_post_meta( $order_id, 'my_field_name', $coupons);
      }
    
    }
    
    /**
     * Display field value on the order edit page
     */
    add_action( 'woocommerce_admin_order_data_after_billing_address', 'my_custom_checkout_field_display_admin_order_meta', 10, 1 );
    
    function my_custom_checkout_field_display_admin_order_meta($order){
        echo '<p><strong>'.__('My Field').':</strong> ' . get_post_meta( $order->id, 'my_field_name', true ) . '</p>';
    }
    

    在使用此代码之前,您可能需要进行一些清理。 ;)

    来源:Fill out custom field with used coupon code WooCommerce

    【讨论】:

      猜你喜欢
      • 2014-11-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-02-25
      • 2017-07-13
      相关资源
      最近更新 更多