Skip Navigation

[Resolved] Unable to send custom cart item data through wpv-woo-buy-or-select shortcode

This thread is resolved. Here is a description of the problem and solution.

Problem:
Unable to send custom cart item data through wpv-woo-buy-or-select shortcode

Solution:
You should use the WooCommerce hook woocommerce_add_cart_item_data and woocommerce_get_item_data to setup add to cart item custom data.

You can find the proposed solution in this case with the following reply:
https://toolset.com/forums/topic/unable-to-send-custom-cart-item-data-through-wpv-woo-buy-or-select-shortcode/#post-1262573

Relevant Documentation:

This support ticket is created 5 years, 5 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
- 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10: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/Kolkata (GMT+05:30)

Tagged: 

This topic contains 11 replies, has 2 voices.

Last updated by jonB-5 5 years, 5 months ago.

Assisted by: Minesh.

Author
Posts
#1262167

My products are added to the cart via the [wpv-woo-buy-or-select] shortcode contained within a view that lists all products related to the current post.

On the parent post page where the products view is shown I have a variable defined via a shortcode (below).

// Define Global Variable
	if (isset($_GET["referrer"])) { // If Referrer is Set in URL Parameter 
		
    	// Get Referrer User Id from URL Parameter
    	$referrerId = $_GET["referrer"];
	
	} else { // Set store branding to product Vendor
		
		$referrerId = get_the_author_meta('ID');
		
	}

I have a custom product field "referrer-id".

When a customer adds a product to the cart, I need to save my global variable to the $cart_item_data (I think in the field "referrer-id") so that I can show who referred that user to the product.

I'm trying to achieve this using the code below...

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

    global $rererrerId;

    $cart_item_data['referrer-id'] = $rererrerId;
 
    return $cart_item_data;
}
 
add_filter( 'woocommerce_add_cart_item_data', 'add_referrer_to_cart_item', 10, 3 );

/*
 * Display referrer info in the cart.
 */
function display_referrer_in_cart( $item_data, $cart_item ) {
    if ( empty( $cart_item['referrer-id'] ) ) {
        return $item_data;
    }
 
    $item_data[] = array(
        'key'     => __( 'Referrer' ),
        'value'   => wc_clean( $cart_item['referrer-id'] ),
        'display' => '',
    );
 
    return $item_data;
}
 
add_filter( 'woocommerce_get_item_data', 'display_referrer_in_cart', 10, 2 );

The problem I have is that the variable doesn't appear to be being found by the function. In the Debug log I noticed that even if the global variable function appears before the other function in my function.php file, they output in the wrong order, so the global variable seems to be defined AFTER the function is executed, not before, meaning the function can't find the variable.

My understanding is limited here, but I suspect it's something to do with the [ wpv-woo-buy-or-select] shorcode template being called before the variable is defined?

Can you advise on what the issue may be and/or suggest a possible solution?

#1262219

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Hello. Thank you for contacting the Toolset support.

Well - Toolset Forms commerce offers the API hooks that you would like to try and play with it if that helps you to achieve what you are looking for: cred_commerce_after_add_to_cart
=> https://toolset.com/documentation/programmer-reference/cred-commerce-api/#cred_commerce_after_add_to_cart

#1262247

Hi Minesh,

As far as I understand it, Cred Commerce doesn't have the ability to allow multiple different items to be added to a cart, which is a key requirement of my system (do correct me if I'm wrong). I also have a multi-language multi-currency setup via WPML and WCML which I suspect may cause further issues if I used cred commerce?

#1262275

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

By default, Toolset Forms Commerce allows adding one product to the cart that is why is no way to add multiple products with CRED commerce

If you want to add multiple products to your cart - you may try to use the following WooCommerce Doc:
=> https://docs.woocommerce.com/document/automatically-add-product-to-cart-on-visit/

This needs custom programming and customizations which is beyond the scope of our support policy, However, I may suggest you to submit the new feature request to allow multiple products to cart using the following link:
=> https://toolset.com/home/contact-us/suggest-a-new-feature-for-toolset/

Or
Try to find a plugin that helps you better in this case, for example:
=> https://wordpress.org/plugins/woocommerce-bulk-order-form/

#1262291
Screenshot 2019-06-07 at 12.10.58.png

Hi Minesh,

Unfortunately that wouldn't solve the problem as it's not the functionality I need.

I can see that your shortcode [wpv-woo-buy-or-select] includes the data I need in the <form> 'action' attribute.

action="/trips/29/?referrer=5&add-to-cart=162"

is there any way to extract the 'referrer' parameter value and use that as the variable in the function I'm executing on woocommerce_add_cart_item_data?

#1262301

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

if you can share access details I will try to check but please note that there is no native solution but I would love to help you so I will give a try.

Please share problem URL and access details.

*** Please make a FULL BACKUP of your database and website.***
I would also eventually need to request temporary access (WP-Admin and FTP) to your site. Preferably to a test site where the problem has been replicated if possible in order to be of better help and check if some configurations might need to be changed.

I have set the next reply to private which means only you and I have access to it.

#1262311

You can see the view of products by going to hidden link

The view is editable at hidden link

#1262317

Just a quick one, please could you let me know if you are about to make any changes to functions.php, so that we don't edit at the same time!

#1262321

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Yes - I checked and I do not see any way to help you here, sorry.

Maybe you should contact WooCommerce experts or WooComemrce support they will guide you better on this issue and if you found the solution, please feel free to add to this ticket so other users may get the benefit of the solution shared.

#1262411

Hi Minesh,

Is there any way to add a parameter to the URL that your shortcode [wpv-woo-buy-or-select] outputs in its action attribute?

#1262417

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Well - maybe you can try custom javascript/jQuery to add such param.

For example:
https://stackoverflow.com/questions/49061360/how-to-pass-selected-parameters-to-a-form-action-url-via-jquery-or-javascript

#1262573

Hi Minesh,

Thank You for the JS suggestion and the link, these became useful in finding the actual issue and will certainly be useful in future!

It turns out that the URL parameters aren't found by the php functions I created only when using the AJAX version of the Add to Cart Button.

To solve the problem I simply went to Woocommerce -> Settings and switched off 'Enable AJAX add to cart buttons on archives'. I'm now able to see the data passed through to the cart items successfully using the code below...

/*
 * Add referrer data to cart item.
*/
function add_referrer_to_cart_item( $cart_item_data, $product_id, $variation_id ) {
 
	$referrerId = $_GET['referrer'];
 	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;
  
    return $cart_item_data;
}
  
add_filter( 'woocommerce_add_cart_item_data', 'add_referrer_to_cart_item', 10, 3 );
 
/*
 * Display referrer info in the cart.
 */
function display_referrer_in_cart( $item_data, $cart_item ) {
    if ( empty( $cart_item['referrer-id'] ) ) {
        return $item_data;
    }
  
    $item_data[] = array(
        'key'     => __( 'Referrer' ),
        'value'   => wc_clean( $cart_item['referrer-id'] ),
        'display' => '',
    );
  
    return $item_data;
}
  
add_filter( 'woocommerce_get_item_data', 'display_referrer_in_cart', 10, 2 );

Thank You for your time!