Home Forums WoodMart support forum Free product in cart

Free product in cart

Viewing 8 posts - 1 through 8 (of 8 total)
  • Author
    Posts
  • #613505

    dweb360
    Participant

    Hello
    For internal management reasons; I have to add a free product to the cart each time a customer buy a product.
    This is done via :

    
    function add_free_product_with_two_items() {
    
    // Define the ID of the free product
    $free_product_id = 20320; // Replace with your actual product ID
    
    // Set the minimum number of products required to trigger the free gift
    $minimum_product_count = 1;
    
    // Retrieve cart items
    $cart = WC()->cart->get_cart();
    
    // Initialize variables
    $non_free_product_count = 0;
    $free_product_exists = false;
    
    // Loop through each item in the cart
    foreach ($cart as $cart_item) {
       
      // Check if the item is not the free product
         if (!isset($cart_item['custom_price'])) {
         
       // Count non-free products
             $non_free_product_count++;
         } else {
     
      // Check if the free product is already in the cart
     $free_product_exists = true;
         }
    }
    
    // If the conditions are met, add the free product to the cart
    if ($non_free_product_count >= $minimum_product_count && !$free_product_exists) {
         WC()->cart->add_to_cart($free_product_id, 1, 0, array(), array('custom_price' => 0));
    }
    
    }
    
    // Hook the function to the WooCommerce calculation
    
    add_action('woocommerce_before_calculate_totals', 'add_free_product_with_two_items');
    

    How to hide product and price and quantity from the cart?

    Thank you

    Attachments:
    You must be logged in to view attached files.
    #613756

    Hung Pham
    Keymaster

    Hi dweb360,

    Thanks for reaching to us.

    Please add the below Custom CSS code to Theme Settings > Custom CSS > Global Custom CSS:

    .product_list_widget>li>a.cart-item-image,
    .product_list_widget .cart-info span.quantity{
        display: none;
    }

    Regards,

    #613761

    dweb360
    Participant

    Hello,
    Thank you for the code but I need to remove images … only for this particular product not for all products in the cart.
    Thank you

    #613915

    Hung Pham
    Keymaster

    Hi dweb360,

    Please remove previous code and use below one:

    .product_list_widget>li> a[data-product_id="20320"] + a.cart-item-image,
    .product_list_widget .cart-info span.quantity{
        display: none;
    }

    Regards,

    #613971

    dweb360
    Participant

    Hello
    Thank you for the code, working on the cart but not on the side cart.
    Thank you

    #614076

    Hung Pham
    Keymaster

    Hi dweb360,

    Please provide URL of the mentioned page, so I can take a closer look.

    Regards,

    #614215

    dweb360
    Participant
    #614737

    Hung Pham
    Keymaster

    Hi dweb360,

    I checked your site and saw that thumbnails and other details have been removed as you wanted https://ibb.co/vwGjy5C

    Let me know if you resolved.

    Regards,

Viewing 8 posts - 1 through 8 (of 8 total)