Skip to content

Instantly share code, notes, and snippets.

@grola
grola / functions.php
Created February 8, 2018 09:29
Flexible Checkout Fields - custom validation method
<?php // do not copy this line if already opened <?php tag
function example_function_is_integer( $field_label, $value ) {
if ( ! ( (is_numeric($value) ? intval($value) == $value : false) ) ) {
wc_add_notice( sprintf( '%s is not integer!', '<strong>' . $field_label . '</strong>' ), 'error' );
}
}
add_filter( 'flexible_checkout_fields_custom_validation', 'example_flexible_checkout_fields_custom_validation' );
function example_flexible_checkout_fields_custom_validation( $custom_validation ) {
@grola
grola / functions.php
Created February 15, 2018 20:55
Price suffix from category
<?php
class studiowp_single_product_price_suffix {
private $suffix;
private $category_id;
/**
* studiowp_single_product_price_suffix constructor.
*
@grola
grola / functions.php
Created February 15, 2018 20:55
Price suffix from category
<?php
class studiowp_single_product_price_suffix {
private $suffix;
private $category_id;
/**
* studiowp_single_product_price_suffix constructor.
*
@grola
grola / functions.php
Last active February 21, 2018 19:55
Add/remove category to product dependent on stock quantity
<?php
class studiowp_product_category_on_stock_change {
protected $category_id;
protected $stock_level;
protected $variable_product_id = 0;
protected $variable_product_add_category = false;
@grola
grola / functions.php
Created February 26, 2018 13:15
Add parameter to attachment url
add_filter( 'wp_get_attachment_url', 'grola_wp_get_attachment_url', 10, 2 );
function grola_wp_get_attachment_url( $url, $post_id ) {
return add_query_arg( 'p2', $post_id, add_query_arg( 'p', $post_id+1, $url ) );
}
@grola
grola / functions.php
Last active February 28, 2018 20:38
Add customer note to all admin emails
<?php
add_action( 'woocommerce_email_after_order_table', 'studiowp_woocommerce_email_after_order_table_customer_note', 10, 4 );
function studiowp_woocommerce_email_after_order_table_customer_note( $order, $sent_to_admin, $plain_text, $email ) {
if ( $sent_to_admin ) {
if ( $order->get_customer_note() != '' ) {
echo sprintf( '<h2>%s</h2>', __( 'Customer note', 'woocommerce' ) );
echo sprintf( '<p><strong>%s</strong></p>', $order->get_customer_note() );
}
}
@grola
grola / functions.php
Last active March 6, 2018 09:27
Prepare cart with products
<?php
class studiowp_add_products_to_cart {
private $products = array();
private $id = '';
public function __construct( $id = 1, $products = array() ) {
$this->products = $products;
$this->id = $id;
@grola
grola / functions.php
Created March 27, 2018 06:41
Add anchor to cart URL
<?php
add_filter( 'woocommerce_get_cart_page_permalink', 'studiowp_woocommerce_get_cart_page_permalink' );
function studiowp_woocommerce_get_cart_page_permalink( $permalink ) {
return $permalink . '#my_id';
}
@grola
grola / functions.php
Last active April 5, 2018 12:44
Flexible shipping: prices include tax
<?php
add_filter( 'flexible_shipping_prices_include_tax', 'wpdesk_flexible_shipping_prices_include_tax' );
function wpdesk_flexible_shipping_prices_include_tax() {
return 'yes' === get_option( 'woocommerce_prices_include_tax', 'no' );
}
@grola
grola / functions.php
Created July 9, 2018 11:29
Always display Get request in DPD UK metabox
add_filter( 'woocommerce_dpd_uk_get_request_always_visible', '__return_true' );