Skip to content

Instantly share code, notes, and snippets.

@mikejolley
Last active September 13, 2021 20:45
Show Gist options
  • Save mikejolley/cd67b0baf1c3767bb29f76471fea120b to your computer and use it in GitHub Desktop.
Save mikejolley/cd67b0baf1c3767bb29f76471fea120b to your computer and use it in GitHub Desktop.

Revisions

  1. mikejolley revised this gist Jun 1, 2016. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion gistfile1.txt
    Original file line number Diff line number Diff line change
    @@ -3,7 +3,7 @@
    /**
    * Code goes in theme functions.php.
    * Product must have upsells, and this works with simple products only.
    * Example link: yoursite.com/add-upsells-to-cart=X, X being your product ID.
    * Example link: yoursite.com?add-upsells-to-cart=X, X being your product ID.
    */
    add_action( 'wp_loaded', 'bulk_upsell_add_to_cart_action', 20 );

  2. mikejolley created this gist May 31, 2016.
    31 changes: 31 additions & 0 deletions gistfile1.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,31 @@
    <?php // Do not include this if already open!

    /**
    * Code goes in theme functions.php.
    * Product must have upsells, and this works with simple products only.
    * Example link: yoursite.com/add-upsells-to-cart=X, X being your product ID.
    */
    add_action( 'wp_loaded', 'bulk_upsell_add_to_cart_action', 20 );

    function bulk_upsell_add_to_cart_action() {
    if ( ! empty( $_GET['add-upsells-to-cart'] ) ) {
    $product_id = absint( $_GET['add-upsells-to-cart'] );
    $product = wc_get_product( $product_id );

    if ( $product ) {
    $upsell_ids = $product->get_upsells();

    if ( $upsell_ids ) {
    $count = 0;

    foreach ( $upsell_ids as $upsell_id ) {
    if ( WC()->cart->add_to_cart( $upsell_id ) ) {
    $count ++;
    }
    }

    wc_add_notice( sprintf( _n( 'Added %d item to the cart', 'Added %d items to the cart', $count ), $count ) );
    }
    }
    }
    }