Untitled

mail@pastecode.io avatar
unknown
plain_text
a month ago
2.8 kB
1
Indexable
Never
add_action('marketking_dashboard_head', function(){

	$user_id = get_current_user_id();
	$is_uk = false;
	$has_vat = false;

	// get whether current vendor is UK or not
	$vendor_country = get_user_meta($user_id, 'billing_country', true);
	if ($vendor_country == 'GB'){
		$is_uk = true;
	}

	// get whether vendor has VAT number
	$vat_number = get_user_meta($user_id, 'marketking_field_5300', true);
	if (!empty($vat_number)){
		$has_vat = true;
	}

	// UK VAT: force taxable + VAT rates
	if ($is_uk && $has_vat){
		// default taxable
		?>
		<script>
		    jQuery(document).ready(function($){
				jQuery('#_tax_status').val('taxable').trigger('change');
				setTimeout(function(){
					jQuery('#_tax_status').val('taxable').trigger('change');
				}, 350);
			}
		</script>
		<?php
	}

	// UK No VAT: force taxable + non-VAT rates
	if ($is_uk && $has_vat){
		// default taxable
		?>
		<script>
		    jQuery(document).ready(function($){
				jQuery('#_tax_status').val('taxable').trigger('change');
				setTimeout(function(){
					jQuery('#_tax_status').val('taxable').trigger('change');
				}, 350);
			}
		</script>
		<?php
	}

    ?>
    <script>
        jQuery(document).ready(function($){
            var originalSaveButton = $('#marketking_save_product_button');
            var taxStatusField = $('select[name="_tax_status"]');
            var taxClassField = $('select[name="_tax_class"]');
            // Clone the original button without copying event listeners and insert it after the original
            var clonedSaveButton = originalSaveButton.clone(false).removeAttr('id');
            originalSaveButton.after(clonedSaveButton);
            // Hide the original button
            originalSaveButton.hide();
            // Add click event to the cloned button
            clonedSaveButton.on('click', function(event){
                var taxStatusValue = taxStatusField.val();
                var taxClassValue = taxClassField.val();
                console.log('Tax Status Value:', taxStatusValue);
                console.log('Tax Class Value:', taxClassValue);

                if(taxStatusValue !== 'taxable') {
                    event.preventDefault(); // Prevent default action
                    alert('Tax status must be set to "Taxable" before saving. Ensure you Tax Class is selected correctly (UK VAT Registered / non UK VAT Registered)');

                } else if (taxClassValue.toLowerCase() === 'standard') {
                    event.preventDefault(); // Prevent default action
                    alert('Tax class cannot be "Standard" when tax status is "Taxable".');
                } else {
                    // Trigger click on the original button
                    originalSaveButton.trigger('click');
                }
            });
        });
    </script>
    <?php
});
Leave a Comment