-
-
Save bulentsakarya/1badfe3d2a933d42741ca2541fec571c to your computer and use it in GitHub Desktop.
Woocommerce ödeme sayfasında bireysel müşteriler için T.C. numarası kurumsal müşteriler için vergi numarası alanını ekleme.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
* Çalışma koşulu şu şekilde; | |
* Ödeme ekranına geldiğinde müşteri varsayılan olarak T.C. alanını görecek ama araştırdığım ve öğrendiğim kadarıyla sadece 5000 TL ve üstü siparişler için T.C. numarası zorunluymuş bu yüzden zorunlu olmayacak. | |
* Zorunlu olmamasına rağmen eğer müşteri buraya yanlış bir T.C. yazarsa doğrulamadan geçecek ve eğer yazmak istemiyorsa boş bırakması için uyarı mesajı gelecek | |
* Eğer firma adına fatura isterse Kurumsal Sipariş Checkbox'ını işaretleyerek T.C. alanı gizlenecek ve vergi dairesi ile vergi numarası alanı gözükecek ve bu alanlar zorunlu olacak | |
* Not: T.C. numarası ve Vergi Numarası doğrulamadan geçiyor. | |
* Eksikler: T.C. Numarasını boş bırakabildikleri için veri tabanına o sipariş için 11111111111 yazdırmayı başaramadım. -> | |
* Ama 147. satırdaki kod sipariş ekranında bu bilgiyi gösteriyor, bunu nasıl yaparız bilen varsa yazsın -> | |
* Aslında 'default' => 11111111111 ile veritabanına bunu yazdırabilirim ama bunu set edince sipariş ekranında bu numaranın gözükmesi hoşuma gitmiyor. | |
*/ | |
//T.C. No, Vergi No ve Vergi Dairesi Alanlarını Oluştur | |
add_filter('woocommerce_checkout_fields', 'tc_vergi_icin_new_checkout_field'); | |
function tc_vergi_icin_new_checkout_field($fields) | |
{ | |
$fields['billing']['checkbox_trigger'] = array( | |
'type' => 'checkbox', | |
'priority' => 8, | |
'label' => __('Kurumsal Sipariş', 'woocommerce'), | |
'class' => array('form-row-wide'), | |
'clear' => true | |
); | |
$fields['billing']['billing_vergi_dairesi'] = array( | |
'label' => __('Vergi Dairesi', 'woocommerce'), | |
'placeholder' => _x('Vergi Dairesi', 'placeholder', 'woocommerce'), | |
'class' => array('form-row form-row-first'), | |
'required' => true, | |
'priority' => 31, | |
'clear' => true | |
); | |
$fields['billing']['billing_vergi_nosu'] = array( | |
'label' => __('Vergi No', 'woocommerce'), | |
'placeholder' => _x('Vergi No', 'placeholder', 'woocommerce'), | |
'class' => array('form-row form-row-last'), | |
'required' => true, | |
'priority' => 32, | |
'clear' => true | |
); | |
$fields['billing']['shipping_tc'] = array( | |
'label' => __('TC Kimlik No', 'woocommerce'), | |
'placeholder' => _x('5.000,00 TL üzeri alışverişlerde zorunludur', 'placeholder', 'woocommerce'), | |
'class' => array('form-row-wide'), | |
'priority' => 9, | |
'clear' => true | |
); | |
if (!isset($_POST['checkbox_trigger'])) { | |
$fields['billing']['shipping_tc']['required'] = false; | |
$fields['billing']['billing_company']['required'] = false; | |
$fields['billing']['billing_vergi_dairesi']['required'] = false; | |
$fields['billing']['billing_vergi_nosu']['required'] = false; | |
} else { | |
$fields['billing']['shipping_tc']['required'] = false; | |
$fields['billing']['billing_company']['required'] = true; | |
$fields['billing']['billing_vergi_dairesi']['required'] = true; | |
$fields['billing']['billing_vergi_nosu']['required'] = true; | |
} | |
return $fields; | |
} | |
// Vergi No Doğrulama Fonksiyonu | |
function validateTaxNo($taxNo) | |
{ | |
if (strlen($taxNo) == 10) { | |
for ($i = 0; $i < 9; $i++) { | |
$v[$i + 1] = ($taxNo[$i] + (9 - $i)) % 10; | |
$vv[$i + 1] = ($v[$i + 1] * pow(2, (9 - $i))) % 9; | |
$vv[$i + 1] = ($v[$i + 1] != 0 && $vv[$i + 1] == 0) ? 9 : $vv[$i + 1]; | |
} | |
$sum = array_sum($vv); | |
$sum = ($sum % 10 == 0) ? 0 : (10 - ($sum % 10)); | |
return ($sum == $taxNo[9]) ? true : false; | |
} | |
return false; | |
} | |
// TC Doğrula Fonksiyonu | |
function isTcKimlik($tc) | |
{ | |
// Array içindeki denemeler tc kriterine uyduğu için hariç tuttuk. | |
$exclude = array('11111111110', '22222222220', '33333333330', '44444444440', '55555555550', '66666666660', '7777777770', '88888888880', '99999999990'); //Bunlar aşağıdaki fonksiyonu yanıltan numaralar dilerseniz kaldırabilirsiniz satırı. | |
if (in_array($tc, $exclude)) { | |
return false; | |
} | |
if (strlen($tc) < 11) { | |
return false; | |
} | |
if ($tc[0] == '0') { | |
return false; | |
} | |
$plus = ($tc[0] + $tc[2] + $tc[4] + $tc[6] + $tc[8]) * 7; | |
$minus = $plus - ($tc[1] + $tc[3] + $tc[5] + $tc[7]); | |
$mod = $minus % 10; | |
if ($mod != $tc[9]) { | |
return false; | |
} | |
$all = 0; | |
for ($i = 0; $i < 10; $i++) { | |
$all += $tc[$i]; | |
} | |
if ($all % 10 != $tc[10]) { | |
return false; | |
} | |
return true; | |
} | |
// TC Kimlik Noyu Doğrula | |
add_action('woocommerce_checkout_process', 'tc_numara_dogrula'); | |
function tc_numara_dogrula() | |
{ | |
$tcno = $_POST['shipping_tc']; | |
if (!empty($tcno)) { | |
if (!is_numeric($tcno) && !isset($_POST['checkbox_trigger'])) { | |
wc_add_notice(__('T.C alanına lütfen sayısal bir değer girin yada siparişiniz 5.000,00 TL den küçük ise boş bırakın.'), 'error'); | |
} else if (!empty($tcno) && !isset($_POST['checkbox_trigger'])) { | |
if (!isTcKimlik($tcno)) | |
wc_add_notice(__('Lütfen doğru bir TC kimlik no girin yada siparişiniz 5.000,00 TL den küçük ise boş bırakın.'), 'error'); | |
} | |
} | |
} | |
// Vergi Noyu Doğrula | |
add_action('woocommerce_checkout_process', 'vergi_no_dogrula'); | |
function vergi_no_dogrula() | |
{ | |
$vergiNo = $_POST['billing_vergi_nosu']; | |
if (isset($_POST['checkbox_trigger'])) { | |
if (!empty($vergiNo) && !empty($_POST['billing_company'])) { | |
if (!validateTaxNo($vergiNo)) | |
wc_add_notice(__('Lütfen Doğru Bir Vergi No Girin.'), 'error'); | |
} | |
} | |
} | |
//Adminin Sipariş Detayında Fatura Bilgilerinde TC No ve Vergi Dairesi Görebilmesi İçin | |
add_action('woocommerce_admin_order_data_after_billing_address', 'vergi_no_dairesi', 10, 1); | |
function vergi_no_dairesi($order) | |
{ | |
if (get_post_meta($order->get_id(), '_billing_company', true) == null) { | |
if (get_post_meta($order->get_id(), '_shipping_tc', true) == null) { | |
echo '<p><strong>' . __('TC Kimlik No') . ':</strong> ' . 11111111111 . '</p>'; | |
} else { | |
echo '<p><strong>' . __('TC Kimlik No') . ':</strong> ' . get_post_meta($order->get_id(), '_shipping_tc', true) . '</p>'; | |
} | |
} else { | |
echo '<p><strong>' . __('Vergi Dairesi') . ':</strong> ' . $vergiDaire = get_post_meta($order->get_id(), '_billing_vergi_dairesi', true) . '</p>'; | |
echo '<p><strong>' . __('Vergi No') . ':</strong> ' . $vergiNum = get_post_meta($order->get_id(), '_billing_vergi_nosu', true) . '</p>'; | |
} | |
} | |
//Koşullara göre alanları göstermek için gerekli javascript kodları | |
add_action('woocommerce_after_checkout_form', 'kosullu_alan_goster', 6); | |
function kosullu_alan_goster() | |
{ | |
?> | |
<script type="text/javascript"> | |
jQuery('#billing_vergi_dairesi').hide(); | |
jQuery('#billing_vergi_nosu').hide(); | |
jQuery('#billing_company').hide(); | |
jQuery('#billing_vergi_dairesi_field > label').hide(); | |
jQuery('#billing_vergi_nosu_field > label').hide(); | |
jQuery('#billing_company_field > label').hide(); | |
jQuery("#billing_company_field > label > span").remove(); | |
jQuery("#billing_company_field > label").append("<abbr class='required' title='gerekli'>*</abbr>"); | |
jQuery("#billing_vergi_dairesi_field > label").append("<abbr class='required' title='gerekli'>*</abbr>"); | |
jQuery("#billing_vergi_nosu_field > label").append("<abbr class='required' title='gerekli'>*</abbr>"); | |
jQuery('input#checkbox_trigger').change(function() { | |
if (this.checked) { | |
jQuery('#shipping_tc_field > label').hide(); | |
jQuery('#shipping_tc').hide(); | |
jQuery('#billing_vergi_dairesi').fadeIn(); | |
jQuery('#billing_company').fadeIn(); | |
jQuery('#billing_vergi_nosu').fadeIn(); | |
jQuery('#billing_vergi_dairesi_field > label').fadeIn(); | |
jQuery('#billing_vergi_nosu_field > label').fadeIn(); | |
jQuery('#billing_vergi_dairesi_field > label > span').remove(); | |
jQuery('#billing_vergi_nosu_field > label > span').remove(); | |
jQuery('#billing_vergi_dairesi_field').addClass('validate-required'); | |
jQuery('#billing_company_field > label').fadeIn(); | |
jQuery('#billing_company_field').addClass('validate-required'); | |
jQuery('#billing_vergi_nosu_field').addClass('validate-required'); | |
} else { | |
jQuery('#billing_vergi_dairesi').fadeOut(); | |
jQuery('#billing_vergi_dairesi_field > label').fadeOut(); | |
jQuery('#billing_vergi_dairesi').fadeOut(); | |
jQuery('#billing_vergi_nosu').fadeOut(); | |
jQuery('#billing_vergi_dairesi_field > label').fadeOut(); | |
jQuery('#billing_vergi_nosu_field > label').fadeOut(); | |
jQuery('#shipping_tc_field > label').fadeIn(); | |
jQuery('#shipping_tc').fadeIn(); | |
jQuery('#billing_company').fadeOut(); | |
jQuery('#billing_company_field > label').fadeOut(); | |
} | |
}); | |
</script> | |
<?php | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment