Payment Methods Per Gallery
There are times when certain galleries should only accept specific forms of payment. For example, a school photo gallery might only allow checks, while a wedding gallery may only allow credit cards. This snippet gives you control over that by adding settings to each gallery. You can pick which payment methods are available, and when a customer adds images from that gallery to their cart, only the selected methods will be shown at checkout.
// Add payment methods per gallery meta field.
add_filter( 'sunshine_admin_meta_sunshine-gallery', 'sunshine_payment_methods_per_gallery_meta_fields', 999 );
function sunshine_payment_methods_per_gallery_meta_fields( $options ) {
$payment_methods = sunshine_get_payment_methods();
$payment_methods_options = array();
foreach ( $payment_methods as $id => $payment_method ) {
$payment_methods_options[ $id ] = $payment_method->get_name();
}
$options['sunshine-gallery-options'][1000]['fields'][] = array(
'id' => 'payment_methods',
'name' => __( 'Allowed Payment Methods', 'sunshine-photo-cart' ),
'type' => 'checkbox_multi',
'options' => $payment_methods_options,
);
return $options;
}
/* Filter payment methods based on gallery allowed payment methods. */
add_filter( 'sunshine_allowed_payment_methods', 'sunshine_payment_methods_per_gallery' );
function sunshine_payment_methods_per_gallery( $payment_methods ) {
$cart_items = SPC()->cart->get_cart_items();
if ( empty( $cart_items ) ) {
return $payment_methods;
}
foreach ( $cart_items as $item ) {
$gallery = $item->get_gallery();
if ( empty( $gallery ) ) {
continue;
}
$allowed_payment_methods = $gallery->get_meta_value( 'payment_methods', true );
if ( empty( $allowed_payment_methods ) ) {
continue;
}
foreach ( $payment_methods as $id => $payment_method ) {
if ( ! in_array( $id, $allowed_payment_methods ) ) {
unset( $payment_methods[ $id ] );
}
}
}
return $payment_methods;
}
Learn how to add this custom code to your WordPress website
Still need help?
If you have not yet found your answer in the documentation articles, please contact support