Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save FrancoStino/17392af1597c5b0d608943d66eb6fbbd to your computer and use it in GitHub Desktop.
Save FrancoStino/17392af1597c5b0d608943d66eb6fbbd to your computer and use it in GitHub Desktop.
Add Banner coupon woocommerce into single page, cart and checkout with expiration time
<?
/**
* Add Banner coupon woocommerce into single page, cart and checkout with expiration time
*/
function custom_message_before_single_product() {
global $woocommerce;
$coupon_code = 'casabella21'; // Coupon
$c = new WC_Coupon($coupon_code);
$date_expiration = strtotime("30-4-2021"); // Data di scadenza
$today = strtotime("today midnight"); // Data di oggi
if($today <= $date_expiration && !in_array($coupon_code, $woocommerce->cart->get_applied_coupons())){ ?>
<div class="banner_coupon">Sconto aggiuntivo del <?php echo $c->amount ?>% con il codice:
<span class="scissors_coupon">✂</span>
<a href="#" onclick="CopyToClipboard('code_coupon');return false;"><span id="code_coupon"><?php echo $coupon_code ?></span></a>
</div>
<style>
.banner_coupon {
border: 2px dashed #000;
text-align: center;
margin: 0 0 20px 0;
padding: 2em;
background: linear-gradient(90deg,rgba(0, 0, 0, 1) 0%,rgba(190, 143, 117, 1) 100%);
border-radius: 5px;
color: #fff;
font-size: 1.2em;
position: relative;
}
.scissors_coupon {
position: absolute;
top: -18px;
right: 0;
font-size: 21px;
color: #000;
}
#code_coupon {
display: block;
border: 1px dashed #fff;
width: 130px;
padding: 5px;
margin: 20px auto;
text-transform: uppercase;
font-size: 16px;
color: #fff;
}
</style>
<script>
function CopyToClipboard(id)
{
var r = document.createRange();
r.selectNode(document.getElementById(id));
window.getSelection().removeAllRanges();
window.getSelection().addRange(r);
document.execCommand('copy');
window.getSelection().removeAllRanges();
}
</script>
<?php
} else {
return false;
}
}
add_action( 'woocommerce_single_product_summary', 'custom_message_before_single_product', 5 ); // For single product page
add_action( 'woocommerce_before_cart_table', 'custom_message_before_single_product', 10); // For cart page table
add_action( 'woocommerce_before_checkout_form', 'custom_message_before_single_product', 10); // For checkout page
add_action( 'woocommerce_archive_description', 'custom_message_before_single_product', 10 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment