WPITECH
WooCommerce

How to Setup WooCommerce Deposits for Store Product

Setup-WooCommerce-Deposits

WooCommerce deposits are the most common payment options in an online stores that deal with high-worth items.

In many cases, online store owners charge deposit to cover the expenses and to make sure that the customer continued interest in the transaction.

WooCommerce deposits not considered as the standard payment option. It is the ecommerce payment option that introduced by different section of merchants.

The basic idea is to include a custom deposit field that displays the amount of deposit and later on deduct the amount in the final cart checkout step.

How to Add Custom WooCommerce Deposit Field in the Product Tab

This code adds a custom field in the Product tab. Now store owners have choice to set their deposit amount for products in store.

function add_custom_fields_WooCommerce_deposit() {
     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 for WooCommerce deposit', 'woocommerce' )
         )
     );
  }
add_action( 'woocommerce_product_options_general_product_data', 'add_custom_fields_WooCommerce_deposit');

To look for the fields that added here for WooCommerce deposit, go to the Products > All Products > Product Data > Simple Product.

Add Custom WooCommerce Deposit Field in the Product Tab

In the above code, woocommerce_wp_text_input() pass an array as an argument. It also allows you to pass multiple arguments to the hook.

The array element “id” associate the field value and the woocommerce_product_options_general_product_data() is the WordPress hook that executes the action.

Now, the other step is to save the value of the custom fields of the product. Add the following code snippet:

function custom_fields_save_WooCommerce_deposit( $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_WooCommerce_deposit' );

Save value WooCommerce deposit custom field

In this code snippet, the id ‘_deposit_price’ function as saving the deposit price and the update_post_meta() updates the value of the custom WooCommerce deposit field that is the meta key for the post.

How to Return Deposit Amount in WooCommerce

As the deposit is now completely set, it is necessary that the product price should reflect this element. For this add the following code that checks if the product has a deposit price set or not. If this is the scenario, the deposit price will returned instead of the actual price. If the deposit price is not set then it returns zero.

function filter_woocommerce_deposit_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_deposit_get_price', 10, 2 );

This is how the product page looks if the WooCommerce deposit is not set it returns zero.

WooCommerce deposit get price

In the above code, $deposit_price get the filed using id_deposit_price and get_post_meta. For this whole process, get_post_meta uses a meta key to retain the information.

How to Set the Prices for Product & Checkout Pages

I have considered the following prices as an example.

Full Price: £5.00

Deposit: £1 .00

These are the prices that are going to be displayed on product pages and checkout page. Add the following code to implement this step.

function filter_woocommerce_get_price_deposit( $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_deposit', 'filter_woocommerce_get_price_deposit', 10, 2 );
add_filter( 'woocommerce_cart_product_price', 'filter_woocommerce_get_price_deposit', 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 );

WooCommerce-deposit

Related posts

Show Category in WooCommerce Product Loop

WPITECH

How to Display Category Wise Product in WooCommerce

WPITECH

How To Hide or Disable Add To Cart Button In WooCommerce Store

WPITECH