This website uses cookies

Our website, platform and/or any sub domains use cookies to understand how you use our services, and to improve both your experience and our marketing relevance.

📣 Try the fastest hosting platform with pay-as-you-go pricing & 24/7 expert support! MIGRATE NOW →

How to Setup WooCommerce Deposit for Store Product

Updated on April 20, 2021

4 Min Read
WooCommerce Deposit

Deposits are common payment options on ecommerce stores that deal in made-to-order or high-worth items. In many cases, store owners charge a deposit to cover the initial expenses and to ensure continued customer interest in the transaction.

While WooCommerce deposits are not a standard payment option at the platform, it is a popular ecommerce payment option that preferred by a significant section of merchants. The idea is to provide a custom deposit field that would display the amount of the deposit and then deduct the amount in the final cart checkout stage.

Add Custom WooCommerce Deposit Field in the Product Tab 

The following code snippet adds a custom field in the Product tab. Storeowners can set their choice of the deposit amount for the products in the store.

function add_custom_fields_seo() {
     global $woocommerce, $post;
        woocommerce_wp_text_input(
         array(
                'id'      => '_deposit_price',
                'label'   => __( 'Deposit price((£)', 'woocommerce' ),
                'placeholder' => '15',
                'desc_tip' => 'true',
                'description' => __( 'Enter the custom deposit price here SEO sugeested keyword .', 'woocommerce' )
         )
     );
  }
add_action( 'woocommerce_product_options_general_product_data', 'add_custom_fields_seo');

To access the field, go to the Products > All Products > Product Data > Simple Product. Here’s how the field looks on the platform.

deposite price

In the above code snippet, woocommerce_wp_text_input() passes an array as an argument (The array allows you to pass multiple arguments to the hook). Note the array element “id” that identifies the field value. Finally, woocommerce_product_options_general_product_data() is the WordPress hook that actually executes the action.

Now, the next step is to save the value of the custom field as the metadata of the product. This is accomplished by the following code snippet:

function custom_fields_save_seo( $post_id ){
$woocommerce_text_field = sanitize_text_field( $_POST['_deposit_price'] );
if( is_numeric( $woocommerce_text_field ) || empty( $woocommerce_text_field ) ){
     update_post_meta( $post_id, '_deposit_price', esc_attr( $woocommerce_text_field ) );
}
}
add_action( 'woocommerce_process_product_meta', 'custom_fields_save_seo' );

value saved

In the above snippet, the id ‘_deposit_price’ saves the deposit price. Next, update_post_meta() actually updates the value of the custom deposit field (the meta key) for the post.

Return Deposit Amount

Since the deposit is now set, it is important that the product price should reflect this fact. For this, the following code snippet checks if the product has a deposit price set. If this is the case, the deposit price is returned instead of the normal price. If the deposit price is not set,  the function returns zero.

function filter_woocommerce_get_price( $price, $product ){
$product_id = $product->get_id();
$deposit_price = get_post_meta( $product_id, '_deposit_price', true );
if( ! empty( $deposit_price )  ) {
     return $deposit_price;
}
return 0;
}
add_filter( 'woocommerce_get_price', 'filter_woocommerce_get_price', 10, 2 );

Here is how the product page would look if the WooCommerce deposit is not set.

deposite price not set

In the above code snippet, $deposit_price get the filed using id_deposit_price using get_post_meta.For this, get_post_meta uses a meta key to retrieve the information (by default, this returns the data of all keys).

Setting the Prices at the Product & Checkout  Pages

For the purpose of this tutorial, I have decided upon the following:

Full Price: £85.00

Deposit: £15.00

These two price items will be displayed on both the Product page and the Checkout/Cart page. For this, the following code snippet will do the trick:

function filter_woocommerce_get_price_html( $price, $product ){
$product_id = $product->get_id();
$deposit_price = get_post_meta( $product_id, '_deposit_price', true );
$product_price = get_post_meta( $product_id, '_price', true );
if( ! empty( $deposit_price )  ) {
    return wc_price($product_price) . ' <i>Deposit: ' . wc_price($deposit_price) . '</i>';
}
return wc_price( $product_price );
}
add_filter( 'woocommerce_get_price_html', 'filter_woocommerce_get_price_html', 10, 2 );
add_filter( 'woocommerce_cart_product_price', 'filter_woocommerce_get_price_html', 10, 2 );
function filter_woocommerce_cart_product_subtotal( $product_subtotal, $product, $quantity ){
$product_id = $product->get_id();
$deposit_price = get_post_meta( $product_id, '_deposit_price', true );
$product_price = get_post_meta( $product_id, '_price', true );
if( ! empty( $deposit_price )  ) {
     return wc_price( $product_price * $quantity ) . ' <i>Deposit: ' . wc_price( $deposit_price * $quantity ) . '</i>';
}
return wc_price( $product_price * $quantity );
}
add_filter( 'woocommerce_cart_product_subtotal', 'filter_woocommerce_cart_product_subtotal', 10, 3 );

This is how the price items will appear on the product page:

WooCommerce deposite

Wrapping Up

WooCommerce Deposit is an important payment option that is common in niches hat deal in bespoke items. As you can see, setting up the deposit field and displaying the deposit amount on product and checkout pages is a simple matter of adding code snippets to the functions.php.

If you need help with implementing the idea on your WooCommerce store, drop a comment below and I will get back to you.

Share your opinion in the comment section. COMMENT NOW

Share This Article

Owais Alam

is the WordPress Community Manager at Cloudways - A Managed WooCommerce Hosting Platform and a seasoned PHP developer. He loves to develop all sorts of websites on WordPress and is in love with WooCommerce in particular. You can email him at [email protected]

×

Get Our Newsletter
Be the first to get the latest updates and tutorials.

Thankyou for Subscribing Us!

×

Webinar: How to Get 100% Scores on Core Web Vitals

Join Joe Williams & Aleksandar Savkovic on 29th of March, 2021.

Do you like what you read?

Get the Latest Updates

Share Your Feedback

Please insert Content

Thank you for your feedback!

Do you like what you read?

Get the Latest Updates

Share Your Feedback

Please insert Content

Thank you for your feedback!

Want to Experience the Cloudways Platform in Its Full Glory?

Take a FREE guided tour of Cloudways and see for yourself how easily you can manage your server & apps on the leading cloud-hosting platform.

Start my tour

CYBER WEEK SAVINGS

  • 0

    Days

  • 0

    Hours

  • 0

    Mints

  • 0

    Sec

GET OFFER

For 4 Months &
40 Free Migrations

For 4 Months &
40 Free Migrations

Upgrade Now