I am looking for a snippet to Edit WooCommerce Prices Programmatically for ALL products: Simple Products and Product variations
The best I've found is this method which uses a coupon. Problem for me is that this method does not really modify the price but apply the the coupon instead.
- I need a code to really modify the price for all products
- Doing this in the Database would be a right method too.
add_action( 'woocommerce_before_cart', 'quadlayers_coupon_individual_products' );
function quadlayers_coupon_individual_products () {
$coupon_code = 'quadlayers30'; //Use your coupon code between single quotes' '
if ( WC()->cart->has_discount( $coupon_code ) ) return;
foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
$autocoupon = array( 2970 ); //Use your product ID inside the array( id )
if ( in_array( $cart_item['product_id'], $autocoupon ) ) {
WC()->cart->apply_coupon( $coupon_code );
wc_print_notices();
}
}
}