Site logo

Archived topic

Manipulating cart after sticky atc submit

10 replies · Started by Danny on June 29, 2021

Viewing posts 1–11 of 11

Hi,

I have a certain function which conditionally makes changes in the cart hooked into the following 2 actions:

woocommerce_before_cart
woocommerce_before_mini_cart

But, this code doesn’t run when the sticky add to cart panel is used. I was trying to figure out what action does it invoke that could be used but I ran out of possibilities for further online research. Do keep in mind the code is not live on the site at the moment but I do know the code actually works well, just missing info on how to invoke it after the sticky atc submission.

Important to note that the function does do WC()->cart->add_to_cart and WC()->cart->remove_cart_item operations so that limits the possibilities a bit not to induce an infinite loop.

Any help would be greatly appreciated.

Hi there,

Not exactly sure what's the aim here but I think you'll have to use generate_wc_sticky_add_to_cart_action filter to change things within the sticky add to cart panel.

Here're the default actions -
https://share.getcloudapp.com/o0ueXNJL

Or perhaps it's the generate_wc_cart_panel_checkout_button_output filter you're working on?

Here's the code involved in gist (for your reference) -
https://gist.github.com/ejcabquina/d311cb594c16c577d09d52611d7fafd4

Hi Elvin,

To elaborate further on the current action that happens there.

Basically, when this product (link in private) is added to cart I check for it, and replace it with the actual product but I add a coupon code needed and depending on geo add 2 free products as well. This is something I can't seem to achieve only after the sticky add to cart is clicked. I don't need to change anything visually on it or anything like that.

*I am looking at the references you sent just wanted to quicky explain

Hi there,

what is the function you have hooked in? As I can only assume that the condition is reliant on the default Add to Cart button, it may be a simple case of modifying that condition to include the Sticky Add to Cart button

Hi David,

As per the first message, it is hooked into:

woocommerce_before_cart
woocommerce_before_mini_cart

I need to see the actual function - not where it is hooked into - to determine what it is doing that doesn't work with the sticky add to cart button.

no problem, I didn't paste as it seemed irrelevant since I know it doesn't get triggered which is the core of my issue, function itself is not something I need help with obviously as it works well

function checkProductAvailability(){
	$productIdsToCheck = [238136, 176399, 176400];
	$allProductsPurchasable = true;
	foreach($productIdsToCheck as $productIdToCheck){
		$_product = wc_get_product($productIdToCheck);
		if(!$_product->is_purchasable()){
			$allProductsPurchasable = false;
			break;
		}
	}
	return $allProductsPurchasable;
}

function fs_apply_discount_to_cart() {
	//check for flash sale product
	$product_id = 238136;
	$in_cart = false;
	
	$prep_in_cart = false;
	$main_in_cart = false;
	
	$replaceQuantity = 1;
	
	$allProductsPurchasable = checkProductAvailability();
	
	foreach( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
		$product_in_cart = $cart_item['product_id'];
		if ( $product_in_cart === $product_id ){
			$in_cart = true;
			$replaceQuantity = $cart_item['quantity'];
			//remove flash sale product
			WC()->cart->remove_cart_item( $cart_item_key );
		}
		if ( $product_in_cart === 176399 ){
			$prep_in_cart = true;
		}
		
		if ( $product_in_cart === 176400 ){
			$main_in_cart = true;
		}
		
	}

	if ( $in_cart ) {
		if($allProductsPurchasable){
			$coupon_code = '4THJULYEARLY';
		}else{
			$coupon_code = '4THJULYEARLYROTW';
		}
		
		
		WC()->cart->add_to_cart( "52", $replaceQuantity );
		
		//single kit doesn't get free shampoos, check AS9 quantity before adding
		$asixQuantity = 1;
		foreach( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
			$product_in_cart = $cart_item['product_id'];
			if ( $product_in_cart === 52 ){
				$asixQuantity = $cart_item['quantity'];
			}
		}
		
		if($asixQuantity > 1){
			//remove all coupons
			WC()->cart->remove_coupons();
			//add sale code
			WC()->cart->add_discount(sanitize_text_field($coupon_code));

			//add free shampoos
			//176399 Ceramic Prep Shampoo
			//176400 Ceramic Maintenance Shampoo
			if(!$prep_in_cart && $allProductsPurchasable){
				WC()->cart->add_to_cart( "176399", 1 );
			}
			if(!$main_in_cart && $allProductsPurchasable){
				WC()->cart->add_to_cart( "176400", 1 );
			}
		}
	}
}

add_action( 'woocommerce_before_cart', 'fs_apply_discount_to_cart' );
add_action( 'woocommerce_before_mini_cart', 'fs_apply_discount_to_cart' );
add_action( 'woocommerce_review_order_before_cart_contents', 'fs_apply_discount_to_cart' );

//------------------- css switch -------------------------//

function switchBasedOnProductAvailability(){
	$allProductsPurchasable = checkProductAvailability();
	if(!$allProductsPurchasable){
		echo "<style>.md_rotw_show{display:revert !important;}.md_rotw_hide{display:none !important;}</style>";
	}
}
add_action("wp_head", 'switchBasedOnProductAvailability');

I assumed your code isn't working because the conditions it sets are not being met when the Sticky Add to Cart Form actions are complete. It uses a different form action and button submit to the default add to cart. But looking at your function its testing for items in cart - so i am not sure if that is the issue.

I am going to ask Tom to take a look to see if he can spot what may be going on.

Hi there,

So these functions aren't firing when you add something to your cart on the Shop page when using the sticky add to cart panel? What about without the panel?

From what I can see there, your code will only fire when those hooks are available on the page. That should be:

woocommerce_before_cart - in the cart (no sticky add to cart panel there)
woocommerce_before_mini_cart - when the mini cart widget is active (I'm assuming)

What if you use a more generic hook?

add_action( 'wp', function() {
    if ( function_exists( 'is_woocommerce' ) && is_woocommerce() ) {
        // your code here
    }
} );

Hi Tom,

So, the function fires on any add to cart, quantity adjustment etc except when the sticky add to cart button is used. Now, because a lot of this happens via ajax I am not sure above would work (I will try it thou and will let you know).

That's strange, I wouldn't think those hooks would fire (especially if using AJAX to add to cart). They look like regular on-page hooks.

This archived topic is closed to new replies.