Skip to content

Instantly share code, notes, and snippets.

View tanmay27vats's full-sized avatar

Tanmay Vats tanmay27vats

View GitHub Profile
@tanmay27vats
tanmay27vats / function.php
Last active April 18, 2024 08:30
Remove "Product Type/Product Data" Dropdown Options - WooCommerce
add_filter( 'product_type_selector', 'remove_product_types' );
function remove_product_types( $types ){
unset( $types['grouped'] );
unset( $types['external'] );
unset( $types['variable'] );
return $types;
}
@tanmay27vats
tanmay27vats / script.js
Last active January 5, 2024 22:58
Custom Add to cart WooCommerce, Refresh fragments data, Refresh cart total items, Get product variation
product_add_to_cart : function(){
var ajx_data = {};
var ajx_grab = {};
$(document).on('change','.single-product .variations_form select',function(e){
var $this = $(this);
var pro_id = $(".single-product .variations_form").attr('data-product_id');
var attribute_name = $this.attr('data-attribute_name');
var attribute_value = $this.val();
var post_ajxurl = window.location+"?wc-ajax=get_variation";
@tanmay27vats
tanmay27vats / function.php
Created July 18, 2017 06:01
Get product's default variation ID, variation price or variation object without plugin.
function tv_find_matching_product_variation( $product, $attributes )
{
foreach( $attributes as $key => $value )
{
if( strpos( $key, 'attribute_' ) === 0 )
{
continue;
}
unset( $attributes[ $key ] );
@tanmay27vats
tanmay27vats / diagonal-difference.php
Created August 9, 2018 13:29
Diagonal difference of 2D array - PHP. Given a square matrix, calculate the absolute difference between the sums of its diagonals... Note: |x| is the absolute value of x
// Complete the diagonalDifference function below.
function diagonalDifference($arr) {
$d_ar = [];
$main_len = count($arr);
foreach($arr as $key => $ar) {
$sub_len = count($ar);
if($main_len != $sub_len) {
throw new Exception('Array index count mismatched. Hence this is not square array.');
}
@tanmay27vats
tanmay27vats / .bashrc
Created March 12, 2022 11:54
Add git branch name to shell prompt in Ubuntu with custom text. Sudo nano ~/.bashrc
# Colors definition
BLUE='\e[94m'
L_BLUE='\e[96m'
YELLOW='\e[93m'
GREEN='\e[32m'
L_GREEN='\e[92m'
RED='\e[31m'
L_RED='\e[91m'
NC='\e[0m'
@tanmay27vats
tanmay27vats / update.sql
Created April 24, 2017 08:40
Renaming Custom Post Types and Taxonomies Through MySQL Query - Wordpress
SQL query for renaming the posts:
UPDATE `wp_posts` SET `post_type` = '<new post type name>' WHERE `post_type` = '<old post type name>';
SQL query for renaming taxonomy:
UPDATE `wp_term_taxonomy` SET `taxonomy` = '<new taxonomy name>' WHERE `taxonomy` = '<old taxonomy name>';
# That should take care of all of the database areas. Just remember to match the new names in the code where the post types or taxonomies are registered. As far as I know, this is not handled in any plugins yet.
@tanmay27vats
tanmay27vats / open_projects_on_terminal.sh
Created February 25, 2022 13:02
Open projects on the terminal in multiple tabs at once.
#!/bin/bash
####################################
#
# Open all required porjects.
#
####################################
gnome-terminal --tab --command="bash -c 'cd /path/to/your/project/angular-project-1; npm start; $SHELL'" \
--tab --command="bash -c 'cd /path/to/your/project/angular-project-2; ng serve -p 4201; $SHELL'" \
--tab --command="bash -c 'cd /path/to/your/project/angular-project-3; ng serve -p 4202; $SHELL'"
@tanmay27vats
tanmay27vats / solution.py
Created February 1, 2022 08:13
[Python] Valid Parentheses - Given a string s containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid. An input string is valid if: Open brackets must be closed by the same type of brackets. Open brackets must be closed in the correct order. Example 1: Input: s = "()" Output: true Example 2: Input: …
class Solution:
def isValid(self, s: str) -> bool:
s = s.replace(' ','')
if len(s)%2 != 0:
return False
dict = {'(' : ')', '[' : ']', '{' : '}'}
ar = []
for i in s:
if i in dict:
ar.append(i)
@tanmay27vats
tanmay27vats / solution.py
Last active February 1, 2022 07:24
[Python] Roman to Integer - Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M.
class Solution:
def romanToInt(self, s: str) -> int:
roman_dict = { 'I':1, 'V':5, 'X':10, 'L':50, 'C':100, 'D':500, 'M':1000, 'IV':4, 'IX':9, 'XL':40, 'XC':90, 'CD':400, 'CM':900 }
i = 0
sum = 0
while i < len(s):
if s[i:i+2] in roman_dict and i+1<len(s):
sum+=roman_dict[s[i:i+2]]
i+=2
else:
@tanmay27vats
tanmay27vats / solution.py
Created February 1, 2022 07:10
[Python] Longest Common Prefix - Write a function to find the longest common prefix string amongst an array of strings. If there is no common prefix, return an empty string "". Example 1: Input: strs = ["flower","flow","flight"] Output: "fl" Example 2: Input: strs = ["dog","racecar","car"] Output: "" Explanation: There is no common prefix among …
class Solution:
def longestCommonPrefix(self, strs: List[str]) -> str:
if len(strs) == 0:
return ""
common_prefix = strs[0]
for i in range(1,len(strs)):
temp = ""
if len(common_prefix) == 0:
break
for j in range(len(strs[i])):