How to Disable Add-to-Cart URLs in WooCommerce? Is This Code Safe?
-
Hello,
I would like to disable the functionality that allows users to add products to the cart via custom URLs in WooCommerce. Can we use below code to disable that functionality?
// Disable add to cart via URL
function disable_add_to_cart_url( $query_args ) {
if ( isset( $query_args['add-to-cart'] ) ) {
wp_die( 'Adding products to the cart via URL is disabled on this site.' );
}
return $query_args;
}
add_filter( 'woocommerce_add_to_cart_url', 'disable_add_to_cart_url' );Can you confirm if this code is valid and safe to use? Would it disable the add-to-cart functionality via URL without causing issues or conflicts with any other functionality in WooCommerce?
I want to disable it for all the variations.
https://example.com/?add-to-cart=PRODUCT_ID&quantity=QUANTITY
https://example.com/?add-to-cart=VARIATION_ID&quantity=QUANTITY
https://example.com/?add-to-cart=GROUPED_PRODUCT_ID&quantity%5BSUB_PRODUCT_ID%5D=QUANTITY
https://example.com/?add-to-cart=GROUPED_PRODUCT_ID&quantity%5BSUB_PRODUCT_ID%5D=QUANTITY&quantity%5BSUB_PRODUCT_ID%5D=QUANTITY
https://example.com/checkout/?add-to-cart=PRODUCT_ID&quantity=QUANTITY
https://example.com/cart/?add-to-cart=PRODUCT_ID&quantity=QUANTITYThank you for your help!
- You must be logged in to reply to this topic.