Kendi ihtiyacım için yazmış olduğum otomatik kupon kodu tanımlamasını aşağıdaki adımları takip ederek kendi sitenize entegre edebilirsiniz.
Bu işlemi eklenti ile de yapabilmektesiniz, dilerseniz eklentiyi kullanarak yapabilirsiniz.
Biz eklentisiz bir şekilde yapıcaz, sebebi ise; sitemizde ne kadar çok eklenti olursa, sitemizin performansı buna bağlı olarak düşer.
Bu sebeple eklentisiz bir şekilde ekleyeceğiz fakat eklenti kullanarak yapmak isteyenler, eklentiye buradan ulaşabilirler.
Aşağıdaki kodu functions.php içerisine ekliyoruz.
"strpos($_SERVER['REQUEST_URI'], 'odeme') !== false" buradaki odeme kısmını ödeme sayfanızın url yapısı nasılsa o şekilde değiştirmelisiniz.
"$cart_template" kısmını eğer sepet ve ödeme sayfasında woocommerce'in altyapısını kullanıyorsanız woocommerce olarak bırakabilirsiniz.
WooCommerce altyapısını kullanmıyorsanız, cartflows kullanıyorsanız orayı cartflows olarak değiştirmelisiniz.
Kodu ekledik ve düzenledik. Peki, müşterilerimizle linki nasıl paylaşacağız.
Aşağıdaki linkteki /odeme/ kısmını ödeme sayfanızın url yapısı neyse o şekilde değiştiriyoruz.
Son olarak; KUPON_KODUNUZ kısmını oluşturduğunuz kupon kodunuzu yazmanız gerekiyor.
Örnek link: https://siteadiniz.com/odeme/?coupon_code=KUPON_KODUNUZ
if(strpos($_SERVER['REQUEST_URI'], 'odeme) !== false):
if(!function_exists('wp_apply_auto_coupon_code')):
function wp_apply_auto_coupon_code(){
$get_coupon_code = isset($_GET['coupon_code']) ? $_GET['coupon_code'] : '';
$cart_template = 'woocommerce'; // woocommerce, cartflow
if($get_coupon_code):
?>
<script>
window.addEventListener('load', function(){
const couponInput = document.querySelector('#coupon_code');
const couponButton = document.querySelector('[name="apply_coupon"]');
if(couponInput.value < 0) return
couponInput.value = '<?php echo $get_coupon_code ?>';
<?php if($cart_template == 'woocommerce'): ?>
couponButton.click();
<?php elseif($cart_template == 'cartflow'): ?>
const submitCouponButton = document.querySelector('.wcf-submit-coupon');
const cartflow_submit_coupon = () => {
const coupon_wrapper_class = jQuery( '.wcf-custom-coupon-field' ),
coupon_wrapper = jQuery( this ).closest( coupon_wrapper_class ),
coupon_field = coupon_wrapper.find( '.wcf-coupon-code-input' ),
coupon_value = jQuery('#coupon_code').val();
if ( '' === coupon_value ) {
coupon_field.addClass( 'field-required' );
return false;
}
coupon_field.removeClass( 'field-required' );
const data = {
coupon_code: coupon_value,
action: 'wcf_woo_apply_coupon',
security: cartflows.wcf_validate_coupon_nonce,
};
jQuery.ajax( {
type: 'POST',
url: cartflows.ajax_url,
data,
success( code_data ) {
const coupon_message = jQuery( '.wcf-custom-coupon-field' );
coupon_message
.find( '.woocommerce-error, .woocommerce-message' )
.remove();
if ( code_data && code_data.status === true ) {
jQuery( document.body ).trigger( 'update_checkout', {
update_shipping_method: false,
} );
coupon_message.prepend( code_data.msg );
coupon_wrapper_class.addClass( 'wcf-coupon-applied' );
} else if ( code_data && code_data.msg ) {
coupon_message.prepend( code_data.msg );
coupon_wrapper_class.removeClass(
'wcf-coupon-applied'
);
} else {
console.log(
'Error: Error while applying the coupon. Response: ' +
code_data.data && code_data.data.error
? code_data.data.error
: code_data
);
}
},
} );
}
cartflow_submit_coupon();
<?php endif; ?>
});
</script>
<?php
endif;
}
endif;
add_action('wp_footer', 'wp_apply_auto_coupon_code');
endif;