'submit', 'name' => 'ux-buy-now', 'value' => esc_attr( get_the_ID() ), 'class' => [ 'ux-buy-now-button', 'button', 'primary', 'ml-half', ], ]; if ( $wc_wp_theme_element_class = wc_wp_theme_get_element_class_name( 'button' ) ) { $button_atts['class'][] = $wc_wp_theme_element_class; } ?> is_type( 'external' ) ) { return false; } return $show; } /** * Checks if the current request is a Buy Now request. * * @return bool True if it is a Buy Now request, false otherwise. */ private function is_buy_now_request() { return isset( $_REQUEST['ux-buy-now'] ) && is_numeric( wp_unslash( $_REQUEST['ux-buy-now'] ) ); // phpcs:ignore } /** * Buy now action. * * @return void */ public function add_to_cart_action() { if ( ! $this->is_buy_now_request() ) { return; } if ( isset( $_REQUEST['variation_id'] ) && ! $_REQUEST['variation_id'] ) { // phpcs:ignore return; } if ( isset( $_REQUEST['quantity'] ) && is_array( $_REQUEST['quantity'] ) ) { // phpcs:ignore foreach ( $_REQUEST['quantity'] as $quantity ) { // phpcs:ignore if ( ! $quantity ) { return; } } } if ( ! isset( $_REQUEST['add-to-cart'] ) || $_REQUEST['add-to-cart'] !== $_REQUEST['ux-buy-now'] ) { // phpcs:ignore $_REQUEST['add-to-cart'] = $_REQUEST['ux-buy-now']; // phpcs:ignore } } /** * Redirect user after quick buy button is submitted. * * @param string $url Url. * * @return string */ public function buy_now_redirect( $url ) { if ( ! $this->is_buy_now_request() ) { return $url; } $redirect = $this->get_redirect_url(); if ( ! $redirect['url'] ) { return $url; } return $redirect['url']; } /** * Get redirect url. * * @return array */ private function get_redirect_url() { $url = [ 'url' => '' ]; $redirect = get_theme_mod( 'product_buy_now_redirect', 'checkout' ); switch ( $redirect ) { case 'cart': $url = [ 'type' => 'internal', 'url' => wc_get_cart_url(), ]; break; case 'checkout': $url = [ 'type' => 'internal', 'url' => wc_get_checkout_url(), ]; break; } return $url; } }