最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

php - Add a custom field to Woocommerce Checkout Blocks and get the value - Stack Overflow

programmeradmin3浏览0评论

I added some data with the function :

woocommerce_register_additional_checkout_field(
            array(
                'id'       => 'namespace/newsletter-opt-in',
                'label'    => 'Subscribe to newsletter ?',
                'location' => 'order',
                'type'     => 'checkbox',
            )
        );

I try to check this checkbox but I can't get the value However, the data is well returned in my order data column

I tried :

$order = wc_get_order($order_id);
$subscribe = $order->get_meta('namespace/newsletter-opt-in');

But $subscribe is empty

I added some data with the function :

woocommerce_register_additional_checkout_field(
            array(
                'id'       => 'namespace/newsletter-opt-in',
                'label'    => 'Subscribe to newsletter ?',
                'location' => 'order',
                'type'     => 'checkbox',
            )
        );

I try to check this checkbox but I can't get the value However, the data is well returned in my order data column

I tried :

$order = wc_get_order($order_id);
$subscribe = $order->get_meta('namespace/newsletter-opt-in');

But $subscribe is empty

Share Improve this question edited Mar 31 at 17:49 LoicTheAztec 255k24 gold badges399 silver badges446 bronze badges asked Mar 30 at 21:38 Jean R.Jean R. 5744 gold badges19 silver badges46 bronze badges 2
  • Don't know if it's a typo error, but make sure the meta key matches exactly. In your code, the ID is 'namespace/newsletter-opt-in', but you're trying to retrieve it with 'szf/newsletter-opt-in'. – Richard Commented Mar 31 at 7:23
  • 1 Yes it's a typo error ! Thanks – Jean R. Commented Mar 31 at 9:06
Add a comment  | 

2 Answers 2

Reset to default 2

Your provided code is a bit incomplete and you missed saving the field value. Try the following:

// Add a custom checkout field (checkout blocks)
add_action(
    'woocommerce_init',
    function() {
        woocommerce_register_additional_checkout_field( array(
            'id'          => 'namespace/newsletter-opt-in',
            'label'       => 'Subscribe to newsletter?',
            'location'    => 'order',
            'type'        => 'checkbox',
        ) );
    }
);

// Save the custom checkout field value (when the checkbox ix checked)
add_action(
    'woocommerce_set_additional_field_value',
    function ( $key, $value, $group, $wc_object ) {
        if ( 'namespace/newsletter-opt-in' === $key ) {
            $wc_object->update_meta_data( 'newsletter_opt_in', $value );
        }
    },
    10,
    4
);

Now the field value is saved as custom order meta data when submitting an order and you can retreive the "newsletter opt-in" value from the WC_Order object like:

$order = wc_get_order($order_id);
$subscribe = $order->get_meta('newsletter_opt_in');

The value will equal to '1' if the customer has subscribed. If the customer didn't subscribed there is no value (an empty string).

In complement to @LoicTheAztec answer, I use this hook :

woocommerce_payment_complete

The code :

#[Action('woocommerce_payment_complete')]
function validate_newsletter_opt_in($order_id)
{
    $order = wc_get_order($order_id);
    $newsletter_opt_in = $order->get_meta('newsletter_opt_in');

    if ($newsletter_opt_in) {
        $email = $order->get_billing_email();

        if ($email) {
                //here the code to subscribe
        }
    }
}
发布评论

评论列表(0)

  1. 暂无评论