Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save xlplugins/46e5f7d53a21f9adbef10a0238d667e0 to your computer and use it in GitHub Desktop.
Save xlplugins/46e5f7d53a21f9adbef10a0238d667e0 to your computer and use it in GitHub Desktop.
Sale page redirect to checkout page using the Elementor pro add to cart button
class WFFN_Change_Action_Of_Add_To_Cart {
// Define an array of landing page IDs and their corresponding checkout URLs
public $checkout_urls = [
347536 => 'https://dharmasun.shedrub1.site/checkouts/tte-full/', // en
347545 => 'https://dharmasun.shedrub1.site/de/checkouts/tte-full/', // de
// Add more entries as needed
];
public function __construct() {
add_action( 'wfacp_after_checkout_page_found', [ $this, 'remove_notice' ] );
add_filter( 'woocommerce_add_to_cart_form_action', [ $this, 'change_form_action' ] );
add_filter('woocommerce_cart_contents_changed', [$this, 'handle_cart_contents_changed']);
}
public function change_form_action( $action ) {
global $post;
if ( 'wffn_landing' === $post->post_type && isset( $this->checkout_urls[ $post->ID ] ) ) {
// Change the action URL to the specific checkout URL based on the post ID
$action = $this->checkout_urls[ $post->ID ];
}
return $action;
}
public function remove_notice() {
$notices = WC()->session->get( 'wc_notices', array() );
if ( ! isset( $notices['error'] ) ) {
return;
}
foreach ( $notices['error'] as $index => $notice ) {
if ( 'No product in this checkout page' === $notice['notice'] ) {
unset( $notices['error'][ $index ] );
break;
}
}
WC()->session->set( 'wc_notices', $notices );
}
// Add new method for cart contents changed
public function handle_cart_contents_changed($data) {
if (isset($_SERVER['REDIRECT_URL']) && false !== strpos($_SERVER['REDIRECT_URL'], '/checkouts/')) {
remove_action('woocommerce_add_to_cart', [WFCH_Public::get_instance(), 'woocommerce_add_to_cart'], 99);
}
return $data;
}
}
new WFFN_Change_Action_Of_Add_To_Cart();
@jojobabrian
Copy link

jojobabrian commented Mar 27, 2024

We are using this code on our site. We have a product with variations. When this code is enabled, the default variation is added to the cart, ignoring the selection that is made. https://jojobacompany.com/sp/bogo/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment