Answer the question
In order to leave comments, you need to log in
How to show a hidden item in the cart, or give a gift to the user?
You need to do this: When adding at least one product to the woocommerce cart, add a gift to it, just without a description and without anything, but so that the client sees that he will have some kind of gift upon delivery or purchase.
I decided to create a hidden product, and when checking the cart, add it there.
But there was a problem, the gift is not displayed in the list of goods in the basket, and the inscription hangs above the list
This item cannot be purchased.
function addGiftToCart() {
$gift_id = 2299;
if ( WC()->cart->get_cart_contents_count() !== 0 ) {
wc_print_notice( __( 'Мы дарим Вам подарок!', 'woocommerce' ), 'notice' );
WC()->cart->add_to_cart( $gift_id );
}
}
add_action( 'woocommerce_check_cart_items', 'addGiftToCart' );
Answer the question
In order to leave comments, you need to log in
In general, while digging, everything began to work.
There are suggestions that it is necessary to set the price for the product, even 0, and then it will appear in the basket.
Just in case, I will give the code below, suddenly when the thread is useful to someone:
function productAlreadyInCart($gift_id) {
global $woocommerce;
$items = $woocommerce->cart->get_cart();
foreach($items as $item => $values) {
$_product = $values['data']->get_id();
if ($_product === $gift_id) return true;
}
return false;
}
function addGiftToCart() {
$gift_id = 2299;
if ( WC()->cart->get_cart_contents_count() !== 0 ) {
if ( !productAlreadyInCart($gift_id) ) {
wc_print_notice( __( 'Мы дарим Вам подарок!', 'woocommerce' ), 'notice' );
WC()->cart->add_to_cart( $gift_id );
}
}
}
add_action( 'woocommerce_check_cart_items', 'addGiftToCart' );
Can't a product be "purchased" if we pass its quantity of 0 units and without specifying its cost?
$WC_Cart = new WC_Cart();
$var = $WC_Cart->add_to_cart( $product_id, $quantity, $variation_id, $variation, $cart_item_data );
How about that idea.
Make the product visible, put it in a separate category ("gifts" category), which is not displayed on the site, and add it.
Make the price of the product symbolic.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question