Skip to content

Instantly share code, notes, and snippets.

View emre-edu-tech's full-sized avatar

Media Pons emre-edu-tech

View GitHub Profile
@emre-edu-tech
emre-edu-tech / functions.php
Last active May 4, 2024 14:21
Various code snippets for Crocoblock Plugins
// Crocoblock Code Snippets
// Jet Smart Filters
// -----------------
// Snippet No: 1
// This code below sorts an array of filters that are related to a Custom Post Type
// Because Custom Post Type values are not coming in alphabetical order, this code is needed
// 601 is the Id of Jet Smart Filter Type
function theme_name_jet_filter_options( $options, $filter_id, $filter ){
if($filter_id == 601){
asort($options);
@emre-edu-tech
emre-edu-tech / functions.php
Created May 1, 2024 06:11
Update the price suffix using woocommerce_get_price_html filter and show it only on Single Product Template
// Show price suffix only on single product page
add_filter( 'woocommerce_get_price_html', 'custom_price_suffix', 100, 2 );
function custom_price_suffix( $price, $product ) {
if(is_singular('product')) {
$price = $price . ' <span class="leoclean-price-suffix">Preis inkl. MwSt. & exkl. Versand</span>';
}
return apply_filters( 'woocommerce_get_price', $price );
}
@emre-edu-tech
emre-edu-tech / functions.php
Created April 27, 2024 11:39
Code snippet that adds Back to Products/Shop button on Single Product Template
// Add Back to Shop button on Single Product Template
add_action('woocommerce_before_single_product', 'print_back_to_products_btn');
function print_back_to_products_btn() {
$shop_page_url = get_permalink(woocommerce_get_page_id( 'shop' ));
echo '<a style="margin-bottom: 10px" class="button" href="' . $shop_page_url . '"><i class="fas fa-arrow-left"></i> All Products</a>';
}
// Replace the Add to Cart Button on Single Product Template to go to a link that is decided by an ACF plugin field
add_action( 'woocommerce_single_product_summary', 'replace_single_add_to_cart_button', 1 );
function custom_product_button(){
$product = wc_get_product();
// HERE your custom button text and link
$button_text = __( "Buy from external resource", "custom-textdomain" );
// Display button
echo '<a class="button" style="text-align: center; margin-bottom: 10px;" target="_blank" href="'.get_field('external_product_link', $product->get_id()).'">' . $button_text . '</a>';
}
@emre-edu-tech
emre-edu-tech / functions.php
Last active April 27, 2024 08:16
Change add to cart button to custom button on Product Archive Page
// Wooconmmerce - Change add to cart button to custom button on product archive page
add_filter( 'woocommerce_loop_add_to_cart_link', 'replacing_add_to_cart_button', 10, 2 );
function replacing_add_to_cart_button( $button, $product ) {
$button_text = __("Produkt Details", "the7dt-child");
$button = '<a class="button" href="' . $product->get_permalink() . '">' . $button_text . '</a>';
return $button;
}
@emre-edu-tech
emre-edu-tech / functions.php
Last active January 12, 2024 08:10
Woocommerce Update the product skus with the number that is gathered from the Product Title. SKU is in parentheses
// Update the product skus with the number that is gathered from the Product Title. SKU is in parentheses
// Make it available as an admin page
function update_product_skus() {
$args = array(
'post_type' => 'product',
'posts_per_page' => -1, // Retrieve all products; adjust if needed
);
$products = get_posts($args);
@emre-edu-tech
emre-edu-tech / functions.php
Last active December 5, 2023 17:39
Shortcode to check if a plugin is active. In this example, a plugin with the directory name and main plugin file is used.
<?php
add_shortcode('check_if_scraper_plugin_is_active', function() {
if(is_plugin_active('product-scraper/product-scraper.php')) {
return true;
} else {
return false;
}
});
@emre-edu-tech
emre-edu-tech / functions.php
Created November 21, 2023 09:49
Wordpress - Tweak the logout function. Redirect user to home page after logging out.
<?php
// Tweak the logout function to redirect to home page after logging out
add_action('wp_logout','auto_redirect_after_logout');
function auto_redirect_after_logout(){
wp_safe_redirect( home_url() );
exit;
}
@emre-edu-tech
emre-edu-tech / functions.php
Created November 21, 2023 09:43
Wocoommerce - Detect user Country on Checkout Page
<?php
function get_user_geo_country(){
if(is_checkout()) {
$geo = new WC_Geolocation(); // Get WC_Geolocation instance object
$user_ip = $geo->get_ip_address(); // Get user IP
$user_geo = $geo->geolocate_ip( $user_ip ); // Get geolocated user data.
$country = $user_geo['country']; // Get the country code
if($country != 'DE') {
?>
@emre-edu-tech
emre-edu-tech / functions.php
Created November 21, 2023 09:31
Various Woocommerce Filters about Products and Categories
// Remove product count from category listing
add_filter( 'woocommerce_subcategory_count_html', '__return_false' );