Advertisement
lorro

WooCommerce - Auto add item to cart

Jun 11th, 2015
354
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.80 KB | None | 0 0
  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.   }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement