How To: Add WooCommerce Name Your Price / Donate Amount Field Without A Plugin

Advertisement

WooCommerce plugin has been grown significantly that it allows us to add custom functionality without giving corn-size sweat, kidding!, but yeh, I’m serious.

This time we’ll be adding a custom function that allows us to add custom pricing to aka “name your price”, this could be added to any product you’re planning, or this could be suited to donation type product and still manage directly in WooCommerce.

Let’s get started

Register Our Donate Field

The below action will tell WooCommerce to register a new input field before the add-to-cart button.


/**
 * Display custom field to the front end
 */
function rstheme_woocommerce_before_add_to_cart_button_func() {
    ?>
	<table>
		<tr>
			<td>
				<label for="name_your_price">
                    <?php _e( "Name your price:", "aoim"); ?>
                </label>
            </td>
        </tr>
        <tr>
			<td>
				<input type="text" name="name_your_price" id="name_your_price" placeholder="Name your price" />
			</td>
		</tr>
	</table>
	<?php
}
add_action( 'woocommerce_before_add_to_cart_button', 'rstheme_woocommerce_before_add_to_cart_button_func' );

Store Our Donation Amount

By using the filter below this allows us to filter WooCommerce cart items and store inputted amount for later use.


/**
 * The woocommerce_add_cart_item_data filter allows us to add custom data to products when they’re added to the cart.
 */
function rstheme_add_cart_item_data_func( $cart_item_data, $product_id, $variation_id ) {
    // Has custom price added
    $name_your_price = $_POST['name_your_price'];

    if( $name_your_price &amp;&amp; is_numeric( $name_your_price ) ) {
        // Store the donation price for the product
        $cart_item_data['name_your_price'] = $name_your_price;
    }
    
    return $cart_item_data;
}
add_filter( 'woocommerce_add_cart_item_data', 'rstheme_add_cart_item_data_func', 10, 3 );

Set Amount

Before WooCommerce calculates product amounts or total we add an action to use the previously added meta for our product price.


/**
 * Set name your before adding product to cart
 */
function rstheme_before_calculate_totals_func( $cart ) {
    if ( is_admin() &amp;&amp; ! defined( 'DOING_AJAX' ) ) {
        return;
    }

    // Iterate through each cart item
    foreach( $cart-&gt;get_cart() as $key =&gt; $value ) {
        if( isset( $value['name_your_price'] ) ) {
            
            // get name_your_price value
            $price = $value['name_your_price'];
            
            // set name your price as the new product price added to cart
            $value['data']-&gt;set_price( ( $price ) );

        }
    }
}
add_action( 'woocommerce_before_calculate_totals', 'rstheme_before_calculate_totals_func', 10 );

Complete Code

Here’s a complete PHP code, this part you can copy and paste and have a go.


/**
 * Display custom field to the front end
 */
function rstheme_woocommerce_before_add_to_cart_button_func() {
    ?>
	<table>
		<tr>
			<td>
				<label for="name_your_price">
                    <?php _e( "Name your price:", "aoim"); ?>
                </label>
            </td>
        </tr>
        <tr>
			<td>
				<input type="text" name="name_your_price" id="name_your_price" placeholder="Name your price" />
			</td>
		</tr>
	</table>
	<?php
}
add_action( 'woocommerce_before_add_to_cart_button', 'rstheme_woocommerce_before_add_to_cart_button_func' );

/**
 * The woocommerce_add_cart_item_data filter allows us to add custom data to products when they’re added to the cart.
 */
function rstheme_add_cart_item_data_func( $cart_item_data, $product_id, $variation_id ) {
    // Has custom price added
    $name_your_price = $_POST['name_your_price'];

    if( $name_your_price && is_numeric( $name_your_price ) ) {
        $product    = wc_get_product( $product_id );
        $price      = $product->get_price();

        // Store the overall price for the product
        // $cart_item_data['name_your_price'] = $price + 250;   // This adds product price in total
        $cart_item_data['name_your_price'] = $name_your_price;
    }
    
    return $cart_item_data;
}
add_filter( 'woocommerce_add_cart_item_data', 'rstheme_add_cart_item_data_func', 10, 3 );

/**
 * Set name your before adding product to cart
 */
function rstheme_before_calculate_totals_func( $cart ) {
    if ( is_admin() && ! defined( 'DOING_AJAX' ) ) {
        return;
    }

    // Iterate through each cart item
    foreach( $cart->get_cart() as $key => $value ) {
        if( isset( $value['name_your_price'] ) ) {
            
            // get name_your_price value
            $price = $value['name_your_price'];
            
            // set name your price as the new product price added to cart
            $value['data']->set_price( ( $price ) );

        }
    }
}
add_action( 'woocommerce_before_calculate_totals', 'rstheme_before_calculate_totals_func', 10 );

That’s it, happy coding ^_^

Advertisement