Change Woocommerce price via AJAX

Can I use add_filter with wp_ajax? What I need that change price dynamically via select list placed below ‘Add to Basket’ button.

I have filter with action (easy code):

add_filter( 'woocommerce_get_price','change_price', 10, 2 );
add_action( 'wp_ajax_change_price', 'change_price' );
add_action( 'wp_ajax_nopriv_change_price', 'change_price' );

function change_price( $price, $productd ){

    $custom_price = $_POST['price']; //Custom price from select list via ajax, price is display in console via echo $custom_price; so working.

    if( $custom_price ){
     return $custom_price;
    }else{
     return $price;
    }

}

Can I use it together? everything work but functions doesn’t return custom price.

Matt

Read more here: Change Woocommerce price via AJAX

Leave a Reply

Your email address will not be published. Required fields are marked *