Filter Uncanny Redemption Codes

ulc_modify_add_to_cart

Filters the HTML output for the "Add to Cart" button in WooCommerce, allowing customization before it's displayed.

add_filter( 'ulc_modify_add_to_cart', $callback, 10, 1 );

Description

Filters the HTML for the "Add to Cart" button for specific custom product types. Developers can use this hook to modify or replace the default button HTML, allowing for custom styling, conditional logic, or integration with other features before the button is displayed in WooCommerce.


Usage

add_filter( 'ulc_modify_add_to_cart', 'your_function_name', 10, 1 );

Return Value

The filtered value.


Examples

/**
 * Modify the add to cart HTML for a custom product type.
 *
 * This example adds a custom message before the add to cart button
 * if the product has a price greater than 100.
 *
 * @param mixed $html The original add to cart HTML.
 * @return mixed The modified add to cart HTML.
 */
add_filter( 'ulc_modify_add_to_cart', function( $html ) {
	global $product;

	// Check if the product object is valid and has a price.
	if ( ! $product instanceof WC_Product || ! $product->get_price() ) {
		return $html;
	}

	$product_price = (float) $product->get_price();

	// Add custom message if price is over 100.
	if ( $product_price > 100 ) {
		$custom_message = '<p style="color: green; font-weight: bold;">Special offer on this premium product!</p>';
		$html = $custom_message . $html;
	}

	return $html;
}, 10, 1 );

Placement

This code should be placed in the functions.php file of your active theme, a custom plugin, or using a code snippets plugin.


Source Code

src/classes/integrations/woocommerce/class-woo-automator-codes.php:260

public function custom_product_add_to_cart() {
		global $product;
		if ( empty( $product->get_price() ) ) {
			if ( absint( $product->get_price() ) < 0 ) {
				return;
			}
		}

		// Make sure it's our custom product type.
		$codes_available = SharedFunctionality::get_available_codes_by_group_id( $product->get_id() );
		if ( $codes_available > 0 ) {
			$html = '';

			do_action( 'woocommerce_before_add_to_cart_button' );

			ob_start();

			echo '<form action="' . esc_url( apply_filters( 'woocommerce_add_to_cart_form_action', $product->add_to_cart_url() ) ) . '" class="cart" method="post" enctype="multipart/form-data">';

			do_action( 'woocommerce_before_add_to_cart_button' );
			do_action( 'woocommerce_before_add_to_cart_quantity' );

			echo woocommerce_quantity_input(
				array(
					'max_value' => $codes_available,
					'min_value' => 1,
				),
				$product,
				false
			);

			do_action( 'woocommerce_after_add_to_cart_quantity' );

			echo '<button type="submit" class="' . esc_attr( $this->product_type ) . '_add_to_cart_button button alt" name="add-to-cart" value="' . esc_attr( $product->get_id() ) . '">' . esc_html( $product->add_to_cart_text() ) . '</button>';

			do_action( 'woocommerce_after_add_to_cart_button' );

			echo '</form>';

			do_action( 'woocommerce_after_add_to_cart_form' );

			$html = ob_get_clean();
			echo apply_filters( 'ulc_modify_add_to_cart', $html );

			$display      = __( 'In stock', 'woocommerce' );
			$stock_amount = $codes_available;

			switch ( get_option( 'woocommerce_stock_format' ) ) {
				case 'low_amount':
					if ( $stock_amount <= wc_get_low_stock_amount( $product ) ) {
						/* translators: %s: stock amount */
						$display = sprintf( __( 'Only %s left in stock', 'woocommerce' ), wc_format_stock_quantity_for_display( $stock_amount, $product ) );
					}
					break;
				case '':
					/* translators: %s: stock amount */
					$display = sprintf( __( '%s in stock', 'woocommerce' ), wc_format_stock_quantity_for_display( $stock_amount, $product ) );
					break;
			}

			if ( $product->backorders_allowed() && $product->backorders_require_notification() ) {
				$display .= ' ' . __( '(can be backordered)', 'woocommerce' );
			}
			?>
			<p class="stock instock"><?php echo wp_kses_post( $display ); // WPCS: XSS ok. ?></p>
			<?php
		}
	}


Scroll to Top