Skip to content

Instantly share code, notes, and snippets.

View itzmekhokan's full-sized avatar
👨‍💻
Eat Sleep <Code> Repeat

Khokan Sardar itzmekhokan

👨‍💻
Eat Sleep <Code> Repeat
View GitHub Profile
@itzmekhokan
itzmekhokan / country-prefix-billing-phone.php
Created September 3, 2019 18:25
Add country calling code prefix in woocommerce billing phone on change country in checkout
<?php
add_action( 'wp_footer', 'scripts_for_adding_country_prefix_on_billing_phone' );
function scripts_for_adding_country_prefix_on_billing_phone(){
?>
<script type="text/javascript">
( function( $ ) {
$( document.body ).on( 'updated_checkout', function(data) {
var ajax_url = "<?php echo admin_url('admin-ajax.php'); ?>",
country_code = $('#billing_country').val();
var ajax_data = {
@itzmekhokan
itzmekhokan / disable-auto-login.php
Created August 31, 2019 15:41
Disable auto login from woocommerce my account registration
<?php
add_filter( 'woocommerce_registration_auth_new_customer', '__return_false' );
@itzmekhokan
itzmekhokan / recently-viewed-products.php
Last active August 27, 2019 08:05
Show recently viewed products using shortcode in woocommerce
<?php
function recently_viewed_products_shortcode( $atts ) {
$viewed_products = ! empty( $_COOKIE['woocommerce_recently_viewed'] ) ? (array) explode( '|', wp_unslash( $_COOKIE['woocommerce_recently_viewed'] ) ) : array(); // @codingStandardsIgnoreLine
$viewed_products = array_reverse( array_filter( array_map( 'absint', $viewed_products ) ) );
if ( empty( $viewed_products ) ) return;
$product_ids = implode( ',', $viewed_products );
// if have some attributes
$attr = '';
if( $atts ){
foreach ( $atts as $key => $value ) {
@itzmekhokan
itzmekhokan / noOfPeopleAddedThisInCart.php
Created August 6, 2019 01:38
Show how many people added product in their current cart.
<?php
function show_no_of_people_added_in_cart(){
global $wpdb, $product;
$in_basket = 0;
$wc_session_data = $wpdb->get_results( "SELECT session_key FROM {$wpdb->prefix}woocommerce_sessions" );
$wc_session_keys = wp_list_pluck( $wc_session_data, 'session_key' );
if( $wc_session_keys ) {
foreach ( $wc_session_keys as $key => $_customer_id ) {
// if you want to skip current viewer cart item in counts or else can remove belows checking
if( WC()->session->get_customer_id() == $_customer_id ) continue;
@itzmekhokan
itzmekhokan / functions.php
Created July 11, 2019 17:06
Woocommerce email order items filter by categories
<?php
function modify_wc_email_order_items_args( $args ) {
if( isset( $args['order'] ) && $args['order'] ){
$items = ( isset( $args['items'] ) && $args['items'] ) ? $args['items'] : array();
if( $items ) :
$cat_wisw_pros = array();
foreach ( $items as $item_id => $item ){
$product_id = $item['product_id'];
$cat_ids = wp_get_post_terms( $product_id, 'product_cat', array( 'fields' => 'ids' ) );
foreach ( $cat_ids as $id ) {
@itzmekhokan
itzmekhokan / functions.php
Created July 10, 2019 19:08
Disable site health test from wordpress
<?php
function remove_site_health_test( $tests ) {
if( isset( $tests['direct']['php_version'] ) unset( $tests['direct']['php_version'] ); // remove php version check
if( isset( $tests['async']['background_updates'] ) unset( $tests['async']['background_updates'] ); // remove background update
return $tests;
}
add_filter( 'site_status_tests', 'remove_site_health_test' );
@itzmekhokan
itzmekhokan / functions.php
Created July 7, 2019 06:03
Add quantity inputs in WooCommerce loops
<?php
function add_quantity_in_woocommerce_loop( $html, $product ) {
if ( $product && $product->is_type( 'simple' ) && $product->is_purchasable() && $product->is_in_stock() && ! $product->is_sold_individually() ) {
$html = '<form action="' . esc_url( $product->add_to_cart_url() ) . '" class="cart" method="post" enctype="multipart/form-data">';
$html .= woocommerce_quantity_input( array(), $product, false );
$html .= '<button type="submit" class="button alt">' . esc_html( $product->add_to_cart_text() ) . '</button>';
$html .= '</form>';
}
return $html;
}
@itzmekhokan
itzmekhokan / restrict-wp-image-size.php
Last active June 30, 2019 14:58
Restrict wp media modal image sizes upload
<?php
function wp_handle_upload_prefilter( $file ){
$tempImg = getimagesize( $file['tmp_name'] );
$width= $img[0];
$height =$img[1];
// lets restrict image size (width*height) 400*640 minimum
if ( $minimum['width'] < 400 )
return array( "error" => "Image dimensions are too small. Minimum width is 400px. Uploaded image width is 400 px" );
elseif ( $minimum['height'] < 640 )
return array( "error" => "Image dimensions are too small. Minimum height is 640px. Uploaded image height is 640 px" );
@itzmekhokan
itzmekhokan / mime-extension-exception.php
Created March 28, 2019 17:47
to handle file types not permitted for security reasons
<?php
function add_allow_upload_extension_exception( $types, $file, $filename, $mimes ) {
// Do basic extension validation and MIME mapping
$wp_filetype = wp_check_filetype( $filename, $mimes );
$ext = $wp_filetype['ext'];
$type = $wp_filetype['type'];
if( in_array( $ext, array( 'ai', 'eps' ) ) ) { // if follows illustrator files have
$types['ext'] = $ext;
$types['type'] = $type;
}
@itzmekhokan
itzmekhokan / add-mime-types.php
Created March 28, 2019 17:29
Add additional file types support in wordpress mainly adobe illustrator files
<?php
function add_custom_upload_mimes( $mimes_types ) {
$mimes_types['eps'] = 'application/postscript'; // Adobe Illustrator files
$mimes_types['ai'] = 'application/postscript'; // Adobe Illustrator files
return $mimes_types;
}
add_filter( 'upload_mimes', 'add_custom_upload_mimes' );