获取已添加到购物车中的WooCommerce Ajax上的产品ID,名称和数量,以显示通知 [英] Get product id, name and quantity on WooCommerce Ajax added to cart to display a notice

查看:52
本文介绍了获取已添加到购物车中的WooCommerce Ajax上的产品ID,名称和数量,以显示通知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

浏览了很多类似的问题,但到目前为止没有成功.

Browsed a load of similar questions with no success so far.

我想显示一个WC通知,在常规页面上命名添加到购物车中的最后一个项目.

I want to display a WC notice naming the last item added to the cart on a regular page.

通知已启动并正在运行,但是到目前为止,我还不能识别添加到购物车中的最后一个商品的ID .

Notification is up and running, however, so far I was not able to identify the ID of last item added to the cart.

我已经尝试过了

    $items = WC()->cart->get_cart();
    $ids = array();
    foreach($items as $item => $values) {
        $_product = $values['data']->post;
        $ids[] = $_product->ID;
    }
    $last_product_id = end($ids);
    $added_product = wc_get_product( $last_product_id );
    $added_product_name = $added_product->get_title();

但是,据我了解,在AJAX调用期间,购物车内容不会更新.获取产品ID的最简单方法应该是包含它的AJAX参数,但是无法通过$ _GET读取.

But as I've learned cart content does not get updated during AJAX calls. The easiest way to obtain the product ID should be the AJAX parameter containing it, but it cannot be read via $_GET.

有人知道通过WC hook/jQuery检索最后添加的商品的产品ID的方法吗?

Does anyone know of a way to retrieve the product ID of the last item added via WC hook/jQuery?

推荐答案

用于Ajax added_to_cart委托事件.

For Ajax added_to_cart delegated event.

使用jQuery,您可以轻松获得产品ID 产品名称以及已通过Ajax添加到购物车中的产品数量.

Using jQuery you can get easily the product ID, the product name, and the quantity of a product that has been added to cart with Ajax.

在此代码示例中,使用Sweet Alert组件(SWAL 2),将产品添加到购物车后,我们将显示一个消息灯箱,其中包含产品名称(及其ID):

Here in this code example using Sweet Alert component (SWAL 2), when a product is added to cart, we display a message lightbox, with the product name (and its ID):

// Add the product name as data argument to Ajax add to cart buttons
add_filter( "woocommerce_loop_add_to_cart_args", "filter_wc_loop_add_to_cart_args", 20, 2 );
function filter_wc_loop_add_to_cart_args( $args, $product ) {
    if ( $product->supports( 'ajax_add_to_cart' ) && $product->is_purchasable() && $product->is_in_stock() ) {
        $args['attributes']['data-product_name'] = $product->get_name();
    }
    return $args;
}

// On Ajax added to cart, shows a lightbox with the product name (and the product id)
add_action( 'wp_footer', 'ajax_added_to_cart_popup_script' );
function ajax_added_to_cart_popup_script() {
    ?>
    <script src="https://cdn.jsdelivr.net/npm/sweetalert2@9"></script>
    <script type="text/javascript">
    jQuery( function($){
        // On "added_to_cart" live event
        $(document.body).on('added_to_cart', function( a, b, c, d ) {
            var prod_id   = d.data('product_id'), // Get the product name
                prod_qty  = d.data('quantity'), // Get the quantity
                prod_name = d.data('product_name'); // Get the product name

            Swal.fire({
                title: '<?php _e("Added to cart!"); ?>',
                text: prod_name+' ('+prod_id+')',
                showCancelButton: true,
                confirmButtonColor: '#000',
                cancelButtonColor: '#3085d6',
                confirmButtonText: '<?php _e("View-cart"); ?>',
                cancelButtonText:  '<?php _e("Continue shopping"); ?>'
            }).then((result) => {
                if (result.value) {
                    window.location.href = '<?php echo wc_get_cart_url(); ?>';
                }
            });
        });
    });
    </script>
    <?php
}

代码进入您的活动子主题(或活动主题)的function.php文件中.经过测试,可以正常工作.

Code goes in function.php file of your active child theme (or active theme). Tested and works.

相关:

  • Display a sweet alert on ajax add to cart for a specific Woocommerce cart item count
  • Woocommerce: custom jquery event after added to cart

这篇关于获取已添加到购物车中的WooCommerce Ajax上的产品ID,名称和数量,以显示通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆