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!