Skip to content

Instantly share code, notes, and snippets.

View SiR-DanieL's full-sized avatar

Nicola Mustone SiR-DanieL

View GitHub Profile
@SiR-DanieL
SiR-DanieL / functions.php
Last active October 30, 2023 10:17
Add Text to The Emails Body
<?php
add_action( 'woocommerce_email_header', 'add_email_header', 20 );
function add_email_header() {
echo '<h2>My Heading</h2>';
echo '<p>Add content here</p>';
}
@SiR-DanieL
SiR-DanieL / functions.php
Last active October 30, 2023 10:14
Customize Pre-Order Products
<?php
add_filter( 'body_class', 'add_preorder_class' );
add_filter( 'post_class', 'add_preorder_class' );
function add_preorder_class( $classes ) {
global $post;
$product = wc_get_product( $post->ID );
if ( $product && 'yes' === get_post_meta( $product->get_id(), '_wc_pre_orders_enabled', true ) ) {
$classes[] = 'pre-order';
}
@SiR-DanieL
SiR-DanieL / functions.php
Last active October 30, 2023 10:13
Set a Default Price for Products
<?php
/**
* Sets the product default price to 10. Only works once, if the price is not specified.
*
* @param int $post_id
* @param object $post
*/
function set_product_default_price( $post_id, $post ) {
$product = wc_get_product( $post_id );
$already_set = get_post_meta( $post_id, '_set_default_price', true );
@SiR-DanieL
SiR-DanieL / functions.php
Last active October 30, 2023 10:11
Customize the Footer Credits in Storefront
<?php
/**
* Display the theme credit
*
* @since 1.0.0
* @return void
*/
function storefront_credit() {
?>
<div class="site-info">
@SiR-DanieL
SiR-DanieL / functions.php
Last active October 30, 2023 10:09
WordPress Discord Post: Send only specific categories
<?php
add_filter( 'wp_discord_post_is_new_post', 'wp_discord_post_limit_by_category' );
function wp_discord_post_limit_by_category( $post ) {
return has_category( array( 'Category 1', 'Category 2' ), $post );
}
@SiR-DanieL
SiR-DanieL / functions.php
Last active October 30, 2023 10:09
WordPress Discord Post: Send only specific categories
<?php
add_filter( 'wp_discord_post_is_new_post', 'wp_discord_post_limit_by_category' );
function wp_discord_post_limit_by_category( $post ) {
return has_category( 'Your Category Name', $post );
}
@SiR-DanieL
SiR-DanieL / functions.php
Last active October 30, 2023 10:07
How to limit purchases to one per order
<?php
add_filter( 'woocommerce_add_to_cart_validation', 'wc_limit_one_per_order', 10, 2 );
function wc_limit_one_per_order( $passed_validation, $product_id ) {
if ( 31 !== $product_id ) {
return $passed_validation;
}
if ( WC()->cart->get_cart_contents_count() >= 1 ) {
wc_add_notice( __( 'This product cannot be purchased with other products. Please, empty your cart first and then add it again.', 'woocommerce' ), 'error' );
return false;
@SiR-DanieL
SiR-DanieL / functions.php
Last active October 30, 2023 10:04
Post-sale cross-sell with WooCommerce
<?php
add_action( 'woocommerce_order_details_after_order_table', 'post_sale_cross_sell' );
function post_sale_cross_sell( $order ) {
$cross_sells = array();
$in_order = array();
foreach ( $order->get_items() as $item ) {
$product_id = $order->get_item( $item )->get_product_id();
$product = wc_get_product( $product_id );
$cross_sells = array_merge( $product->get_cross_sell_ids(), $cross_sells );
@SiR-DanieL
SiR-DanieL / functions.php
Last active October 30, 2023 10:00
Delete Expired Coupons Automatically
<?php
// Delete Expired Coupons that have an expiration date set and are not from AutomateWoo
add_action('delete_expired_coupons_hook', 'delete_expired_coupons_action');
function delete_expired_coupons_action() {
$query_args = [
'fields' => 'ids',
'post_type' => 'shop_coupon',
'post_status' => 'any',
'posts_per_page' => -1,
'orderby' => 'date',
@SiR-DanieL
SiR-DanieL / functions.php
Last active October 30, 2023 07:57
Add an Empty Cart Button in WooCommerce
<?php
add_action( 'woocommerce_cart_coupon', 'woocommerce_empty_cart_button' );
function woocommerce_empty_cart_button() {
echo '<a href="' . esc_url( add_query_arg( 'empty_cart', 'yes' ) ) . '" class="button" title="' . esc_attr( 'Empty Cart', 'woocommerce' ) . '">' . esc_html( 'Empty Cart', 'woocommerce' ) . '</a>';
}