Woo Quick Order shortcode

Creates a shortcode `[ccom-wqo]` and inner shortccodes `[ccom-wqo-item]` to produce a bulk order form / table and accept multiple quantities added to cart at once.

wp_register_style( 'ccom-wqo', false );

add_shortcode( 'ccom-wqo', function( $atts, $text ) {

	wp_enqueue_style( 'ccom-wqo' );
	wp_add_inline_style( 'ccom-wqo', '
		form#ccom_wqo {
			display: inline;
		}
		form#ccom_wqo > div {
			display: table;
		}
		form#ccom_wqo > div > div {
			display: table-row;
		}
		form#ccom_wqo > div > div > span {
			display: table-cell;
		}
		form#ccom_wqo > div > div > span:last-child {
			text-wrap: nowrap;
		}
		form#ccom_wqo img {
			width: 2em;
		}
		form#ccom_wqo input[type=number] {
			width: 3em;
			display: inline-block;
		}
	' );

	return sprintf(
		'
			<form id="ccom_wqo" action="/cart/" method="POST">
				<div>
					%s
					<div>
						<span>&nbsp;</span>
						<span>&nbsp;</span>
						<span>
							<input type="submit" value="Add to Cart" class="button">
						</span>
					</div>
				</div>
			</form>
		',
		do_shortcode( $text )
	);

} );

add_shortcode( 'ccom-wqo-item', function( $atts ) {

	if( empty( $atts['id'] ) ) {
		return;
	}

	$product = wc_get_product( intval( $atts['id'] ) );
	if( ! $product ) {
		return;
	}

	return sprintf(
		'
			<div>
				<span>%s</span>
				<span>
					<a href="%s">%s</a>
				</span>
				<span>
					<input type="number" size="3" name="ccom-a2c[%d]" value="0">
					at %s / each
				</span>
			</div>
		',
		$product->get_image( 'thumbnail' ),
		$product->get_permalink(),
		$product->get_title(),
		$product->get_id(),
		$product->get_price_html()
	);

} );

add_action( 'template_redirect', function() {

	if( empty( $_POST['ccom-a2c'] ) || ! is_array( $_POST['ccom-a2c'] ) ) {
		return;
	}

	if( ! class_exists( 'WooCommerce' ) || empty( WC()->cart ) ) {
		return;
	}

	foreach( $_POST['ccom-a2c'] as $product_id => $quantity ) {

		if( intval( $quantity ) < 1 ) {
			continue;
		}

		WC()->cart->add_to_cart( intval( $product_id ), intval( $quantity ) );

	}

} );

Instructions for Woo Quick Order shortcode

  1. Log into a staging or locally hosted clone of your site.
  2. Install and activate Code Snippets plugin.
  3. WP Admin > Snippets > Add New
  4. Copy and paste the code from the section above.
  5. Check to ensure formatting came over properly.
  6. Customize the code as desired.
  7. Add a meaningful title.
  8. Select whether to run on front-end or back-end (or both).
  9. Click “Save and Activate”.
  10. Test your site to ensure it works.
  11. Disable if any problems, or recover.
  12. Repeat for live environment.

Need help modifying Woo Quick Order shortcode?

Contact me. I can help with fitting projects or refer to my partner.

License

All code snippets are licensed GPLv2 (or later) matching WordPress licensing.

Except when otherwise stated in writing the copyright holders and/or other parties provide the program as-is without warranty of any kind, either expressed or implied, including, but not limited to, the implied warranties of merchantability and fitness for a particular purpose.

Disclaimer of warranty