Created
October 19, 2017 22:25
-
-
Save szlegradi/e3a586fad1ae39ee09f0690b511a130f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function bbloomer_add_gift_if_sku_added_cart( $passed, $product_id, $quantity ) { | |
global $woocommerce; | |
/* enter array of SKUs that give gift */ | |
$skuswithgift = array('KCF-N-ATC', 'armsup'); | |
/* enter SKU of product gift */ | |
$giftsku = 'ajandek'; | |
/* enter name of coupon that gives 100% discount for specific product gift */ | |
$coupon_code = 'kedvezmeny'; | |
$product = wc_get_product( $product_id ); | |
$category_slugs = wp_get_post_terms( $product_id, 'product_cat', array( 'fields' => 'slugs' ) ); | |
if ( | |
( $product->get_sku() && in_array( $product->get_sku(), $skuswithgift ) ) || | |
in_array( 'kategoria-slug', $category_slugs ) | |
) { | |
WC()->cart->add_to_cart( wc_get_product_id_by_sku( $giftsku ) ); | |
wc_add_notice( __( 'Az ajándék terméket a kosárba helyeztük!', 'woocommerce' ), 'success' ); | |
$woocommerce->cart->add_discount( $coupon_code ); | |
} | |
return $passed; | |
} | |
add_filter( 'woocommerce_add_to_cart_validation', 'bbloomer_add_gift_if_sku_added_cart', 10, 3 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment