0

In Woocommerce checkout section, I am trying to add a checkbox that adds an additional subscription product. It is working fine and I took help from here

One more requirement is that if there are other products in the cart then I want to change each product prices based on custom fields and new prices should display in the checkout. Thanks in advance.

I used this code and working fine but product price is not changing in PayPal payment page.

        // Display a custom checkout field
    add_action( 'woocommerce_checkout_before_terms_and_conditions', 'custom_checkbox_checkout_field' );
    function custom_checkbox_checkout_field() {
        $value = WC()->session->get('add_a_product');

        woocommerce_form_field( 'cb_add_product', array(
            'type'          => 'checkbox',
            'label'         => '  ' . __('Please check here to get VIP Membership'),
            'class'         => array('form-row-wide'),
        ), $value == 'yes' ? true : false );
    }


    // The jQuery Ajax request
    add_action( 'wp_footer', 'checkout_custom_jquery_script' );
    function checkout_custom_jquery_script() {
        // Only checkout page
        if( is_checkout() && ! is_wc_endpoint_url() ):

        // Remove "ship_different" custom WC session on load
        if( WC()->session->get('add_a_product') ){
            WC()->session->__unset('add_a_product');
        }
        if( WC()->session->get('product_added_key') ){
            WC()->session->__unset('product_added_key');
        }
        // jQuery Ajax code
        ?>
        <script type="text/javascript">
        jQuery( function($){
            if (typeof wc_checkout_params === 'undefined')
                return false;

            $('form.checkout').on( 'change', '#cb_add_product', function(){
                var value = $(this).prop('checked') === true ? 'yes' : 'no';

                $.ajax({
                    type: 'POST',
                    url: wc_checkout_params.ajax_url,
                    data: {
                        'action': 'add_a_product',
                        'add_a_product': value,
                    },
                    success: function (result) {
                        $('body').trigger('update_checkout');
                        //console.log(result);
                    }
                });
            });
        });
        </script>
        <?php
        endif;
    }

    // The Wordpress Ajax PHP receiver
    add_action( 'wp_ajax_add_a_product', 'checkout_ajax_add_a_product' );
    add_action( 'wp_ajax_nopriv_add_a_product', 'checkout_ajax_add_a_product' );
    function checkout_ajax_add_a_product() {
        if ( isset($_POST['add_a_product']) ){
            WC()->session->set('add_a_product', esc_attr($_POST['add_a_product']));
            echo $_POST['add_a_product'];
        }
        die();
    }

    // Add remove free product
    add_action( 'woocommerce_before_calculate_totals', 'adding_removing_specific_product' );
    function adding_removing_specific_product( $cart ) {
        if (is_admin() && !defined('DOING_AJAX'))
            return;

        if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 )
            return;

        // HERE the specific Product ID
        $product_id = 179;

        if( WC()->session->get('add_a_product') == 'yes' && ! WC()->session->get('product_added_key') )
        {

        foreach ( $cart->get_cart() as $cart_item ) {
            $cart_item_key = $cart->add_to_cart( $product_id );
            WC()->session->set('product_added_key', $cart_item_key);

             // get the product id (or the variation id)
             $product_id = $cart_item['data']->get_id();

             // GET THE NEW PRICE (code to be replace by yours)
             $new_price = get_post_meta( $product_id, 'pro_price_extra_info', true );

             // Updated cart item price
             $cart_item['data']->set_price( floatval( $new_price ) );
        }
        }
        elseif( WC()->session->get('add_a_product') == 'no' && WC()->session->get('product_added_key') )
        {
            $cart_item_key = WC()->session->get('product_added_key');
            $cart->remove_cart_item( $cart_item_key );
            WC()->session->__unset('product_added_key');
        }
    }
0

I solved the my issue. this code may help others in future.

            // Display a custom checkout field
            add_action( 'woocommerce_checkout_before_terms_and_conditions', 'custom_checkbox_checkout_field' );
            function custom_checkbox_checkout_field() {
                $value = WC()->session->get('add_a_product');

                woocommerce_form_field( 'cb_add_product', array(
                    'type'          => 'checkbox',
                    'label'         => '&nbsp;&nbsp;' . __('Please check here to get VIP Membership'),
                    'class'         => array('form-row-wide'),
                ), $value == 'yes' ? true : false );
            }


            // The jQuery Ajax request
            add_action( 'wp_footer', 'checkout_custom_jquery_script' );
            function checkout_custom_jquery_script() {
                // Only checkout page
                if( is_checkout() && ! is_wc_endpoint_url() ):

                // Remove "ship_different" custom WC session on load
                if( WC()->session->get('add_a_product') ){
                    WC()->session->__unset('add_a_product');
                }
                if( WC()->session->get('product_added_key') ){
                    WC()->session->__unset('product_added_key');
                }
                // jQuery Ajax code
                ?>
                <script type="text/javascript">
                jQuery( function($){
                    if (typeof wc_checkout_params === 'undefined')
                        return false;

                    $('form.checkout').on( 'change', '#cb_add_product', function(){
                        var value = $(this).prop('checked') === true ? 'yes' : 'no';

                        $.ajax({
                            type: 'POST',
                            url: wc_checkout_params.ajax_url,
                            data: {
                                'action': 'add_a_product',
                                'add_a_product': value,
                            },
                            success: function (result) {
                                $('body').trigger('update_checkout');
                                //console.log(result);
                            }
                        });

                        if(value == 'no'){
                            window.location.reload();
                        }
                    });
                });
                </script>
                <?php
                endif;
            }

            // The Wordpress Ajax PHP receiver
            add_action( 'wp_ajax_add_a_product', 'checkout_ajax_add_a_product' );
            add_action( 'wp_ajax_nopriv_add_a_product', 'checkout_ajax_add_a_product' );
            function checkout_ajax_add_a_product() {
                if ( isset($_POST['add_a_product']) ){
                    WC()->session->set('add_a_product', esc_attr($_POST['add_a_product']));
                    echo $_POST['add_a_product'];
                }
                die();
            }

            // Add remove free product
            add_action( 'woocommerce_before_calculate_totals', 'adding_removing_specific_product' );
            function adding_removing_specific_product( $cart ) {
                if (is_admin() && !defined('DOING_AJAX'))
                    return;

                if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 )
                    return;

                $has_sub = wcs_user_has_subscription( '', '179', 'active' );

                $product_id = 179;

                if( WC()->session->get('add_a_product') == 'yes' && ! WC()->session->get('product_added_key') ){
                    // HERE the specific Product ID


                    foreach ( $cart->get_cart() as $cart_item ) {       

                         // get the product id (or the variation id)
                         $product_idnew = $cart_item['data']->get_id();
                         // GET THE NEW PRICE (code to be replace by yours)
                         $new_price = get_post_meta( $product_idnew, 'pro_price_extra_info', true );
                         $old_price = $cart_item['data']->get_price();

                         if($new_price == NULL){
                            $cart_item['data']->set_price( floatval( $old_price ) );
                         } else {
                            $cart_item['data']->set_price( floatval( $new_price ) );
                         }

                    }

                    $cart_item_key = $cart->add_to_cart( $product_id );
                    WC()->session->set('product_added_key', $cart_item_key);


                } elseif( WC()->session->get('add_a_product') == 'no' && WC()->session->get('product_added_key') ) {

                    $cart_item_key = WC()->session->get('product_added_key');
                    $cart->remove_cart_item( $cart_item_key );      
                    WC()->session->__unset('product_added_key');        

                }elseif ( $has_sub ) {
                    foreach ( $cart->get_cart() as $cart_item ) {       

                         // get the product id (or the variation id)
                         $product_idnew = $cart_item['data']->get_id();
                         // GET THE NEW PRICE (code to be replace by yours)
                         $new_price = get_post_meta( $product_idnew, 'pro_price_extra_info', true );
                            $cart_item['data']->set_price( floatval( $new_price ) );

                    }
                }

            }
            function woo_in_cart($product_id) {
                global $woocommerce;         
                foreach($woocommerce->cart->get_cart() as $key => $val ) {
                    $_product = $val['data'];

                    if($product_id == $_product->id ) {
                        return true;
                    }
                }         
                return false;
            }
            add_action( 'woocommerce_cart_loaded_from_session', 'adding_vip_product' );
            function adding_vip_product( $cart ) {
                $product_id = 179;
                if(woo_in_cart($product_id) ) {

                    foreach ( $cart->get_cart() as $cart_item ) {       

                         // get the product id (or the variation id)
                         $product_idnew = $cart_item['data']->get_id();
                         // GET THE NEW PRICE (code to be replace by yours)
                         $new_price = get_post_meta( $product_idnew, 'pro_price_extra_info', true );
                         if($new_price != 0){
                            $cart_item['data']->set_price( floatval( $new_price ) );
                         }

                    }

                }
            }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.