Q: Is there a way to empty the cart before adding the product 1234?
A: Before adding a product to the cart, it is possible to empty the cart so that the add_to_cart_redirect
hook will always be called. Use the woocommerce_add_to_cart_validation
hook to alter the cart before adding a new item.
/**
* First clear the cart of all products
*/
function clear_cart_before_adding( $cart_item_data ) {
global $woocommerce;
$woocommerce->cart->empty_cart();
return true;
}
add_filter( 'woocommerce_add_to_cart_validation', 'clear_cart_before_adding' );
/**
* Redirect to checkout after adding to cart
*/
function themeprefix_add_to_cart_redirect() {
global $woocommerce;
$checkout_url = $woocommerce->cart->get_checkout_url();
return $checkout_url;
}
add_filter('add_to_cart_redirect', 'themeprefix_add_to_cart_redirect');
Related Posts:
- How to pass/get data to/from the WooCommerce data-product_variations object?
- Filter hook before create order WooCommerce
- How to change Woocommerce breadcrumbs content?
- Add default user field to WooCommerce checkout [closed]
- Woocommerce “added to cart” message on main(home) page
- How to access page variable inside action hook
- WooCommerce comments_template Filter Not Firing
- WordPress hooks/filters insert before content or after title
- How do filters and hooks really work in PHP
- Trouble understanding apply_filters()
- How many filter/action hooks are healthy?
- Filter specific shortcode output?
- Earliest hook to reliably get $post/$posts
- Where to hook into post content?
- What does (10, 2) mean when used with add_filter
- Clarification on filters and hooks
- Valid characters for actions, hooks and filters
- How to hook into unregistering a widget instance?
- How to check if a hook is hooked or not?
- Passing Additional Parameters to add_filter Callable
- What hook do I use to edit the post statuses option in admin?
- Modify WordPress Rest Api Request/Response
- How to add some custom HTML into wordpress admin bar?
- Custom theme hooks / filters – passing arguments
- wp_mail – Remove sitename from email subject
- How to disable all WordPress emails modularly and programatically?
- How to hook wp_list_pages?
- Display WooCommerce newest product reviews on top [closed]
- apply_filters() slices away needed arguments
- WP Rest API – Upload media without saving attachment post
- How to make post and comment count unclickable with dashboard_glance_items hook
- How to use the_excerpt in a filter hook?
- How to add attribute to output with wp_video_shortcode add_filter
- Hook into admin post list page
- Anyway to edit the titlebar of WordPress Widgets in the Admin area?
- How to limit the pages displayed for choosing parent page on page attribute’s menu?
- How can I hide all posts that don’t have a thumbnail?
- Change text of Description in Image Library
- Load different template file when condition met?
- Show only geolocated user country into Woocommerce checkout country fields
- How to remove “out of stock” variation options from size dropdown in woocommerce?
- Am I using the right hook for removing quicktags on the admin TinyMCE?
- About Hooks and Filters
- Add Dropdown menu using “add_filter => wp_nav_menu_items”
- How to enable visual editor when editing comments on the dashboard?
- Should I use add_action(‘publish_post or add_filter(‘publish_post?
- Please explain me what the do_action does
- Is it possible to Hook/Filters Attachment Creation?
- Filter all html output
- Can’t get wp_title filter working in twenty sixteen child theme
- How can I reliably and globally disable wptexturize?
- Customise Jetpack Publicize text
- Removing labels and tag on WordPress’s default login form
- WordPress RSS feed – filter RSS content by custom field value
- How to trigger the core WPLANG to make automatically set a language when the theme is activated? [duplicate]
- How to hook some Unicode texts into calendar widget safely?
- How to add attributes to tag when template cannot be directly modified
- filter the_title problem in nav
- How can I add a fifth option to the alignment picker?
- Too many actions/filters!
- Filter the URL of next_posts_link & previous_posts_link
- How to get list of all hooks of current theme / plugin?
- Which hook is fired when inserting media into a post
- How to replace any occurence of Gravatars with a local placeholder image?
- How does WordPress call functions attached to a certain action hook before calling functions attached to other hooks
- Sensei LMS Hooks to Remove Content
- What hook/filter can I use to add/edit/show/hide the title under (on hover) links on the table view?
- Customize user account activation message
- Conditionally call add_action depending on post_type?
- Woocommerce Shipping module available only for type of products [closed]
- Change WordPress RSS link with filter?
- WordPress tag cloud add more links
- Woocommerce – Hide a Column in Cart Table
- Add a header before fields added with the attachment_fields_to_edit() filter
- Error when overriding only some audio shortcode HTML output
- Hide content editor for posts after approriate date
- Why anything done on comments_array hook gets reset?
- Is there a Woocommerce hook that fires when applying a coupon but before checking if it’s valid?
- Gutenberg disable the “block” tab in right sidebar
- What is the action hook for an order that fails on frontend checkout in WooCommerce?
- Why my admin doesn’t work after adding rest_prepare_post filter?
- How To Change The Html of Products filtration sidebar in Woocommerce?
- Filter category in WooCommerce shop page to display related sub-category
- add_filter() function misunderstanding
- Is it possible to track down Actions and Filters?
- When to use actions and when to use filters
- add_filter to ‘woocommerce_before_main_content’ [closed]
- Yoast SEO hooks overriding themselves
- Editing
- Is possible dequeue/remove style from wp_footer() hook and add on wp_head() hook?
- Same Conditionals Not Working on Two Different Hooks
- Filter or Hook to catch pre-rendering of post content
- How to call a function or method that is Namespaced using another plugin
- How to change currency programmatically on creating order action?
- add filter login_redirect does not contain original requested redirect
- WooCommerce: Add Payment Gateway Field to Webhooks [closed]
- get_header and hook avoid normal call
- Question about how do wordpress filters/actions work
- How to center oEmbedded content
- How to add numeric slug for child page in WordPress 5.9?