Skip to content

Instantly share code, notes, and snippets.

@Acephalia
Last active January 9, 2024 23:42
Show Gist options
  • Save Acephalia/42cfa6243df3cd0cd529b3987b6ba108 to your computer and use it in GitHub Desktop.
Save Acephalia/42cfa6243df3cd0cd529b3987b6ba108 to your computer and use it in GitHub Desktop.
Shortcode To Add Multiple Products To Cart
//Start Shop The Look Shortcode by u/acephaliax
function shop_the_look_button_shortcode($atts) {
// Shortcode attributes
$atts = shortcode_atts(
array(
'ids' => '', // Product IDs comma-separated
),
$atts,
'shop_the_look'
);
// Convert comma-separated product IDs to an array
$product_ids = explode(',', $atts['ids']);
// Build dynamic add to cart URL
$add_to_cart_url = home_url('/?add-more-to-cart='); // Updated URL format
foreach ($product_ids as $product_id) {
$add_to_cart_url .= absint($product_id) . ','; // Append each product ID
}
// Remove the trailing comma
$add_to_cart_url = rtrim($add_to_cart_url, ',');
// Button HTML with the generated URL
$button_html = '<a href="' . esc_url($add_to_cart_url) . '" class="shop-the-look-button shopthelook">Shop the Look</a>';
return $button_html;
}
// Register the shortcode
add_shortcode('shop_the_look', 'shop_the_look_button_shortcode');
//Start Add Multiple Products To Cart URL by https://stackoverflow.com/users/3902411/nate
class add_more_to_cart {
private $prevent_redirect = false; //used to prevent WC from redirecting if we have more to process
function __construct() {
if ( ! isset( $_REQUEST[ 'add-more-to-cart' ] ) ) return; //don't load if we don't have to
$this->prevent_redirect = 'no'; //prevent WC from redirecting so we can process additional items
add_action( 'wp_loaded', [ $this, 'add_more_to_cart' ], 21 ); //fire after WC does, so we just process extra ones
add_action( 'pre_option_woocommerce_cart_redirect_after_add', [ $this, 'intercept_option' ], 9000 ); //intercept the WC option to force no redirect
}
function intercept_option() {
return $this->prevent_redirect;
}
function add_more_to_cart() {
$product_ids = explode( ',', $_REQUEST['add-more-to-cart'] );
$count = count( $product_ids );
$number = 0;
foreach ( $product_ids as $product_id ) {
if ( ++$number === $count ) $this->prevent_redirect = false; //this is the last one, so let WC redirect if it wants to.
$_REQUEST['add-to-cart'] = $product_id; //set the next product id
WC_Form_Handler::add_to_cart_action(); //let WC run its own code
}
}
}
new add_more_to_cart;
/* Add/modify button style as needed. Add css to Theme Customiser > Additional CSS */
.shopthelook {
background-color: #000;
padding:10px;
color: #ffffff;
}
  • Add php snippet to child theme functions php or via code snippets plugin.
  • Add CSS to Theme Customiser > Additional CSS
  • Use shortcode : [shop_the_look ids="828,827"] to add a button that adds the products specified. Change product ID's to match your products.
  • To use in template file: echo do_shortcode('[shop_the_look ids="828,827"]');
@Acephalia
Copy link
Author

Utilised snippet for adding multiple products via add to cart url by Nate. https://stackoverflow.com/a/54622735/11055908

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment