php - WooCommerce run function/action after every add to cart
Get the solution ↓↓↓On a Woocommerce site, I want to show a different cart icon once the cart is not empty. The cart is updated via Ajax, so the check has to run after every add to cart and the change should be permanent (Cart Icon "empty" as long as cart is empty, Cart Icon "full" as long as cart is full).
The following code works (the else state is missing here, so only the "full" (= voll) icon is added once the cart has more than one item, else loop would work similarly), but only on the cart page, because the hook is of course limited to the cart page. I've been trying to get it to work with other hooks, mainly based on Which Hook is running after Woocommerce update cart button action, but I'm doing something wrong.
add_action( 'woocommerce_check_cart_items', 'full_cart') ;
function full_cart() {
if( !WC()->cart->is_empty() ) : ?>
<style type="text/css">
.ion-ios-cart::before{
content: url('../wp-content/uploads/bierkiste-v01-voll.svg') !important;
}
</style>
<?php
endif;
}
Any suggestion on how I can run this code whenever a product is added and keep the change until the status changes (cart is empty or cart is not empty)?
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: using $this when not in object context
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.