Php 通过Ajax从Woocommerce中的购物车项目数据获取重命名的购物车项目名称

Php 通过Ajax从Woocommerce中的购物车项目数据获取重命名的购物车项目名称,php,jquery,ajax,woocommerce,cart,Php,Jquery,Ajax,Woocommerce,Cart,我正在尝试(通过Ajax)检索我在产品上设置的自定义名称,但到目前为止我无法检索它 这是我设置自定义名称的代码 add_action( 'woocommerce_before_calculate_totals', 'add_custom_price', 10, 1); function add_custom_price( $cart_obj ) { // This is necessary for WC 3.0+ if ( is_admin() && ! def

我正在尝试(通过Ajax)检索我在产品上设置的自定义名称,但到目前为止我无法检索它

这是我设置自定义名称的代码

add_action( 'woocommerce_before_calculate_totals', 'add_custom_price', 10, 1);
function add_custom_price( $cart_obj ) {

    // This is necessary for WC 3.0+
    if ( is_admin() && ! defined( 'DOING_AJAX' ) )
        return;

    // Avoiding hook repetition (when using price calculations for example)
    if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 )
        return;

    // Loop through cart items
    foreach ( $cart_obj->get_cart() as $cart_item ) {
        $cart_item['data']->set_name( 'My Test Name' );
        $cart_item['data']->set_price( 40 );
    }
}
在我的Javascript中,我在更新签出时进行Ajax调用:

$( document.body ).on( 'updated_checkout', function() {...
从那里我调用这个PHP函数

add_action( 'wp_ajax_retrieve_custom_product_name', 'retrieve_custom_product_name' );
function retrieve_custom_product_name() {

    //  This is necessary for WC 3.0+
    if ( is_admin() && ! defined( 'DOING_AJAX' ) ) 
        return;

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

    $items = WC()->cart->get_cart();

    foreach($items as $item => $cart_item) {
        $item_name = $cart_item['data']->get_name();
    }
    echo $item_name;
    wp_die();
}
我需要$cart_item['data']->get_name();给我设置的自定义名称,但它只返回原始产品名称


有人知道我做错了什么吗?

你不能通过Ajax从购物车项目中获取新的购物车项目名称,你需要使用
WC\u Sessions
的一些技巧来实现

注意您可以有许多购物车项目,并且您只能返回一个具有实际逻辑的新购物车项目名称

守则:

// Change cart item name and price
add_action( 'woocommerce_before_calculate_totals', 'change_cart_item_name_and_price', 10, 1 );
function change_cart_item_name_and_price( $cart ) {
    if ( is_admin() && ! defined( 'DOING_AJAX' ) )
        return;

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

    // Get new items names from WC_Session
    $session_data = (array) WC()->session->get( 'new_item_names' );

    // Loop through cart items
    foreach ( $cart->get_cart() as $cart_item_key => $cart_item ) {
        $new_item_name = 'My Test Name';

        $cart_item['data']->set_name( 'My Test Name' );
        $cart_item['data']->set_price( 40 );

        // If item name doesn't exist in WC_Session for this cart item, we do it
        if( ! isset($session_data[$cart_item_key]) ) {
            $session_data[$cart_item_key] = $new_item_name;
            WC()->session->set( 'new_item_names', $session_data );
        }
    }
}

// PHP WP Ajax
add_action( 'wp_ajax_get_cart_item_name', 'retrieve_custom_product_name' );
add_action( 'wp_ajax_nopriv_get_cart_item_name', 'retrieve_custom_product_name' );
function retrieve_custom_product_name() {
    $session_data = (array) WC()->session->get( 'new_item_names' );

    // Loop through cart items
    foreach( WC()->session->get('cart') as $cart_item_key => $cart_item ) {
        // Get the new item name from WC_Session
        if( isset($session_data[$cart_item_key]) ) {
            $item_name = $session_data[$cart_item_key];
            break; // We stop the loop to get one item name only (the first one)
        }
    }
    if( isset($item_name) ) {
        echo $item_name;
    }
    die();
}

// Jquery Ajax
add_action('wp_footer', 'custom_ajax_checkout_script');
function custom_ajax_checkout_script( $checkout ) {
    if ( is_checkout() && ! is_wc_endpoint_url() ) :
    ?>
    <script type="text/javascript">
    jQuery( function($){
        if (typeof wc_checkout_params === 'undefined')
            return false;

        // update cart on delivery location checkbox option
        $(document.body).on( 'updated_checkout', function() {
            $.ajax({
                type: 'POST',
                url: wc_checkout_params.ajax_url,
                data: {
                    'action': 'get_cart_item_name',
                },
                success: function (result) {
                    console.log('response: '+result); // just for testing
                },
                error: function(error){
                    console.log(error); // just for testing
                }
            });
        });
    });
    </script>
    <?php
    endif;
}
//更改购物车项目名称和价格
添加操作('woocommerce'在计算总额之前,'change'购物车\商品\名称\价格',10,1);
功能更改\购物车\商品\名称\价格($cart){
if(定义了('DOING'uajax'))
返回;
如果(did_action('woocommerce_before_calculate_totals')>=2)
返回;
//从WC_会话获取新项目名称
$session_data=(array)WC()->session->get('new_item_names');
//循环浏览购物车项目
foreach($cart->get\u cart()作为$cart\u item\u key=>$cart\u item){
$new_item_name='My Test name';
$cart_item['data']->set_name('My Test name');
$cart_item['data']->set_price(40);
//如果此购物车项目的WC_会话中不存在项目名称,我们将执行此操作
如果(!isset($session\u data[$cart\u item\u key])){
$session\u data[$cart\u item\u key]=$new\u item\u name;
WC()->session->set('new\u item\u names',$session\u data);
}
}
}
//PHP-WP-Ajax
添加操作('wp\u ajax\u get\u cart\u item\u name','retrieve\u custom\u product\u name');
添加操作('wp\u ajax\u nopriv\u get\u cart\u item\u name','retrieve\u custom\u product\u name');
函数检索\自定义\产品\名称(){
$session_data=(array)WC()->session->get('new_item_names');
//循环浏览购物车项目
foreach(WC()->session->get('cart')as$cart\u item\u key=>$cart\u item){
//从WC_会话获取新项目名称
if(isset($session\u data[$cart\u item\u key])){
$item_name=$session_data[$cart_item_key];
break;//我们停止循环只获取一个项目名称(第一个)
}
}
if(isset(项目名称)){
echo$item_name;
}
模具();
}
//Jquery Ajax
添加操作(“wp\u页脚”、“自定义\u ajax\u签出\u脚本”);
函数自定义\u ajax\u签出\u脚本($checkout){
如果(is_checkout()&&!is_wc_endpoint_url()):
?>
jQuery(函数($){
if(wc\u checkout\u参数的类型==“未定义”)
返回false;
//在交货地点更新购物车复选框选项
$(document.body).on('updated_checkout',function(){
$.ajax({
键入:“POST”,
url:wc_checkout_params.ajax_url,
数据:{
'action':'get_cart_item_name',
},
成功:功能(结果){
console.log('response:'+result);//仅用于测试
},
错误:函数(错误){
console.log(错误);//仅用于测试
}
});
});
});