In Woocommerce checkout part, I’m attempting so as to add a checkbox that provides an extra subscription product. It’s working wonderful and I took assist from right here
Yet another requirement is that if there are different merchandise within the cart then I wish to change every product costs based mostly on customized fields and new costs ought to show within the checkout.
Thanks prematurely.
I used this code and dealing wonderful however product worth shouldn’t be altering in PayPal fee web page.
// Show a customized checkout area
add_action( 'woocommerce_checkout_before_terms_and_conditions', 'custom_checkbox_checkout_field' );
perform custom_checkbox_checkout_field() {
$worth = WC()->session->get('add_a_product');
woocommerce_form_field( 'cb_add_product', array(
'kind' => 'checkbox',
'label' => ' ' . __('Please examine right here to get VIP Membership'),
'class' => array('form-row-wide'),
), $worth == 'sure' ? true : false );
}
// The jQuery Ajax request
add_action( 'wp_footer', 'checkout_custom_jquery_script' );
perform checkout_custom_jquery_script() {
// Solely checkout web page
if( is_checkout() && ! is_wc_endpoint_url() ):
// Take away "ship_different" customized 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 kind="textual content/javascript">
jQuery( perform($){
if (typeof wc_checkout_params === 'undefined')
return false;
$('type.checkout').on( 'change', '#cb_add_product', perform(){
var worth = $(this).prop('checked') === true ? 'sure' : 'no';
$.ajax({
kind: 'POST',
url: wc_checkout_params.ajax_url,
knowledge: {
'motion': 'add_a_product',
'add_a_product': worth,
},
success: perform (outcome) {
$('physique').set off('update_checkout');
//console.log(outcome);
}
});
});
});
</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' );
perform 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 take away free product
add_action( 'woocommerce_before_calculate_totals', 'adding_removing_specific_product' );
perform adding_removing_specific_product( $cart ) {
if (is_admin() && !outlined('DOING_AJAX'))
return;
if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 )
return;
// HERE the particular Product ID
$product_id = 179;
if( WC()->session->get('add_a_product') == 'sure' && ! 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 substitute by yours)
$new_price = get_post_meta( $product_id, 'pro_price_extra_info', true );
// Up to date cart merchandise worth
$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');
}
}