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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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.