Skip to content

Instantly share code, notes, and snippets.

@szlegradi
Created October 19, 2017 22:25
Show Gist options
  • Save szlegradi/e3a586fad1ae39ee09f0690b511a130f to your computer and use it in GitHub Desktop.
Save szlegradi/e3a586fad1ae39ee09f0690b511a130f to your computer and use it in GitHub Desktop.
<?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