cart ) || ! is_object( $wc->cart ) ) { return false; } return method_exists( $wc->cart, 'get_cart' ); } /** * カートの中身を返す。 * * @return array */ private static function get_cart_contents() { if ( ! self::cart_available() ) { return array(); } $items = WC()->cart->get_cart(); return is_array( $items ) ? $items : array(); } /** * カートにこのプラグインの商品が1つ以上入っているか。 * * @return bool */ public static function cart_has_acscw_product() { foreach ( self::get_cart_contents() as $item ) { if ( isset( $item['product_id'] ) && ACSCW_Products::is_acscw_product( $item['product_id'] ) ) { return true; } } return false; } /** * カートの中身が「このプラグインの商品だけ」か。 * * 空カートは false(介入する理由がない)。 * * @return bool */ public static function cart_is_acscw_only() { $items = self::get_cart_contents(); if ( empty( $items ) ) { return false; } foreach ( $items as $item ) { if ( ! isset( $item['product_id'] ) || ! ACSCW_Products::is_acscw_product( $item['product_id'] ) ) { return false; } } return true; } /** * カートに入っている非ACSCW商品のカートキーを返す。 * * 「単独チェックアウト」モードで取り除く対象。 * * @return string[] カートキーの配列。 */ public static function get_foreign_cart_keys() { $keys = array(); foreach ( self::get_cart_contents() as $key => $item ) { if ( ! isset( $item['product_id'] ) || ! ACSCW_Products::is_acscw_product( $item['product_id'] ) ) { $keys[] = (string) $key; } } return $keys; } /** * チェックアウトの簡略化を適用してよい状況かどうか。 * * 次の全部を満たすときだけ true: * 1. 設定「購入体験 › チェックアウトを簡略化する」が有効(既定 yes) * 2. 管理画面・WP-Cron ではない * 3. REST リクエストではない。ただし **WooCommerce Store API は例外** * (ブロック版チェックアウトの実処理は Store API を通るため、 * ここを一律で弾くとブロック版でゲスト購入の判定が効かなくなる) * 4. カートの中身がこのプラグインの商品だけ * * @return bool */ public static function should_simplify_checkout() { $apply = false; if ( self::is_simplification_enabled() && self::is_front_context() ) { $apply = self::cart_is_acscw_only(); } /** * チェックアウト簡略化の適用可否。 * * 他プラグインとの衝突時に、サイト側が完全に無効化できる出口。 * * @since 3.0.0 * * @param bool $apply 判定結果。 */ return (bool) apply_filters( 'acscw_apply_checkout_simplification', $apply ); } /** * 設定でチェックアウト簡略化が有効かどうか。 * * @return bool */ public static function is_simplification_enabled() { return 'no' !== get_option( 'acscw_simplify_checkout', 'yes' ); } /** * 介入してよいリクエスト種別かどうか。 * * @return bool */ private static function is_front_context() { if ( is_admin() && ! wp_doing_ajax() ) { return false; } if ( function_exists( 'wp_doing_cron' ) && wp_doing_cron() ) { return false; } if ( self::is_rest_request() && ! self::is_store_api_request() ) { return false; } return true; } /** * REST リクエストかどうか。 * * @return bool */ private static function is_rest_request() { return defined( 'REST_REQUEST' ) && REST_REQUEST; } /** * WooCommerce Store API(ブロック版カート/チェックアウト)のリクエストかどうか。 * * WooCommerce 6.9 以降は WC()->is_store_api_request() が使える。 * 古い環境のために URI からの判定もフォールバックとして残す。 * * @return bool */ public static function is_store_api_request() { if ( function_exists( 'WC' ) ) { $wc = WC(); if ( is_object( $wc ) && method_exists( $wc, 'is_store_api_request' ) ) { return (bool) $wc->is_store_api_request(); } } if ( empty( $_SERVER['REQUEST_URI'] ) ) { return false; } $uri = esc_url_raw( wp_unslash( $_SERVER['REQUEST_URI'] ) ); return false !== strpos( $uri, '/wc/store/' ); } /** * 注文にこのプラグインの商品が含まれるか。 * * @param WC_Order|false $order 注文。 * @return bool */ public static function order_has_acscw_product( $order ) { if ( ! is_object( $order ) || ! method_exists( $order, 'get_items' ) ) { return false; } foreach ( $order->get_items() as $item ) { if ( ! method_exists( $item, 'get_product_id' ) ) { continue; } if ( ACSCW_Products::is_acscw_product( $item->get_product_id() ) ) { return true; } } return false; } }