0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

WooCommerce で注文がCheckout Blockで作られた注文かどうかの判定方法

Last updated at Posted at 2025-02-22

WooCommerce が現在標準でCheckout Blockを利用してカートや支払いページが動くようになったのですが、決済後の表示などに関して、クラシックと違う場合があるので、Store APIをいじるよりも表示のフック等で区別した方がシンプルで分かりやすいので、注文単位でCheckout Block経由の注文かどうかを判断した時の方がいい場合の対応ほうを共有します。
ちなみに注文が出来上がった後となりますので、process_payment以降的に考えた方がいいです。

$order->get_created_via() を使う。

注文オブジェクトで使える関数 get_created_vi で判別が出来ます。
ブロックで作られた注文もしくは REST API で作成された場合は'store-api'となります。
クラッシックチェックアウトで作られた注文は'checkout'となります。

functions.php
// $order は \WC_Order オブジェクト
if ( $order->get_created_via() === 'store-api' ) {
    // この注文はブロック版のチェックアウトで作られた
}

DB的な情報としては、データベース上では HPOS を利用して、post の post_typeshop_order_placehold の場合は以下で設定されています。

テーブル: wp_wc_order_operational_data
フィールド: created_via

post の post_typeshop_order の場合で注文データに post データを利用している場合は、投稿メタに以下のように登録されます。
テーブル: wp_postmeta
キー: _created_via

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?