Created
January 17, 2024 16:14
-
-
Save jpneey/350f99c61ed80297e9b85e3e6bdd7493 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
add_action( 'admin_post_nopriv_update_cart', 'scichart_update_cart' ); | |
add_action( 'admin_post_update_cart', 'scichart_update_cart' ); | |
function scichart_update_cart() { | |
defined( 'WC_ABSPATH' ) || exit; | |
/** | |
* Load woo dependencies | |
* Normally, they are only loaded on the fronted | |
*/ | |
include_once WC_ABSPATH . 'includes/wc-cart-functions.php'; | |
include_once WC_ABSPATH . 'includes/class-wc-cart.php'; | |
/** | |
* Instantiate the WC() object | |
*/ | |
if ( is_null( WC()->cart ) ) { | |
wc_load_cart(); | |
} | |
/** | |
* Clear the cart | |
*/ | |
WC()->cart->empty_cart(); | |
/** | |
* Validate user input | |
*/ | |
$to_cart_id = false; | |
$variation_id = filter_input(INPUT_POST, 'variation_id', FILTER_SANITIZE_NUMBER_INT); | |
$product_id = filter_input(INPUT_POST, 'product_id', FILTER_SANITIZE_NUMBER_INT); | |
$quantity = filter_input(INPUT_POST, 'quantity', FILTER_SANITIZE_NUMBER_INT) ?: 1; | |
$to_cart_id = ( $variation_id ) ?: ( $product_id ) ?: false; | |
if ( $to_cart_id ) { | |
/** | |
* Programatically add new item with quantity | |
* to the basket | |
*/ | |
WC()->cart->add_to_cart( $to_cart_id, $quantity ); | |
} | |
/** | |
* Header redirect instead of wp_redirect. | |
* This saves as skipping some apply_filters on the native | |
* wordpress redirect helper function | |
*/ | |
$redirect = wc_get_cart_url(); | |
header( "Location: $redirect" ); | |
exit; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment