WooCommerce function.如何将优惠券代码保存到自定义字段


WooCommerce function.how to save coupon code to a custom field?

如何在WooCommerce中添加自定义产品字段

我按照上面的指南将下面的代码添加到我的函数中.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>';
}

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

使用使用的优惠券代码填写自定义字段 伍商务

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

/**
 * 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>';
}

使用此代码之前,您可能需要清理一下。 ;)

来源:使用使用的优惠券代码填写自定义字段 WooCommerce