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.
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' );
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.
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:
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.
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]