Skip to content

Instantly share code, notes, and snippets.

@Xartok
Created August 18, 2015 18:02
Show Gist options
  • Select an option

  • Save Xartok/d5d067ca5ec8a16baf94 to your computer and use it in GitHub Desktop.

Select an option

Save Xartok/d5d067ca5ec8a16baf94 to your computer and use it in GitHub Desktop.
function set_first_post_proc_free() {
global $woocommerce;
$coupon_monthly = "oppf1";
$coupon_yearly = "oppf2";
$items = $woocommerce->cart->get_cart();
foreach ($items as $cart_item_key => $values) {
$product = $values["data"];
if(!product_is_of_type($values["product_id"], "software")) {
return;
}
// look at the billing period
if($product->is_type( "variation" )) {
$variation = new WC_Product_Variation($values["variation_id"]);
if($variation->get_formatted_name() === "Monthly") {
$coupon = $coupon_monthly;
}
else if($variation->get_formatted_name() === "Yearly") {
$coupon = $coupon_yearly;
}
}
$x = $values["quantity"];
for($i = 0; $i < x; $i ++) {
$woocommerce->cart->add_discount( $coupon );
}
$woocommerce->show_messages();
}
}
add_action( "woocommerce_before_cart", "set_first_post_proc_free" );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment