Skip to content

Instantly share code, notes, and snippets.

@plugin-republic
plugin-republic / pr_query_attachments.php
Created April 29, 2024 08:43
Query WordPress attachments with no alt text and automatically create alt text using file name
<?php
/**
* Query WordPress attachments with no alt text
* Automatically create alt text using file name
* Remove this snippet once you've run it
*/
function pr_query_attachments() {
if( did_action( 'init' ) > 1 ) {
return;
}
<?php
/**
* Remove quantity input field in cart for specific products
*/
function prefix_cart_item_quantity( $product_quantity, $cart_item_key ){
$cart_item = WC()->cart->cart_contents[ $cart_item_key ];
$product_id = $cart_item['data']->get_id();
if( in_array( $product_id, array( 467, 789 ) ) ){
$product_quantity = $cart_item['quantity'];
}
<?php
/**
* Add div inside group wrapper
*/
function prefix_open_group_inner( $group_id, $group, $group_index ) {
echo '<div class="my-group-inner">';
}
add_action( 'pewc_open_group_inner', 'prefix_open_group_inner', 10, 4 );
function prefix_close_group_inner( $group_id, $group, $group_index ) {
<?php
/**
* Filter group wrapper classes
* @param $classes Array
*/
function prefix_filter_group_wrapper_class( $classes, $group_id, $group, $product_id ) {
$classes[] = 'my-new-group-class';
return $classes;
}
add_filter( 'pewc_filter_group_wrapper_class', 'prefix_filter_group_wrapper_class', 10, 4 );
<?php
/**
* Open a new div to wrap multiple groups
* Ensure you update the group ID
*/
function prefix_open_group_wrapper( $group_id, $group, $group_index ) {
if( $group_id == 455 ) {
echo '<div class="my-group-wrapper">';
}
}
<?php
/**
* Array of discount rates
*/
function prefix_discount_rates( $rates, $product_id ) {
$excluded_ids = array( 1234 );
if( in_array( $product_id, $excluded_ids ) ) {
return $rates;
}
$rates = array(
<?php
/**
* Enable text values from calculation lookup tables
*/
add_filter( 'pewc_allow_text_calcs', '__return_true' );
<?php
/**
* Specify the default quantities here
* You'll need to update the child product IDs to match yours
*/
function florian_child_quantity( $quantity_field_value, $child_product_id, $item ) {
if( $child_product_id == 267 ) {
$quantity_field_value = 3;
}
if( $child_product_id == 269 ) {
<?php
/**
* Force a child product to be automatically selected
*/
function prefix_filter_default_child_independent_quantity( $quantity_field_value, $child_product_id, $item ) {
if( $child_product_id == 9999 ) { // Change this to your child product ID
return 1;
}
return $quantity_field_value;
}
<?php
/**
* Ensure tax isn't added to options with 'value only'
*/
function prefix_check_tax_for_option_price( $check, $option_price, $item ) {
if( ! empty( $item['option_price_visibility'] && $item['option_price_visibility'] == 'value' ) ) {
return false;
}
return $check;
}