从Woocommerce中的购物车商品数据中通过Ajax获取重命名的购物车商品名称

问题描述 投票:2回答:1

我正在尝试检索(通过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();给我我设置的自定义名称,但它只返回原始产品名称。

有没有人对我在这里做错了什么有任何想法?

php jquery ajax woocommerce cart
1个回答
0
投票

你不能通过这种方式从购物车项目获得新的购物车项目名称,你将需要使用一些技巧使用WC_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;
}

代码位于活动子主题(或活动主题)的function.php文件中。经过测试和工作。

© www.soinside.com 2019 - 2024. All rights reserved.