Checkbox field that add a subscription product and change prices of other products in checkout and cart page

Question

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
, , , Pullata Praveen 3 years 2020-06-05T08:10:34-05:00 0 Answers 99 views 0

Leave an answer

Browse
Browse