Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?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;
- $product_id = 30; // your product ID here
- $free_product_id = 2287; // your free product ID here
- $found = false;
- // check if 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 == $product_id ){
- $found = true;
- }
- }
- // if product not found, add it
- if ( $found ) $woocommerce->cart->add_to_cart( $free_product_id );
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement