Skip to content

Instantly share code, notes, and snippets.

View moskalukigor's full-sized avatar

Ihor moskalukigor

View GitHub Profile
@moskalukigor
moskalukigor / functions.php
Created April 25, 2018 09:00
Wordpress Search Order By Post Type
<?php
add_filter( 'posts_orderby', 'order_search_by_posttype', 10, 1 );
function order_search_by_posttype( $orderby ){
if( ! is_admin() && is_search() ) :
global $wpdb;
$orderby =
"
CASE WHEN {$wpdb->prefix}posts.post_type = 'product' THEN '1'
WHEN {$wpdb->prefix}posts.post_type = 'post' THEN '2'
WHEN {$wpdb->prefix}posts.post_type = 'page' THEN '3'
@moskalukigor
moskalukigor / functions.php
Created April 20, 2018 13:00
Ajax update cart total ( mini cart )
<?php
add_filter( 'woocommerce_add_to_cart_fragments', function($fragments) {
ob_start(); ?>
<a href="<?php echo wc_get_cart_url(); ?>" class="fa fa-shopping-cart" title="Shopping Cart"><?php echo WC()->cart->get_cart_contents_count(); ?></a>
<?php $fragments['a.fa-shopping-cart'] = ob_get_clean();
return $fragments;
@moskalukigor
moskalukigor / init.js
Last active May 21, 2018 08:07
Temporary Block CF7 Submit on Send
var wpcf7Submit = $('.wpcf7-submit');
document.addEventListener( 'wpcf7submit', function( event ) {
wpcf7Submit.attr('disabled', 'disabled');
}, false);
document.addEventListener( 'wpcf7mailsent', function( event ) {
wpcf7Submit.removeAttr('disabled');
}, false);
document.addEventListener( 'wpcf7mailfailed', function( event ) {
wpcf7Submit.removeAttr('disabled');
@moskalukigor
moskalukigor / USEinsta.php
Created January 30, 2018 09:16 — forked from Tusko/USEinsta.php
Instagram API + Wordpress Cache
<?php
/*
You need to authorize your app with scope=public_content
Example:
https://api.instagram.com/oauth/authorize/?client_id=YOUR_CLIENT_ID&redirect_uri=REDIRECT_URL&response_type=token&scope=public_content
*/
$get_my_insta = instagram_feed('USERNAME', 'APP_IP', 'ACCESS_TOKEN', 10);
if ( $get_my_insta ) {
@moskalukigor
moskalukigor / functions.php
Last active February 21, 2018 13:07
popular product wordpress
<?php
function get_most_popular_products($atts = array())
{
extract($atts);
$posts_per_page = !isset( $posts_per_page ) ? 10 : $posts_per_page;
$order = !isset( $order ) ? "DESC" : $order;
$post_status = !isset( $post_status ) ? "publish" : $post_status;
$post_type = !isset( $post_type ) ? "product" : $post_type;
$args = array(
@moskalukigor
moskalukigor / functions.php
Created December 20, 2017 09:03
pre_dump
<?php
function pre_dump() {
echo '<pre style="background:#eee; border: solid 1px #ccc; color:#444; border-radius: 3px; padding:20px;">';
foreach (func_get_args() as $arg) {
var_dump($arg);
echo "<br>";
}
echo '</pre>';
}
@moskalukigor
moskalukigor / functions.php
Created December 19, 2017 14:25
Recursive return parent array by value
<?php
function recursive_return_parent_array_by_value($needle, $haystack) {
$return = false;
foreach ($haystack as $key => $val) {
if (is_array($val)) {
$return = recursive_return_parent_array_by_value($needle, $val);
if( isset($return['found']) )
return $return;
@moskalukigor
moskalukigor / getTaxTermsByOtherTaxTerm.php
Created October 4, 2017 08:54 — forked from vovadocent/getTaxTermsByOtherTaxTerm.php
Get Taxonomy Terms By Other Taxonomy Term
<?php
function getTaxTermsByOtherTaxTerm($taxonomy_1, $term_id, $taxonomy_2, $post_type = NULL) {
global $wpdb;
$post_type_q = !empty($post_type_q) ? "AND p.post_type = '$post_type'" : "";
$sql = "SELECT r.object_id FROM $wpdb->terms AS t
INNER JOIN $wpdb->term_taxonomy AS tt ON t.term_id = tt.term_id
INNER JOIN $wpdb->term_relationships AS r ON r.term_taxonomy_id = tt.term_taxonomy_id
INNER JOIN $wpdb->posts AS p ON (p.ID = r.object_id AND p.post_status = 'publish' $post_type_q)
WHERE tt.taxonomy = '$taxonomy_1' AND t.term_id = $term_id";
$res = $wpdb->get_col($sql);
@moskalukigor
moskalukigor / functions.php
Created September 26, 2017 10:00
Remove product from cart , id
add_filter( 'woocommerce_add_cart_item_data', 'wdm_empty_cart', 10, 3);
function wdm_empty_cart( $cart_item_data, $product_id, $variation_id )
{
global $woocommerce;
foreach ( $woocommerce->cart->get_cart() as $cart_item_key => $cart_item ) {
if($cart_item['product_id'] == 512 ){
$woocommerce->cart->remove_cart_item($cart_item_key);
}
}
@moskalukigor
moskalukigor / content-single-product.php
Created September 13, 2017 07:55 — forked from georgybu/content-single-product.php
WooCommerce - Show next\prev products from current product category (when viewing a single product) 1. If product is last -> Next product is first 2. If product is first -> Prev product is last forked from https://gist.github.com/2176823 (This question was in http://stackoverflow.com/questions/13597687/woocommerce-get-next-previous-product/13612387
<?php
// get next and prev products
// Author: Georgy Bunin (bunin.co.il@gmail.com)
// forked from https://gist.github.com/2176823
function ShowLinkToProduct($post_id, $categories_as_array, $label) {
// get post according post id
$query_args = array( 'post__in' => array($post_id), 'posts_per_page' => 1, 'post_status' => 'publish', 'post_type' => 'product', 'tax_query' => array(
array(
'taxonomy' => 'product_cat',