Support » Plugin: WooCommerce » Add Free Item to Cart Per Order

  • Resolved TPOH-AMeyer

    (@tpoh-ameyer)


    We’d like to add a free item to every order placed. It will be a special product code and we’d like to lock the quantity to one.

    This will allow us to bundle the free gift to every order, regardless of what quantity of other items are in the shopping cart.

    Can the WooComm plugin handle this?

    Thanks!

    https://wordpress.org/plugins/woocommerce/

Viewing 7 replies - 1 through 7 (of 7 total)
  • https://wordpress.org/plugins/pricing-deals-for-woocommerce/

    Plugin has an auto-add free item function, you can set limits to occur once per cart…

    (I’m just in the process of revising the auto-add function, making it more efficient…)

    The plugin should be easier, or this code snippet should do it:

    <?php
      // Auto add-to-cart
      // code goes in functions.php for your child theme
      add_action( 'init', 'add_product_to_cart' );
      function add_product_to_cart() {
        if ( ! is_admin() ) {
          global $woocommerce;
          $free_product_id = 2287; // your free product ID here
          $found = false;
          // check if free product already in cart
          if ( sizeof( $woocommerce->cart->get_cart() ) > 0 ) {
            foreach ( $woocommerce->cart->get_cart() as $cart_item_key => $values ) {
              $_product = $values['data'];
              if ( $_product->id == $free_product_id ){
                $found = true;
              }
            }
            // if product not found, add it
            if ( $found ) $woocommerce->cart->add_to_cart( $free_product_id );
          }
        }
      }

    However, it won’t stop a customer getting another free item on their next order.

    where do i add this snippet to make it work??

    also were would the free product ID come from is the the defined SKU or would it be the ID from the MySQL database? if so which table and which attribute would i want to use, sorry i’m new to woocommerce but i have some wordpress experience… help me if you can please thanks!

    oh duh functions.php in my child theme

    If you bring up your list of products page and hover over your free product, its id will appear.

    Well, I’ve thought better of it. The code does not remove the free product if the customer removes the paid product from the cart. So for completeness it would be necessary to tap into remove-product-from-cart with another function to do that.

    For this function, it would be necessary to look through the cart and add up the number of free products and the number of paid products, then either add or deduct a number of free products as appropriate.

    A bit too much code for a forum answer for me.

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘Add Free Item to Cart Per Order’ is closed to new replies.