WC remove coupon already apply error

In the wocommerece checkout page if you don't want to show the message or warning 'Coupon code already applied!' or want to change this message you can apply wocommerece filter called 'woocommerce_add_error' and a custom function to remove or change the this default text message. As like other filter code this should also placed in the functions.php of your active theme. you can place this code anywhere in side the file best to put this code end of the file and use some comments that this is custom added code to prevent future inconvenience. Check this code below you can simply copy in your functions file and change change according to your needs.



<?php
// APPLY WC ADD ERROR FILTER
add_filter( 'woocommerce_add_error', 'my_woocommerce_add_error' );

/* HIDE MESSAGE 'Coupon code already applied!' - DALJEET */
function my_woocommerce_add_error( $error ) {
    if( 'Coupon code already applied!' == $error ) {
        // HERE CAN ADD YOUR CUSTOM MESSAGE AS WELL  
        $error = '';
    }
    return $error;
}
?>

WC variations remove select option of selectboxes

Removing the varioations first option '- select -' from all the selectbox in wordpress wocommerece there is filter called 'woocommerce_dropdown_variation_attribute_options_args' applying this filter we can change or we can remove the first default option in the all variations fields. Apply this filter all the script need in the function mentioned below. Use this function and filter to in the functions.php of your child-theme of regular theme to get the results.


<?php
// REMOVE VARIATION OPTION '- SELECT -' FROM ALL SELECT BOX
add_filter( 'woocommerce_dropdown_variation_attribute_options_args', 'variations_remove_select_text');

// CUSTOM FUNCTION TO REMOVE SELECT OPTION OR VARIATION FILEDS
function variations_remove_select_text( $args ){
    $args['show_option_none'] = '';
    return $args;

}
?>

Wocommerece change cart coupon label

Here some code to change the wordpress wocommerece cart coupon label. Sometime need to show the custom label for the coupon label in the checkout page. Here is the wocommerece filter to change the checkout page cart coupon label. Al like other filters you can apply this filter code in the functions file of the child-theme or in the normal theme. Instead of the 'coupon' label we can use the 'discount' or 'save' or 'off' etc. labels. Check the code below:-

<?php
// FILTER CART TOTAL COUPON LABEL
add_filter('woocommerce_cart_totals_coupon_label', wc_cart_point_rewards_change_coupon_label',11,2 );

// CUSTOM FUNCTION TO CHANGE CART TOTAL COUPON LABEL
function wc_cart_point_rewards_change_coupon_label( $html , $coupon)
{
    $html='Discount 10%:';
    return $html;
}

?>

Wocommerece removing all the shipping and billing details fields

Somtime we don't need the wordpress wocommerece shipping and billing details fields on the checkout page so we can easily remove all the fields or the fields that we really don't need or required. So There is filter called the woocommerce_checkout_fields for the woocommerece. We can call a custom function that have the code to remove or unset the billing and shipping fields for the checkout page. Same as other wordpress filters we can apply this filter in the functions.php file of the theme folder. Here below is the code in which i totally removed all the billing and shipping fields.

<?php
// FILTER FOR THE CHECKOUT FIELDS
add_filter( 'woocommerce_checkout_fields' , 'custom_override_checkout_fields' );

// CUSTOM FUNCTION TO REMOVE ALL THE BILLING AND SHIPPING FIELDS OF THE CHECKOUT PAGE
function custom_override_checkout_fields( $fields ) 
{
    // ALL BILLING FIELDS
    unset($fields['billing']['billing_first_name']);
    unset($fields['billing']['billing_last_name']);
    unset($fields['billing']['billing_company']);
    unset($fields['billing']['billing_address_1']);
    unset($fields['billing']['billing_address_2']);
    unset($fields['billing']['billing_city']);
    unset($fields['billing']['billing_postcode']);
    unset($fields['billing']['billing_country']);
    unset($fields['billing']['billing_state']);
    unset($fields['billing']['billing_phone']);
    unset($fields['order']['order_comments']);
    unset($fields['billing']['billing_address_2']);
    unset($fields['billing']['billing_postcode']);
    unset($fields['billing']['billing_company']);
    unset($fields['billing']['billing_last_name']);
    unset($fields['billing']['billing_email']);
    unset($fields['billing']['billing_city']);
    
    // ALL SHIPPING FIELDS
    unset($fields['shipping']['shipping_first_name']);
    unset($fields['shipping']['shipping_last_name']);
    unset($fields['shipping']['shipping_company']);
    unset($fields['shipping']['shipping_address_1']);
    unset($fields['shipping']['shipping_address_2']);
    unset($fields['shipping']['shipping_city']);
    unset($fields['shipping']['shipping_postcode']);
    unset($fields['shipping']['shipping_country']);
    unset($fields['shipping']['shipping_state']);
    unset($fields['shipping']['shipping_phone']);
    unset($fields['order']['order_comments']);
    unset($fields['shipping']['shipping_address_2']);
    unset($fields['shipping']['shipping_postcode']);
    unset($fields['shipping']['shipping_company']);
    unset($fields['shipping']['shipping_last_name']);
    unset($fields['shipping']['shipping_email']);
    unset($fields['shipping']['shipping_city']);
    return $fields;

}

?>

Wodpress custom ajax script

Here is a example of the custom ajax code in the wodpress CMS. We can use this without creating our custom new files we just add your jquery script and php script in the existing wordpress theme. This example is good use of the standard wordpress ajax feature. In this example we have a form that have some fields with the values and also have tow different buttons to do different actions. In this example we get the form data via ajax and then send the two different mails to the admin according to the button press. On button use to send the mails for change requests and second button use to send the mail request to cancel the subscription. You can put your jquery code in common files like header of footer of the theme just make sure the ID or identifier should be very unique for the buttons not to match with any other elements in the website. Or you can put this jquery code in your template if which this form exist. In these given scripts there are some commented code can be use for different purposes not removed can be more helpful some other actions.

HERE IS THE JQUERY CODE TO USE IN TEMPLATE FILE OR HEADER OR FOOTER FILE

<script type="text/javascript">
// AJAX FOR THE CHANGE PACKAGE PAGE
jQuery( document ).on( 'click', '#change_request_pack, #cancel_request_pack', function() {
alert('You order is currently under review. We will email when we have made requested changes!');
jQuery('#change_request_message').css('display','block');
    // var lib_data_id = $(this).prev(':input').val(); 
    // var data = jQuery('.variations_form').serialize() + "&action=change_request_pack";
    var data = "action=change_request_pack";
    data += "&type="+jQuery(this).val();
    data += "&adults="+jQuery('#adults').val();
    data += "&kids="+jQuery('#kids').val();
    data += "&design-package-1="+jQuery('#design-package-1').val();
    data += "&design-package-2="+jQuery('#design-package-2').val();
    data += "&design-package-3="+jQuery('#design-package-3').val();
    data += "&design-package-4="+jQuery('#design-package-4').val();
    data += "&your-price="+jQuery('#your-price').val();
    data += "&subscription-id="+jQuery('#subscription-id').val();
    jQuery.ajax({
    url : '<?php echo admin_url( 'admin-ajax.php' );?>',
    type : 'get',
    data : data,
    success : function( response ) { 
    // response = jQuery.parseJSON(response);   
    // alert(response);
   
    jQuery('#main').animate({
        scrollTop: jQuery("#change_request_message").offset().top
    }, 1000);

        /*response = $.parseJSON(response);       
        if(response.status=='added'){       
            $("#addBtn"+response.lib_data_id).val('Remove from request folder');    
            $("#addBtn"+response.lib_data_id).addClass('remove');
        }else{
            $("#addBtn"+response.lib_data_id).val('Add to request folder'); 
            $("#addBtn"+response.lib_data_id).removeClass('remove');
        }
        */
    }
    });
});
</script>

HERE BELOW THE PHP SCRIPT USE IN THE FUNCTIONS FILE OF THE THEME

<?php
add_action( 'wp_ajax_nopriv_change_request_pack', 'change_request_pack' );
add_action( 'wp_ajax_change_request_pack', 'change_request_pack' );
function change_request_pack() {      
    global $wpdb;
    $return['Type'] = $_GET['type'];
    $return['Adults'] = $_GET['adults'];
    $return['Kids'] = $_GET['kids'];
    $return['Subscription ID'] = $_GET['subscription-id'];
    $return['Design Package 1'] = $_GET['design-package-1'];
    $return['Design Package 2'] = $_GET['design-package-2'];
    $return['Design Package 3'] = $_GET['design-package-3'];
    $return['Design Package 4'] = $_GET['design-package-4'];
    $return['Package'] = $_GET['your-price'];

    $to = get_option('admin_email');

    if($return['Type']=="Submit Changes")
    {
$subject = "Package Change Request for Subscription ID #".$return['Subscription ID'];
$message = "<p>Package Change Request for Subscription ID #".$return['Subscription ID']."</p>";
$message .= "<b>Adults: </b> ".$return['Adults'];
$message .= "<br/><b>Kids: </b> ".$return['Adults'];
$message .= "<br/><b>Subscription ID: </b> ".$return['Subscription ID'];
$message .= "<br/><b>Design Package 1: </b> ".$return['Design Package 1'];
$message .= "<br/><b>Design Package 1: </b> ".$return['Design Package 2'];
$message .= "<br/><b>Design Package 1: </b> ".$return['Design Package 3'];
$message .= "<br/><b>Design Package 1: </b> ".$return['Design Package 4'];
$message .= "<br/><b>Package: </b> ".$return['Package'];
}
else if($return['Type']=="Cancel Subscription")
{
$subject = "Cancelation request for Subscription ID #".$return['Subscription ID'];
$message = "<b>Cancelation request for Subscription ID #".$return['Subscription ID']."</b>";
}

$headers[] = "From: Admin <admin@toothbrush-39739797453324098.com>";
$headers[] = "Content-type: text/html";

wp_mail( $to, $subject, $message, $headers );


    echo json_encode($return);
    exit();
    die;

}
?>

Woocommerce only one product in cart

Only one product purchase at a time by use in wordpress wocommerece can be possible adding the filter in the theme functions.php file this is a cutom function so you can add in your child theme functions.php file anywhere if you have not the child theme you can add this code the the functions.php file of the regular theme. The logic to do this we just use a custom function in which we empty cart and then we add our product in the cart that i want to purchase and ad we call this function on the add to cart filter. Here is the code you can use by simply copying in your theme functions file.


/// ONLY ONE PRODUCT IN CAR AT A TIME
add_filter( 'woocommerce_add_cart_item_data', 'woo_custom_add_to_cart' );


function woo_custom_add_to_cart( $cart_item_data ) {
    
    global $woocommerce;
    // Empty cart remove all the items already in the cart.

    $woocommerce->cart->empty_cart();

    // Do nothing with the data and return
    return $cart_item_data;
}

PHP get difference of two given dates

To get the difference of the two given dates there is a custom made function in the php script share here you can pass two different dates and will get the difference of the date in the positive or negative numbers. It will help to determine that difference of the past dates or futures dates. Just copy this function and call this function passing the two dates as the parameters of this function.

<?php
// FUNCTION RETURN DAYS REMAINS OR DIFFERENCE OF TWO DATES
function days_remain($d1,$d2)
{
$date1=date_create(date("Y-m-d",strtotime($d1)));
$date2=date_create(date("Y-m-d",strtotime($d2)));
$diff=date_diff($date1,$date2);
// $diff->format("%R%a days");
// print_r($diff);
// return $diff->days;
return $diff->format("%R%a");
}

// CALL THE FUNCTION
$date_1 = "2016-10-10";
$date_2 = "2016-10-12";
echo days_remain($date_1,$date_2);
// OUTPUT "+2"


// CALL THE FUNCTION
$date_1 = "2015-11-09";
$date_2 = "2016-12-08";
echo days_remain($date_1,$date_2); 


// CALL THE FUNCTION
$date_1 = "2021-03-07";
$date_2 = "2050-01-11";
echo days_remain($date_1,$date_2); 


// CALL THE FUNCTION
$date_1 = "1988-08-30";
$date_2 = "1999-11-19";
echo days_remain($date_1,$date_2); 


// CALL THE FUNCTION
$date_1 = "2005-09-25";
$date_2 = "2011-04-16";
echo days_remain($date_1,$date_2); 


// CALL THE FUNCTION
$date_1 = "1995-02-16";
$date_2 = "2009-09-22";
echo days_remain($date_1,$date_2); 

// CALL THE FUNCTION
$date_1 = "2001-04-21";
$date_2 = "2016-12-19";
echo days_remain($date_1,$date_2); 

// CALL THE FUNCTION
$date_1 = "2000-03-23";
$date_2 = "2025-08-29";
echo days_remain($date_1,$date_2); 
?>

Students Tech Life