Skip to content

Add single product to cart

What I love about offering Sunshine Photo Cart as a WordPress client gallery plugin is it can easily be extended to work exactly as each photographer needs. For photographers who only offer a single product with no options (often a Digital Download), this small code snippet will allow your customers to more quickly add to cart. Instead of opening the default modal where customers select which product for the image to add to cart, this will get the first product, add it to cart, show a success window, then automatically close it.

From Gallery View

This works with the default add to cart button and the one in the Lightbox!

add_action( 'sunshine_modal_display_add_to_cart', 'sunshine_modal_display_add_to_cart_direct', 1 );
function sunshine_modal_display_add_to_cart_direct() {
	if ( ! empty( $_POST['imageId'] ) ) {
		$image = sunshine_get_image( intval( $_POST['imageId'] ) );
		if ( $image->exists() ) {
			$products = sunshine_get_products( $image->get_price_level() );
			if ( ! empty( $products ) ) {
				$product = $products[0];
				$add_to_cart_result = SPC()->cart->add_item( $product->get_id(), $image->get_id(), $image->get_gallery_id(), $image->get_price_level(), '', 1 );
				if ( $add_to_cart_result ) {
					$result = array(
						'html' => '<div class="sunshine--success"></div><script>setTimeout( function() { sunshine_close_modal(); }, 1000 );</script>'
					);
					wp_send_json_success( $result );
				}
			}
		}
	}
	return false;
}

Learn how to add this custom code to your WordPress website

For General Products

Sunshine Photo Cart also allows you to sell any type of products using the Sell Anything add-on. This code goes with these General type products:

add_action( 'sunshine_modal_display_general_product', 'sunshine_modal_display_general_product_direct', 1 );
function sunshine_modal_display_general_product_direct() {
	if ( ! empty( $_POST['productId'] ) ) {
		$product = sunshine_get_product( intval( $_POST['productId'] ) );
		if ( $product->exists() ) {
			$price_level        = intval( $_POST['priceLevel'] );
			$add_to_cart_result = SPC()->cart->add_item( $product->get_id(), '', '', $price_level, '', 1 );
			if ( $add_to_cart_result ) {
				$result = array(
					'html' => '<div class="sunshine--success"></div><script>setTimeout( function() { sunshine_close_modal(); }, 1000 );</script>',
				);
				wp_send_json_success( $result );
			}
		}
	}
	return false;
}

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

Sunshine Photo Cart for WordPress