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 November 21, 2023 09:23
Show quantity and quantity buttons next to Add to Cart buttons on Woocommerce Product Archive Template
<?php
/**
* Override loop template and show quantity and quantity buttons next to add to cart buttons
*/
add_filter( 'woocommerce_loop_add_to_cart_link', 'quantity_inputs_for_woocommerce_loop_add_to_cart_link', 10, 2 );
function quantity_inputs_for_woocommerce_loop_add_to_cart_link( $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 );
@emre-edu-tech
emre-edu-tech / functions.php
Last active October 13, 2024 07:14
Filter to remove the product count from the list of categories
<?php
// Remove product count from category listing
add_filter( 'woocommerce_subcategory_count_html', '__return_false' );
@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: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
Last active October 13, 2024 07:10
Woocommerce Update the product skus with the number that is gathered from the Product Title. In this example SKU is in parentheses in Product Title.
<?php
// 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
);
@emre-edu-tech
emre-edu-tech / functions.php
Last active October 13, 2024 07:03
Change add to cart button to custom button on Product Archive Page
<?php
// 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", "textdomain");
$button = '<a class="button" href="' . $product->get_permalink() . '">' . $button_text . '</a>';
return $button;
}
<?php
// Replace the Add to Cart Button on Single Product Template to go to a link that is also an ACF plugin URL 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
@emre-edu-tech
emre-edu-tech / functions.php
Last active October 13, 2024 06:54
Code snippet that adds Back to Products/Shop button on Single Product Template
<?php
// Add "Back to Shop" button on Single Product Template
// Sometimes it is useful a button to get back to Shop page
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>';
}
@emre-edu-tech
emre-edu-tech / functions.php
Last active October 13, 2024 07:08
Update the price suffix using woocommerce_get_price_html filter and show it only on Single Product Template
<?php
// 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="mpons-price-suffix">Preis inkl. MwSt. & exkl. Versand</span>';
}
return apply_filters( 'woocommerce_get_price', $price );
}
@emre-edu-tech
emre-edu-tech / functions.php
Last active October 13, 2024 06:51
Various code snippets for Crocoblock Plugins
<?php
// 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 ){