Created
July 19, 2019 08:39
-
-
Save conschneider/ceb2d250b63bb8ee3aecc19b7a1bc82f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
Plugin Name: Testing Code | |
Description: This is just a test plugin for some actions and filters. | |
Version: 0.1 | |
Author: Con | |
*/ | |
/** | |
* Security measure to prevent direct access. | |
* Always add that. | |
*/ | |
if (! defined('ABSPATH')) { | |
exit; // Exit if accessed directly | |
} | |
/** | |
* This function uses the woocommerce_states filter to modify the states | |
* loaded by the variable $states. | |
* To test go to your checkout and select United Kingdom, | |
* then select Counties. | |
*/ | |
function wc_uk_counties_add_counties($states) | |
{ | |
// redefine the states list for England. | |
$states['GB'] = array( | |
'AV' => 'Avon', | |
'BE' => 'Bedfordshire', | |
); | |
// Output our new states list for england. | |
return $states; | |
} | |
// Bind function to filter. | |
add_filter('woocommerce_states', 'wc_uk_counties_add_counties'); | |
/** | |
* This function uses the WordPress core wp_count_posts function | |
* to count all posts by type. Our type is 'product' | |
* | |
*/ | |
function woocat_product_count_shortcode() | |
{ | |
$count_posts = wp_count_posts('product'); | |
return $count_posts->publish; | |
} | |
// Bind to shortcode [woo_product_count]. | |
add_shortcode('woo_product_count', 'woocat_product_count_shortcode'); | |
/** | |
* This function greets the world and prooves you can make the machine talk. | |
* | |
*/ | |
function woocat_hello_world() | |
{ | |
echo "Hello World"; | |
} | |
// Bind to single product page before short description. | |
add_action('woocommerce_single_product_summary', 'woocat_product_count_shortcode'); | |
/** | |
* This function uses PHP core methods to show the IP address. | |
* | |
*/ | |
function come_get_ip() | |
{ | |
foreach (array('HTTP_CLIENT_IP', 'HTTP_X_FORWARDED_FOR', 'HTTP_X_FORWARDED', 'HTTP_X_CLUSTER_CLIENT_IP', 'HTTP_FORWARDED_FOR', 'HTTP_FORWARDED', 'REMOTE_ADDR') as $key) { | |
if (array_key_exists($key, $_SERVER) === true) { | |
foreach (explode(',', $_SERVER[$key]) as $ip) { | |
if (filter_var($ip, FILTER_VALIDATE_IP) !== false) { | |
//return $ip; | |
echo $ip . '<br>'; | |
} | |
} | |
} | |
} | |
} | |
// Bind to shortcode [show_ip]. | |
add_shortcode('show_ip', 'come_get_ip'); | |
/** | |
* Here are some advanced functions with limited comments. | |
* | |
*/ | |
function custom_notice_for_backorders_allowed() | |
{ | |
//instantinate product object | |
global $product; | |
//check if backorders are allowed | |
if ($product->backorders_allowed()) { | |
?> | |
Backorders allowed: Put some text and <a href="#">links here.</a> | |
<?php | |
} | |
} | |
// simple output after product title | |
add_action('woocommerce_single_product_summary', 'simple_output_after_product_titel'); | |
//output after product title | |
add_action('woocommerce_single_product_summary', 'custom_notice_for_backorders_not_allowed'); | |
function custom_notice_for_backorders_not_allowed() | |
{ | |
global $product; | |
//check if backorders are not allowed | |
if (! $product->backorders_allowed()) { | |
?> | |
Backorders not allowed: Put some text and <a href="#">links here.</a> | |
<?php | |
} | |
} | |
// hide coupon field on cart page | |
function hide_coupon_field_on_cart($enabled) | |
{ | |
if (is_cart()) { | |
$enabled = false; | |
} | |
return $enabled; | |
} | |
add_filter('woocommerce_coupons_enabled', 'hide_coupon_field_on_cart'); | |
// hide coupon field on checkout page | |
function hide_coupon_field_on_checkout($enabled) | |
{ | |
if (is_checkout()) { | |
$enabled = false; | |
} | |
return $enabled; | |
} | |
add_filter('woocommerce_coupons_enabled', 'hide_coupon_field_on_checkout'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment