How to know the page from which a product is added to the cart? – WooCommerce

Please try below code :

In this display only page/post URL and page/post ID. You need to store page ID/URL and can get from database.

NOTE : This is code working only in details page.

function custom_to_cart_item( $cart_item_data, $product_id, $variation_id ) {
    $page_url = wp_get_referer();
    $page_id = url_to_postid( $page_url );
add_filter( 'woocommerce_add_cart_item_data', 'custom_to_cart_item', 10, 3 );