URL: Display Text Offer To User If the Add to Schedule Link Has Been Clicked

Display custom text (an offer) to a user if the Add to Schedule Link has already been clicked by the user.

Important: Always test customizations to your site on a staging environment before making changes to your live / production site.  If you are not experienced with testing changes on a staging site, here is a good article to review.
* Tracks a users clicks on a link and shows a new offer if needed.
* @param array $link The link parameters.
* @param WC_Product $product The WooCommerce product.
* @param int $user_id The users id.
* @return array filtered link or redirect.
function xx_autoship_track_user_clicks( $link, $product, $user_id ){

  // Get the current list of links tracked
  $tracked_links = get_user_meta( $user_id, '__autoship_tracked_links', true );
  $tracked_links = empty( $tracked_links ) ? array() : $tracked_links;
  $new_offer = false;

  // Now check if this product link has already been clicked.
  $key = $link['item'] . '-' . $link['min'] . '-' . $link['max'];
  if ( isset( $tracked_links[$key] ) ){

    // Increase the click count
    $tracked_links[$key]['clicks'] += 1;
    $new_offer = true;

  } else {

    // Hasn't been clicked by this user so add it.
    $tracked_links[$key] = array(
      'clicks'=> 1,
      'link'  => $link


  // Add the updated tracked links to user.
  add_user_meta( $user_id , '__autoship_tracked_links', $tracked_links, true );

  // Initiate New offer if this link was already clicked.
  if ( $new_offer ){

    // Create a dynamic offer allowing user to add this product to the next five scheduled orders ( not including next one )
    $link['min'] = 1;
    $link['max'] = 6;
    $offer = autoship_get_scheduled_order_item_add_url( $link );

    $msg = sprintf( __( 'WOW! Looks like you\'ve already added this item to your next scheduled order.  Since we always like to reward our loyal customers you can add this to your next six ( 6 ) scheduled orders at the same discount! Click <a href="%s">HERE</a> to take advantage of this offer.', 'autoship' ), $offer );
    wc_add_notice( $msg , 'success' );

		wp_redirect( autoship_get_scheduled_orders_url() );


  return $link;

add_filter('autoship_add_to_scheduled_order_link_values', 'xx_autoship_track_user_clicks', 10, 3 );

Still need help? Contact Us Contact Us