daily pastebin goal
68%
SHARE
TWEET

WooCommerce - Auto add item to cart

lorro Jun 11th, 2015 (edited) 237 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?php
  2.   // Auto add-to-cart
  3.   // code goes in functions.php for your child theme
  4.   add_action( 'init', 'add_product_to_cart' );
  5.   function add_product_to_cart() {
  6.     if ( ! is_admin() ) {
  7.       global $woocommerce;
  8.       $product_id = 30; // your product ID here
  9.       $free_product_id = 2287; // your free product ID here
  10.       $found = false;
  11.       // check if product already in cart
  12.       if ( sizeof( $woocommerce->cart->get_cart() ) > 0 ) {
  13.         foreach ( $woocommerce->cart->get_cart() as $cart_item_key => $values ) {
  14.           $_product = $values['data'];
  15.           if ( $_product->id == $product_id ){
  16.             $found = true;
  17.           }
  18.         }
  19.         // if product not found, add it
  20.         if ( $found ) $woocommerce->cart->add_to_cart( $free_product_id );
  21.       }
  22.     }
  23.   }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top