• Resolved jayjay10110

    (@jayjay10110)


    Hi i’m trying to add a bundle to a cart but I can’t figure out what is the right config or hooks that I will use.

    I’m using gravity form multi step and woocommerce product bundle in which users can build a bundle step by step.

    function pre_submission_handler( $form ) {
        
    global $woocommerce;
    $bundle_id_post = $_POST['input_2']; // bundle id
    $bundleproducts = $_POST['input_13']; //products on the bundle
        
    if (isset($bundle_id_post)) {
        $bundle_id = wc_get_product($bundle_id_post);
            if ($bundle_id->is_type('bundle')) {
         $bpContainer = array();
         if(!empty($bundleproducts)) {
         foreach ($bundleproducts as $bp => $b) {
           $bpContainer[ $bp ] = array();
           $bpContainer[ $bp ][ 'product_id' ] = $b;
           $bpContainer[ $bp ][ 'quantity' ] = 1;
           $bpContainer[$bp]['variation_id'] = '';
           $bpContainer[$bp]['attributes'] = array();
        }
    }
                
        $cart_item_data = (array) apply_filters( 'woocommerce_add_cart_item_data', $cart_item_data, $bundle_id_post, '', 1 );
       $added = WC_PB()->cart->add_bundle_to_cart( $bundle_id_post, 1, $cart_item_data );
       //$added_to_cart = WC()->cart->add_to_cart( $bundle_id, 1, 0, array(), $cart_item_data );
        print_r($added);
        // print_r($cart_item_data);
        wp_enqueue_script('wc-add-to-cart-bundle');
        }
        } 
        
    }
    
    add_action( 'gform_pre_submission', 'pre_submission_handler' );
Viewing 3 replies - 1 through 3 (of 3 total)
Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Bundle Add to cart code’ is closed to new replies.