Skip Navigation

[Resolved] URL Parameter no longer going through [wpv-woo-buy-or-select] Add to Cart

This support ticket is created 5 years, 4 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
- 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Hong_Kong (GMT+08:00)

This topic contains 1 reply, has 2 voices.

Last updated by Luo Yang 5 years, 4 months ago.

Assisted by: Luo Yang.

Author
Posts
#1365743

I am using the code below to pass a URL parameter through to cart item data when using the wpv-woo-buy-or-select shortcode. This code was tested and working, but the URL parameter is now unavailable to the function for an unknown reason.

I previously had an issue with achieving this that was resolved (see https://toolset.com/forums/topic/unable-to-send-custom-cart-item-data-through-wpv-woo-buy-or-select-shortcode/#post-1262573) but nothing in this solution has helped resolve the current issue.

What I expect to happen: Adding an item to the cart from hidden link should pass the 'referrer' parameter to the function below but the function says there is no parameter to get.

I have tried disabling my theme and the plugins, I have tried removing the majority of the code from my functions.php file and I have tried back-dating my version of WooCommerce views by two versions, but none of these have resolved the issue.

I can only suspect it's something to do with the shortcode as that's the only part of the journey I've been unable to investigate. Please can you help me debug what may be causing this issue?

/*
 * 
 * Add referrer data to cart item.
 * 
*/
function add_trip_and_referrer_to_cart_item( $cart_item_data, $product_id, $variation_id ) {

	error_log("Begin...");
	$URLParam = $_GET["referrer"];
	error_log(print_r($URLParam,true));
	
	if(!empty($_GET["referrer"])){
		
		   error_log("Referrer ID set in URL Parameter");
		   $referrerParam = $_GET["referrer"];
		
		// Check if Referrer is an approved agency for current Trip
		   $tripId = toolset_get_related_post( $product_id, 'product-for-a-trip', 'parent');
		   error_log(print_r($tripId,true));
		   error_log("Approved Agencies...");
		   $approvedAgenciesRaw = types_render_field('approved-agencies-trip-level',array('id' => $tripId, 'separator' => ','));
		   $approvedAgenciesArray = explode(',', $approvedAgenciesRaw);
		   error_log(print_r($approvedAgenciesArray,true));
		
		   if (in_array( $referrerParam, $approvedAgenciesArray )) {
			   
			   // Referrer Id is that of an approved agency
			      error_log("Referrer ID in URL Parameter IS an Approved Agency");
			   // Use Branding of the Referring Agency (URL Parameter)
			      $referrerId = $referrerParam;
			   
		   } else {
			   
			   // Referrer Id is NOT that of an approved agency
			   // Use Branding of Trip Vendor (Post Author)
		   		  error_log("Referrer ID in URL Parameter is NOT an Approved Agency");
		
			   // Get Trip Vendor (Post Author) ID
			   // $productId = $cart_item['product_id'];
		   		  $tripId = toolset_get_related_post( $product_id, 'product-for-a-trip', 'parent');
			   // Set Referrer as product Vendor
	       		  $referrerId = get_post_field( 'post_author', $tripId );
		
		   		//  error_log("Vendor Id...");
		   		//  error_log(print_r($referrerId,true));
			   
		   }
		   error_log("Referrer Id...");
		   error_log(print_r($referrerId,true));
		
	} else {
				
		// Referrer Id is NOT Specified
		// Use Branding of Trip Vendor (Post Author)
		   error_log("Referrer ID NOT set in URL Parameter");
		
		// Get Trip Vendor (Post Author) ID
		// $productId = $cart_item['product_id'];
		   $tripId = toolset_get_related_post( $product_id, 'product-for-a-trip', 'parent');
		// Set Referrer as product Vendor
	       $referrerId = get_post_field( 'post_author', $tripId );
		
		//   error_log("Vendor Id...");
		//   error_log(print_r($referrerId,true));
		
	}
	
    $referrerName = get_user_meta($referrerId,'nickname',true);
	error_log(print_r($referrerName,true));
	
    $cart_item_data['referrer-id'] = $referrerId;
	$cart_item_data['referrer-name'] = $referrerName;
  
    return $cart_item_data;
}

add_filter( 'woocommerce_add_cart_item_data', 'add_trip_and_referrer_to_cart_item', 10, 3 );

To recreate the issue, log in and go to hidden link. On this page you'll see a product listed with the 'Add to Cart' button. If you click 'Add to Cart', the order item in the cart should then have a meta line that reads "Agency:
Tall Ships America", the Nickname of the user account with ID of 2, which was the ID specified by the URL parameter.

In wp-admin the shortcode is used at hidden link

The debug log should be available to you in the /wp-content folder with the attached ftp credentials.

Thanks in advance!

#1366043

Hello,

There isn't such kind of feature within Toolset Plugins, the shortcode [wpv-woo-buy-or-select] can not pass custom URL parameters, see our document:
https://toolset.com/documentation/user-guides/views-shortcodes/#wpv-woo-buy-or-select

In your case, you might consider custom codes, for example, you can get the full referrer URL with PHP variable $_SERVER['HTTP_REFERER']
hidden link

Then parse above URL, find the URL parameter "referrer", and use it in your custom PHP codes.
hidden link

For your reference.