1. Home
  2. Knowledge Base
  3. WooCommerce Deposits and Part Payments
  4. Advanced Topics for Deposits and Part Payments
  5. Programmatically set whether a product should have a deposit option
  1. Home
  2. Knowledge Base
  3. WooCommerce Deposits and Part Payments
  4. Programmatically set whether a product should have a deposit option

Programmatically set whether a product should have a deposit option

There might be an occasion where you want to have more flexibility over which products have a deposit.

There are two code snippets below. The first shows how to set a deposit on all products in a list.

The second product shows how to set a deposit on all products in a certain category.

In both cases, you’ll need to update the snippet slightly for your own use case – either change the list of product IDs in the first snippet, or change the category slug in the second snippet.

<?php
/**
* Programmatically set whether a product should have a deposit option
* Set the global value for 'Accept Deposits' to 'Some Products'
*/
function prefix_product_has_deposit( $has_deposit, $product_id ) {
// All the products in this list will not have a deposit option
if( in_array( $product_id, array( 123, 456, 789 ) ) {
$has_deposit = false;
} else {
$has_deposit = true;
}
return $has_deposit;
}
add_filter( 'wcdpp_product_has_deposit', 'prefix_product_has_deposit', 10, 2 );
/**
* Programmatically set whether a product should have a deposit option
* Set the global value for 'Accept Deposits' to 'Some Products'
*/
function prefix_product_has_deposit_by_category( $has_deposit, $product_id ) {
// All the products in this list will not have a deposit option
if( has_term( array( 'fruit' ), 'product_cat', $product_id ) ) {
$has_deposit = false;
} else {
$has_deposit = true;
}
return $has_deposit;
}
add_filter( 'wcdpp_product_has_deposit', 'prefix_product_has_deposit_by_category', 10, 2 );

Here’s how to add a snippet.

Was this article helpful?

Related Articles