При помощи ajax передаю value в functions.php
jQuery(document).ready(function($){
$('body').on('submit', '[data-name=add_to_cart]', function(e) {
var regular = 2;
$.ajax({
url: '<?php echo admin_url('admin-ajax.php') ?>',
type: 'POST',
data: 'action=add_custom_price®ular='+regular,
dataType: 'json',
success: function( response ) {
alert( response );
}
});
});
});
в functions.php обработчик
add_action ('wp_ajax_add_custom_price', 'add_custom_price');
add_action ('wp_ajax_nopriv_add_custom_price', 'add_custom_price');
add_action ('woocommerce_before_calculate_totals', 'add_custom_price');
function add_custom_price( $cart_object ) {
global $woocommerce;
//для проверки в $_REQUEST['regular'] передается 2
$custom_price = json_decode($_REQUEST['regular']);
$prdct = wc_get_product('994');
$s = $prdct->sale_price;
$p = $prdct->regular_price;
if($custom_price == 2){
foreach ( $cart_object->cart_contents as $key => $value ) {
if($value['data']->get_id() == 994){
$value['data']->set_price($s);
}
}
}else{
foreach ( $cart_object->cart_contents as $key => $value ) {
if($value['data']->get_id() == 994){
$value['data']->set_price($p);
}
}
}
echo $custom_price;
}
и здесь я не могу понять в чем проблема, $custom_price
возвращает 2, а в set_price попадает переменная $p.
Ну а если поставить if($custom_price == 0)
то в set_price попадает $s.
Что я делаю не так?