Skip to content

Instantly share code, notes, and snippets.

View geotsiokos's full-sized avatar
🏠
Working from home

George Tsiokos geotsiokos

🏠
Working from home
View GitHub Profile
@geotsiokos
geotsiokos / functions.php
Created January 10, 2024 17:39
Orders products by stock, putting out-of-stock ones to the end of the list
// Orders products by stock, putting out-of-stock ones to the end of the shop and category archives
add_filter( 'posts_clauses', 'order_by_stock_status' );
function order_by_stock_status( $args ) {
global $wpdb;
$join_suffix = $args['join'];
if ( is_woocommerce() && !is_admin() ) {
$args['join'] = " LEFT JOIN {$wpdb->wc_product_meta_lookup} wc_product_meta_lookup ON $wpdb->posts.ID = wc_product_meta_lookup.product_id " . $join_suffix;
$args['orderby'] = ' wc_product_meta_lookup.stock_status ASC, wc_product_meta_lookup.product_id DESC ';
// Appends Affiliates URL parameter and ID in every URL, when the affiliate cookie exists
add_action( 'template_redirect', 'affiliates_pname_redirect' );
function affiliates_pname_redirect() {
$pname = get_option( 'aff_pname', AFFILIATES_PNAME );
if( isset( $_COOKIE[AFFILIATES_COOKIE_NAME] ) && !$_GET[$pname] ) {
$location = esc_url( add_query_arg( $pname, $_COOKIE[AFFILIATES_COOKIE_NAME] ) );
wp_redirect( $location );
}
}
@geotsiokos
geotsiokos / functions.php
Created November 11, 2021 21:26
Adds a hyphen to the product SKU right after three characters starting from the left
// Adds a hyphen to the product SKU right after three characters starting from the left
add_filter ( 'woocommerce_product_search_indexer_filter_content', 'example_woocommerce_product_search_indexer_filter_content', 10, 3 );
function example_woocommerce_product_search_indexer_filter_content( $content, $context, $post_id ) {
if ( $context === 'post_content' ) {
$product = wc_get_product( $post_id );
if ( $product ) {
$product_sku = $product->get_sku();
$alternative_product_sku = substr_replace( $product_sku, '-', 3, 0 );
$content .= ' ' . $alternative_product_sku;
}
@geotsiokos
geotsiokos / functions.php
Last active August 24, 2022 13:13
Adds extra column in Affiliates > Total, Export file for custom registration field with name extra_field
add_filter( 'affiliates_totals_export_file_headings', 'example_affiliates_totals_export_file_headings' );
function example_affiliates_totals_export_file_headings( $headings ) {
$headings[] = 'Data';
return $headings;
}
add_filter( 'affiliates_totals_export_file_line', 'example_affiliates_totals_export_file_line', 10, 4 );
function example_affiliates_totals_export_file_line( $line, $affiliate_id, $user_id, $result ) {
$extra_field = get_user_meta( $user_id, 'extra_field', true );
@geotsiokos
geotsiokos / functions.php
Created April 8, 2020 16:55
Display WC categories/subcategories on shop and archive templates
add_action( 'woocommerce_before_shop_loop', 'example_woo_list_categories' );
/**
* Action Callback
*/
function example_woo_list_categories() {
$result = false;
$output = '';
if ( is_product_category() ) {
$queried_object = get_queried_object();
@geotsiokos
geotsiokos / functions.php
Created March 20, 2020 10:40
Modifies the product description shown on WPS live search results
add_filter( 'woocommerce_product_search_field_product_description', 'wpsrd_woocommerce_product_search_field_product_description', 10, 2 );
function wpsrd_woocommerce_product_search_field_product_description( $description, $post_id ) {
if ( $post_id == 32 ) { // here you should use the product id you desire instead of 32
$description = 'a description'; // write a description enclosed in single quotes
}
return $description;
}
@geotsiokos
geotsiokos / functions.php
Created November 4, 2019 16:46
Display referring affiliate id in a column when visiting shop orders in WP back-end
add_filter( 'manage_edit-shop_order_columns', 'custom_shop_order_column', 20 );
function custom_shop_order_column($columns) {
$reordered_columns = array();
// Inserting columns to a specific location
foreach( $columns as $key => $column){
$reordered_columns[$key] = $column;
if ( $key == 'order_status' ) {
// Inserting after "Status" column
$reordered_columns['my-column1'] = esc_html( 'Referrer' );
@geotsiokos
geotsiokos / gist:9c31282bba00efa97145676d2c5dbfd4
Created October 12, 2019 11:26
Modify "Thanks for signing up!" message for new affiliate registrations
add_filter( 'affiliates_thanks_sign_up_text', 'example_affiliates_thanks_sign_up_text' );
function example_affiliates_thanks_sign_up_text( $text ) {
// the default text is this:
$text = '<p>' . esc_html__( 'Thanks for signing up!', 'affiliates' ) . '</p>';
return $text;
}
add_action( 'woocommerce_after_account_downloads', 'example_woocommerce_after_account_downloads' );
function example_woocommerce_after_account_downloads() {
if (
class_exists( 'Groups_Group' ) &&
class_exists( 'Groups_User_Group' )
) {
$group_ids = Groups_Group::get_group_ids();
$user_id = get_current_user_id();
foreach ( $group_ids as $group_id ) {
if ( Groups_User_Group::read( $user_id, $group_id ) ) {
add_filter( 'affiliates_supported_currencies', 'example_affiliates_supported_currencies' );
function example_affiliates_supported_currencies( $currencies ) {
$currencies[] = 'INR';
return $currencies;
}