Skip to content

Instantly share code, notes, and snippets.

@Acephalia
Acephalia / wchacbfnliu.php
Created April 21, 2024 10:15
Woocommerce hide add to cart button for non logged in users but show stock.
//Hide add to cart button for non logged in users but show stock.
function hide_add_to_cart_for_non_logged_in_users() {
if (!is_user_logged_in()) {
?>
<style>
form.cart {
display: none;
}
</style>
@Acephalia
Acephalia / wpcitwebp.php
Created April 14, 2024 22:33
Wordpress Convert Image To WebP and Delete Original File On Upload
//Convert uploaded files to webp and delete uploaded file
function compress_and_convert_images_to_webp($file) {
// Check if file type is supported
$supported_types = ['image/jpeg', 'image/jpg', 'image/png', 'image/webp'];
if (!in_array($file['type'], $supported_types)) {
return $file;
}
// Get the path to the upload directory
@Acephalia
Acephalia / wcttc.php
Last active April 11, 2024 07:46
Woocommerce add to cart for product thumbnail link
//Don't use this it is a bad idea. This code replaces all acrhcive and shop page thumbnail links to work as add to cart urls. When clicked the item will be added to cart instead of opening the single product page. Works only for simple products (obviously).
function wc_custom_shop_thumbnail_link( $link, $product ) {
if ( $product->is_type( 'simple' ) && ( is_shop() || is_product_category() || is_product_tag() ) ) {
$link = '?add-to-cart='. $product->get_id();
}
return $link;
}
add_filter( 'woocommerce_loop_product_link', 'wc_custom_shop_thumbnail_link', 10, 2 );
@Acephalia
Acephalia / wcdquss.php
Created March 25, 2024 20:58
Woocommerce Dequeue Unused Styles & Scripts
add_action( 'template_redirect', 'remove_woocommerce_styles_scripts', 999 );
function remove_woocommerce_styles_scripts() {
if ( function_exists( 'is_woocommerce' ) ) {
if ( ! is_woocommerce() && ! is_cart() && ! is_checkout() ) {
remove_action('wp_enqueue_scripts', [WC_Frontend_Scripts::class, 'load_scripts']);
remove_action('wp_print_scripts', [WC_Frontend_Scripts::class, 'localize_printed_scripts'], 5);
remove_action('wp_print_footer_scripts', [WC_Frontend_Scripts::class, 'localize_printed_scripts'], 5);
}
}
@Acephalia
Acephalia / wcppen.php
Created March 20, 2024 23:35
Woocommerce Change Password Protected product Error Notice
function custom_password_protected_error_message( $translated_text, $text, $domain ) {
if ( $text === 'This product is protected and cannot be purchased.' ) {
$translated_text = 'Your custom error message here.';
}
return $translated_text;
}
add_filter( 'gettext', 'custom_password_protected_error_message', 20, 3 );
@Acephalia
Acephalia / wbrsp.php
Created March 9, 2024 03:42
Woocommerce Bulk Resave/Update All Products
// This code will resave / update 200 woocommerce products every time you visit one of your pages. Increase or decrease limit depending on your server resources.
add_action( 'woocommerce_loaded', 'update_products_by_x' );
function update_products_by_x(){
$limit = 200;
// getting all products
$products_ids = get_posts( array(
'post_type' => 'product', // or ['product','product_variation'],
'numberposts' => $limit,
'post_status' => 'publish',
@Acephalia
Acephalia / wcraoattach.php
Created March 9, 2024 03:39
Woocommerce Fix CSV Imported Images Showing Up As Unattached In Media Gallery
//This script will reattach media files to their relvant products (when you open and update a product) that do not get assigned when you import via a csv.
add_action( 'woocommerce_process_product_meta', function ( $post_id, $post ) {
global $wpdb;
$attachment_ids = isset( $_POST['product_image_gallery'] ) ? array_filter( explode( ',', wc_clean( $_POST['product_image_gallery'] ) ) ) : array();
// update posts in the $attachment_ids array to have post_parent = $post_id
$wpdb->query( $wpdb->prepare( "UPDATE $wpdb->posts SET post_parent = %d WHERE ID IN (" . implode( ',', $attachment_ids ) . ")", $post_id ) );
}, 21, 2 );
@Acephalia
Acephalia / wrgi.sql
Created March 9, 2024 03:17
Woocommerce Remove Gallery Images
This SQL query wll remove all gallery images from all Woo products but leave the Product Image in tact. It will not delete the images from the media gallery. Just detach them from the gallery.
DELETE pm
FROM pwj_postmeta pm
INNER JOIN pwj_posts p ON pm.post_id = p.ID
WHERE p.post_type = 'product'
AND pm.meta_key = '_product_image_gallery';
@Acephalia
Acephalia / wpsfsf.php
Created February 22, 2024 07:24
Wordpress Submit Sendfox Form On User Registration
// Wordpress Submit Sendfox Form On User Registration by u/acephaliax
// This snippet will submit a sendfox form of your choosing anytime a new user signs up via a non standard resgstarion method on your wordpress site. Replace the form details below with the form from your dashboard.
function auto_submit_sendfox_form_on_user_register( $user_id ) {
$user_info = get_userdata( $user_id );
// Prepare the data to be sent
$body = array(
'first_name' => $user_info->first_name,
'last_name' => $user_info->last_name,
@Acephalia
Acephalia / wcgiat.php
Last active February 10, 2024 12:17
Swap Woocommerce Product Thumbnails With First Gallery Image
//Swap Woocommerce product thumbnail with first image in gallery by u/acephaliax
//This snippet will swap out the product thumbnail for all archive and shop loops to display first galery image but show product image as main image on a single product page.
add_filter('woocommerce_product_get_image', 'custom_woocommerce_product_get_image', 10, 5);
function custom_woocommerce_product_get_image($image, $product, $size, $attr, $placeholder) {
if (is_shop() || is_product_category() || is_product_tag()) { // Adjust condition as needed
$gallery_images = $product->get_gallery_image_ids();
if (!empty($gallery_images)) {
$image_id = $gallery_images[0]; // Sets first gallery image as thumbnail