Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save andreiglingeanu/5ac37401c66edb55e870e0e94c74e230 to your computer and use it in GitHub Desktop.
Save andreiglingeanu/5ac37401c66edb55e870e0e94c74e230 to your computer and use it in GitHub Desktop.
<?php
add_filter(
'blocksy:pro:custom-post-type:output-content',
function ($content, $id) {
$BLOCKSY_THANK_YOU_PAGE_ID = 2478;
if ($id !== $BLOCKSY_THANK_YOU_PAGE_ID) {
return $content;
}
global $wp;
if (! isset($wp->query_vars['order-received'])) {
return $content;
}
$order = false;
$order_id = $wp->query_vars['order-received'];
$order_id = apply_filters( 'woocommerce_thankyou_order_id', absint( $order_id ) );
$order_key = apply_filters( 'woocommerce_thankyou_order_key', empty( $_GET['key'] ) ? '' : wc_clean( wp_unslash( $_GET['key'] ) ) ); // WPCS: input var ok, CSRF ok.
if ($order_id > 0) {
$order = wc_get_order($order_id);
if (
! $order
||
! hash_equals($order->get_order_key(), $order_key)
) {
$order = false;
}
}
if (! $order) {
return $content;
}
$billing_first_name = $order->get_billing_first_name();
$coupon = new WC_Coupon();
$coupon->set_code( $billing_first_name . '10' ); // Coupon code will contain billing first name and 10. For example: John20
$coupon->set_amount( 10 ); // Discount amount
$coupon->set_discount_type( 'percent' ); // Discount type can be 'fixed_cart', 'percent' or 'fixed_product', defaults to 'fixed_cart'
$coupon->set_usage_limit( 1 ); // usage limit
$coupon->save();
$coupon_content = '<p>Täname ostu eest. Tänutäheks kingime sulle personaalse sooduskupongi: ' . $billing_first_name . '10 mis annab sulle 10% soodustust järgmiselt ostult.</p>';
return $coupon_content . $content;
},
10,
2
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment