30 January 2020

How to Make Billing and Shipping Fields Optional and required in WooCommerce

Make Billing and Shipping Fields Optional and required in WooCommerce




For not required

  1. country
  2. first_name
  3. last_name
  4. company
  5. address_1
  6. address_2
  7. city
  8. state
  9. postcode


add_filter( 'woocommerce_default_address_fields' , 'optional_default_address_fields' );
 function optional_default_address_fields( $address_fields ) {
 $address_fields['company']['required'] = false;
 $address_fields['postcode']['required'] = false;
 $address_fields['city']['required'] = false;
 $address_fields['state']['required'] = false;
 return $address_fields;
 }




For required



add_filter( 'woocommerce_default_address_fields' , 'optional_default_address_fields' );
 function optional_default_address_fields( $address_fields ) {
 $address_fields['company']['required'] = true;
 $address_fields['postcode']['required'] = true;
 $address_fields['city']['required'] = true;
 $address_fields['state']['required'] = true;
 return $address_fields;
 }

27 January 2020

How to submit form with anchor tag (link) in jquery?

 Submit form with anchor tag (a and link) in jquery


<form method="get" action="">
<input type="hidden" name="ID" value="<?php echo $dealid;?>">
<input type="hidden" name="con" value="construction-contract">
<a href="" onclick="this.closest('form').submit();return false;" value="CONSTRUCTION CONTRACT" class="contract-button">CONSTRUCTION CONTRACT</a>
</form>

25 January 2020

How to sorting in descending and ascending in sql?

Sorting in descending order in SQL




Descending



"select * from $dealtable where user_id='$uid' AND status='$status' ORDER BY ID DESC"


Ascending




"select * from $dealtable where user_id='$uid' AND status='$status' ORDER BY ID ASC"

21 January 2020

How to Prevent negative inputs in form input type=“number”?

Prevent negative inputs in form input type=“number”?




<html>
<body>
<form action="#">
  <input type="number" name="test" min=0 oninput="validity.valid||(value='');"><br>
  <input type="submit" value="Submit">
</form>
</body>
</html>

20 January 2020

How to trigger (use) an event when contenteditable is changed in jQuery?

Trigger an event when contenteditable is changed




<div class="changeable" contenteditable="true"> Click this div to edit it </div>


jQuery(document).ready(function($){

var contents = $('.changeable').html();
$('.changeable').blur(function() {
    if (contents!=$(this).html()){
        alert('Handler for .change() called.');
        contents = $(this).html();
    }
});


});

How to get warning when exit browser without saving?

Get warning when exit browser without saving



My website is www.wpcodekit.com



<script>
jQuery(document).ready(function($){

$(window).unbind('beforeunload');

$("input[type='submit']").click(function(){
     $(window).unbind('beforeunload');
});


$("input").on("change paste keyup", function() {


  $(window).bind('beforeunload', function(){
return 'Are you sure you want to leave?';
  });

});


var contenteditable = document.querySelector('[contenteditable]'),
    text = contenteditable.textContent;



$(contenteditable).each(function(){

$(this).on("change paste keyup", function() {

$(window).bind('beforeunload', function(){
return 'Are you sure you want to leave?';
});

});
});


$("input[type='submit']").click(function(){
     $(window).unbind('beforeunload');
});


});
</script>

How to get default value from input field in jquery?

Get default value from input field in jquery



$("#name").on("change paste keyup", function() {

 var default_value = this.defaultValue;

});

18 January 2020

How to send custom email template on buy specific product in woocommerce?

Send custom email template on buy specific product in woocommerce



add_action( 'woocommerce_thankyou', 'wpcodekit_check_order_product_id' );

function wpcodekit_check_order_product_id( $order_id ){

$order = wc_get_order( $order_id );
$items = $order->get_items();
foreach ( $items as $item_id => $item ) {
   $product_id = $item->get_variation_id() ? $item->get_variation_id() : $item->get_product_id();
   if($product_id === 6112) {
 
$customername = $order->get_billing_first_name() .' '.$order->get_billing_last_name();
$message = file_get_contents(WP_CONTENT_DIR . '/themes/twentynineteen-child/emails/premiumsupport.html');
$message = str_replace("{customeremail}",$customername,$message);
$toadmin = $order->get_billing_email();
$quotename = "Let Us Structure Your Partnership";
$headers = 'From: "' . $quotename . '" <no-reply@brrrrinvest.com>' . "\r\n";
$headers .= "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type: text/html; charset=UTF-8" . "\r\n";

wp_mail( $toadmin, $quotename, $message, $headers );
}

if(($product_id === 2750)||($product_id === 2749)) {
 
$customername = $order->get_billing_first_name() .' '.$order->get_billing_last_name();
$message = file_get_contents(WP_CONTENT_DIR . '/themes/twentynineteen-child/emails/emailconsult.html');
$message = str_replace("{customeremail}",$customername,$message);
$toadmin = $order->get_billing_email();
$quotename = "Let Us Structure Your Partnership";
$headers = 'From: "' . $quotename . '" <no-reply@brrrrinvest.com>' . "\r\n";
$headers .= "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type: text/html; charset=UTF-8" . "\r\n";

wp_mail( $toadmin, $quotename, $message, $headers );
}
}
}

How to send custom email template in wordpress?

 Send custom email template in wordpress



$body = file_get_contents(WP_CONTENT_DIR . '/themes/Avada-Child-Theme/contact-email.html');
$body = str_replace("#name#",$_POST['quotename'],$body);
$body = str_replace("#email#",'<a style="color:#fff" href="mailto:'.$_POST['quoteemail'].'">'.$_POST['quoteemail'].'</a>',$body);
$body = str_replace("#mobile#",$_POST['quotemobile'],$body);
$body = str_replace("#message#",$_POST['quotemessage'],$body);


$toadmin = "Scott@USAPrivateMoney.com, Jack@USAPrivateMoney.com, Prospect.60008.140@realtyjuggler.com";

$adminmessage = "You just got one more visitor that is ".$quoteemail." who filled proof of funds letter. Please find the attachment";


$headers = 'From: "' . $quotename . '" <no-reply@usaprivatemoney.com>' . "\r\n";
$headers .= "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type: text/html; charset=UTF-8" . "\r\n";


mail( $toadmin, $quotename, $body, $headers );



Example


  if($product_id === 6112) {
 
$customername = $order->get_billing_first_name() .' '.$order->get_billing_last_name();
$message = file_get_contents(WP_CONTENT_DIR . '/themes/twentynineteen-child/emails/premiumsupport.html');
$message = str_replace("{customeremail}",$customername,$message);
$toadmin = $order->get_billing_email();
$quotename = "Let Us Structure Your Partnership";
$headers = 'From: "' . $quotename . '" <no-reply@brrrrinvest.com>' . "\r\n";
$headers .= "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type: text/html; charset=UTF-8" . "\r\n";

wp_mail( $toadmin, $quotename, $message, $headers );
}

How to check if Order Contains Product ID in woocommerce (if product is buy)?

Check if Order Contains Product ID in woocommerce



add_action( 'woocommerce_thankyou', 'codekit_check_order_product_id' );
 
function codekit_check_order_product_id( $order_id ){
$order = wc_get_order( $order_id );
$items = $order->get_items();
foreach ( $items as $item_id => $item ) {
   $product_id = $item->get_variation_id() ? $item->get_variation_id() : $item->get_product_id();
   if ( $product_id === ABC ) {
       // do something
   }
}
}

How to get order info (billing info) from order in woocommerce?

woocommerce-easily-get-order-info-total-items-etc-from-order


// Get Order ID
$order->get_id();

// Get Order Totals $0.00
$order->get_formatted_order_total();
$order->get_cart_tax();
$order->get_currency();
$order->get_discount_tax();
$order->get_discount_to_display();
$order->get_discount_total();
$order->get_fees();
$order->get_formatted_line_subtotal();
$order->get_shipping_tax();
$order->get_shipping_total();
$order->get_subtotal();
$order->get_subtotal_to_display();
$order->get_tax_location();
$order->get_tax_totals();
$order->get_taxes();
$order->get_total();
$order->get_total_discount();
$order->get_total_tax();
$order->get_total_refunded();
$order->get_total_tax_refunded();
$order->get_total_shipping_refunded();
$order->get_item_count_refunded();
$order->get_total_qty_refunded();
$order->get_qty_refunded_for_item();
$order->get_total_refunded_for_item();
$order->get_tax_refunded_for_item();
$order->get_total_tax_refunded_by_rate_id();
$order->get_remaining_refund_amount();

// Get Order Items
$order->get_items();
$order->get_items_key();
$order->get_items_tax_classes();
$order->get_item();
$order->get_item_count();
$order->get_item_subtotal();
$order->get_item_tax();
$order->get_item_total();
$order->get_downloadable_items();

// Get Order Lines
$order->get_line_subtotal();
$order->get_line_tax();
$order->get_line_total();

// Get Order Shipping
$order->get_shipping_method();
$order->get_shipping_methods();
$order->get_shipping_to_display();

// Get Order Dates
$order->get_date_created();
$order->get_date_modified();
$order->get_date_completed();
$order->get_date_paid();

// Get Order User, Billing & Shipping Addresses
$order->get_customer_id();
$order->get_user_id();
$order->get_user();
$order->get_customer_ip_address();
$order->get_customer_user_agent();
$order->get_created_via();
$order->get_customer_note();
$order->get_address_prop();
$order->get_billing_first_name();
$order->get_billing_last_name();
$order->get_billing_company();
$order->get_billing_address_1();
$order->get_billing_address_2();
$order->get_billing_city();
$order->get_billing_state();
$order->get_billing_postcode();
$order->get_billing_country();
$order->get_billing_email();
$order->get_billing_phone();
$order->get_shipping_first_name();
$order->get_shipping_last_name();
$order->get_shipping_company();
$order->get_shipping_address_1();
$order->get_shipping_address_2();
$order->get_shipping_city();
$order->get_shipping_state();
$order->get_shipping_postcode();
$order->get_shipping_country();
$order->get_address();
$order->get_shipping_address_map_url();
$order->get_formatted_billing_full_name();
$order->get_formatted_shipping_full_name();
$order->get_formatted_billing_address();
$order->get_formatted_shipping_address();

// Get Order Payment Details
$order->get_payment_method();
$order->get_payment_method_title();
$order->get_transaction_id();

// Get Order URLs
$order->get_checkout_payment_url();
$order->get_checkout_order_received_url();
$order->get_cancel_order_url();
$order->get_cancel_order_url_raw();
$order->get_cancel_endpoint();
$order->get_view_order_url();
$order->get_edit_order_url();

// Get Order Status
$order->get_status();

// source: https://docs.woocommerce.com/wc-apidocs/class-WC_Order.html

How to add another email in woocommerce order?



add_filter( 'woocommerce_email_recipient_customer_completed_order', 'your_email_recipient_filter_function', 10, 2);

function your_email_recipient_filter_function($recipient, $object) {
    $recipient = $recipient . ',jaspreet797386@gmail.com';
    return $recipient;

16 January 2020

How to exchange related products with full description in woocommerce?

Exchange related products with full description in woocommerce



add_action( 'woocommerce_after_single_product_summary', 'woocommerce_output_related_products', 2 );
remove_action( 'woocommerce_after_single_product_summary', 'woocommerce_output_related_products', 20 );

How to fix woocommerce-rating-stars-bugs-sssss?




body.woocommerce .star-rating span::before {
  content: "SSSSS" !important;
  color: #e0b22f !important;
}

.woocommerce .star-rating span{
font-family:star !important;
}

.star-rating::before {
    color: #e0b22f;
}

How to Remove WooCommerce Breadcrumbs in WordPress?

Remove WooCommerce Breadcrumbs in WordPress




add_action('template_redirect', 'remove_shop_breadcrumbs' );
function remove_shop_breadcrumbs(){

    if (is_shop())
        remove_action( 'woocommerce_before_main_content', 'woocommerce_breadcrumb', 20, 0);

}

OR


if(is_shop()){ ... } // shop page

if(is_front_page() ){ ... } // static front page

if(is_home()){ ... } // default page




                                                                        OR


remove_action('woocommerce_before_main_content', 'woocommerce_breadcrumb', 20, 0);

How to Hide WooCommerce Product Quantity Field from Product Pages?

Hide WooCommerce Product Quantity Field from Product Pages


www.wpcodekit.com


function quantity_wp_head() {
 
if ( is_product() ) {
    ?>
<style type="text/css">.quantity, .buttons_added { width:0; height:0; display: none; visibility: hidden; }</style>
<?php
    }
}
    add_action( 'wp_head', 'quantity_wp_head' );


Or



add_filter( 'woocommerce_is_sold_individually', 'cw_remove_quantity_fields');
function cw_remove_quantity_fields( $return, $product ) {
switch ( $product->product_type ) :
case "grouped":
return true;
break;
case "external":
return true;
break;
case "variable":
return true;
break;
default:
return true;
break;
endswitch;
}




Or




function cw_remove_quantity_fields( $return, $product ) {
    return true;
}
add_filter( 'woocommerce_is_sold_individually', 'cw_remove_quantity_fields', 10, 2 );




How to Hide WooCommerce Product Quantity Field from Product Pagesd_individually', 'cw_remove_quantity_fields', 10, 2 );

04 January 2020

How to fetch file name, extension and basename from url in php?

Fetch file name, extension and basename from url in php

<?php
$file = 'http://localhost/brrrr/wp-content/uploads/2019/11/New-Business-card-4.jpg';
$ext = pathinfo($file);

echo $ext['dirname'] . '<br/>';   // Returns folder/directory
echo $ext['basename'] . '<br/>';  // Returns file.html
echo $ext['extension'] . '<br/>'; // Returns .html
 echo $ext['filename'] . '<br/>';  // Returns file 
?>

How to accept image extension from url in php?

Accept image extension from url in php



<?php
 $file_name = 'http://localhost/brrrr/wp-content/uploads/2019/11/New-Business-card-4.jpg';
echo $file_name."<br>";
$allowed = array('gif', 'png', 'jpg');
$ext = pathinfo($file_name, PATHINFO_EXTENSION);
if (!in_array($ext, $allowed)) {
    echo 'error';
}
else{
echo "this is ok";
}
 

?>

03 January 2020

How to remove spinners from number field(quantity fields ) in woocommerce?

WooCommerce quantity fields without spinners



/* Disable input[type=number] buttons until the world is ready */
input::-webkit-outer-spin-button, input::-webkit-inner-spin-button {
    display:none;
}


OR




<input type="text" pattern="[0-9]*" ... />

02 January 2020

How to download pdf file on browser instead of saving it on the server in fpdf?

fpdf Output('filename.pdf','F'); downloading file on browser instead of saving it on the server



Output('D','filename.pdf');

How to create elementor popup with code in wordpress?

Create elementor popup with code in wordpress


Use in main-function file

// Add Shortcode
function elementor_poup( $atts ) {

// Attributes
$atts = shortcode_atts(
array(
'id' => '',
),
$atts
);
$etid = $atts['id'];


echo '<div class="elementor-popup-'.$etid.'"><div class="e-popup-content"><div class="eclose"><svg viewBox="0 0 10.6 11"><polygon points="6.3,5.4 10.4,1.4 9.2,0.3 5.3,4.2 1.3,0.3 0.2,1.4 4.3,5.4 0.2,9.5 1.3,10.7 5.3,6.8 9.2,10.7 10.4,9.5 "></polygon></svg></div>';
echo do_shortcode("[elementor-template id=$etid]");
echo '</div></div>';

echo'<style>
.epopup-'.$etid.'
{
cursor:pointer;
}
.elementor-popup-'.$etid.' .eclose
{

    z-index: 99999;
    position: absolute;
    top: 1em;
    right: 1em;
    cursor: pointer;
    line-height: 0;
    border-radius: 50%;
    padding: 10px;
    background: #DDE2EB;
    width: 40px;
    height: 40px;
    fill: #999999;
    -webkit-transition: fill .3s ease-in-out;
    transition: fill .3s ease-in-out;
}
.elementor-popup-'.$etid.'
{
width:100%;
position:fixed;
left:0px;
top:0px;
height:100%;
background:rgba(0, 22, 58, 0.89);
display:none;
align-items:center;
    z-index: 999990000;
}
.elementor-popup-'.$etid.' .e-popup-content section
{
width:100% !important;
}
.e-popup-content
{
width: 100%;
    margin: auto;
position: relative;
}
.eclose svg {
    width: 14px;
    margin: 3px auto;
    display: table;
}

</style>
';
echo '<script>jQuery(document).ready(function($){';
?>

function validateEmail<?php echo $etid; ?>($email) {
  var emailReg = /^([a-zA-Z0-9_.+-])+\@(([a-zA-Z0-9-])+\.)+([a-zA-Z0-9]{2,4})+$/;
  return emailReg.test( $email );
}

$('.epopup-<?php echo $etid; ?>').click(function(){
$('.elementor-popup-<?php echo $etid; ?>').css("display","flex");
});



$('.elementor-popup-<?php echo $etid; ?> .eclose').click(function(){
$('.elementor-popup-<?php echo $etid; ?>').css("display","none");
});

$('.epopup-<?php echo $etid; ?> .infusion-recaptcha').click(function(){
alert("before");
var epopemailemail<?php echo $etid; ?> = $('.epopup-<?php echo $etid; ?> .infusion-field-input').val();

if( !validateEmail<?php echo $etid; ?>(epopemailemail<?php echo $etid; ?>)) {
$('.epopup-<?php echo $etid; ?> .infusion-field-input').css("border","1px solid red");
return false;
}
});

<?php
echo '});</script>';

}
add_shortcode( 'elementor-popup', 'elementor_poup' );


Copy only elementor section id 
Design a section


[elementor-template id="6165"]

                    Paste elementor section id in shortcode  


[elementor-popup id="6165"]


          Add class which give you below this line(in elementor)

epopup-6165

How to add re-captcha v3 on all Elementor forms using coding?

 Add re-captcha v3 on all Elementor forms using coding add_action('wp_footer',function(){     ?> <script src="https://www...