【问题标题】:Get product properties on "added_to_cart" JS events on WooCommerce single product page在 WooCommerce 单一产品页面上获取“added_to_cart”JS 事件的产品属性
【发布时间】:2021-01-15 19:52:32
【问题描述】:

我正在学习如何使用 ' added_to_cart' javascript 事件,以便我可以通过 javascript 警报(例如 SweetAlert2)显示添加的产品信息。

我在我的 WooCommerce 测试网站中使用 Get specific WooCommerce product data on "added_to_cart" javascript event 答案代码(刚刚从 jQuery 中删除了 bianoTrack() 函数),设置了 3 个产品(即 2 个简单和 1 个变体)。

在我的 Chrome 开发者 JS 控制台中,我得到了以下信息:

我没有看到任何产品信息。

有谁知道为什么“added_to_cart”javascript 事件无法获取产品信息?

我已为“添加到购物车”按钮启用 AJAX。

【问题讨论】:

  • 嗨@LoicTheAztec,谢谢你的回复。我一直在查看您在 WooCommerce 上的帖子,感觉很棒。我想知道我的设置中是否有一些我可能遗漏的东西。这是我的设置: - WordPress 5.5.1 - WooCommerce 4.5.2 有什么我应该检查的吗?谢谢。
  • 抱歉,jQuery 代码在上一个 WooCommerce 版本中运行良好,请参阅 this fresh screenshot ...您的情况还有其他问题,我猜不出是什么,抱歉。
  • 感谢@LoicTheAztec,我将尝试另一种设置,看看是什么问题。感谢您的帮助!谢谢。
  • 有没有办法让它在单一产品页面上运行?
  • 与普通添加到购物车(在单个产品页面上)一样,当您将产品添加到购物车时页面会重新加载,您不能使用“added_to_cart”事件,因为它仅用于 Ajax 添加到购物车.

标签: php jquery ajax wordpress woocommerce


【解决方案1】:

这不适用于单个产品页面正常添加到购物车。它用于 Ajax 在产品循环中添加到购物车,例如商店页面、存档页面、相关产品……

请注意,通过 Ajax 添加到购物车的产品不会重新加载页面。

该代码在上一个 WooCommerce 版本上仍然可以完美运行:

这是一个“甜蜜警报 2”示例,在 ajax 添加到购物车上显示产品名称:

// Add some product data to "add to cart" button for 'added_to_cart' js event
add_action( 'woocommerce_loop_add_to_cart_link', 'filter_wc_loop_add_to_cart_link', 10, 3 );
function filter_wc_loop_add_to_cart_link( $button_html, $product, $args ) {
    if( $product->supports( 'ajax_add_to_cart' ) ) {
        $search_string  = 'data-product_sku';

        // Insert custom product data as data tags
        $replace_string = sprintf(
            'data-product_name="%s" data-product_price="%s" data-currency="%s" %s',
            $product->get_name(), // product name
            wc_get_price_to_display( $product ), // Displayed price
            get_woocommerce_currency(), // currency
            $search_string
        );

        $button_html = str_replace($search_string, $replace_string, $button_html);
    }
    return $button_html;
}

// The jQuery code that will handle the event getting the required  product data
add_action( 'wp_footer', 'added_to_cart_js_event' );
function added_to_cart_js_event(){
    ?>
    <script src="https://unpkg.com/sweetalert2@7.20.1/dist/sweetalert2.all.js"></script>
    <script type="text/javascript">
    (function($){
        $(document.body).on('added_to_cart', function( event, fragments, cart_hash, button ) {
            var product_id    = button.data('product_id'),   // Get the product id
                product_qty   = button.data('quantity'),     // Get the quantity
                product_sku   = button.data('product_sku'),  // Get the product sku
                product_name  = button.data('product_name'), // Get the product name
                product_price = button.data('product_price'), // Get the product price
                currency      = button.data('currency');     // Get the currency

            // For testing: View all product available data on console log (to be removed)
            console.log( button.data() );

            const toast = swal.mixin({
                toast: true,
                showConfirmButton: false,
                timer: 10000
            });
            toast({
                type: 'success',
                title: 'Product "'+product_name+'" added to Cart'
            });
        });
    })(jQuery);
    </script>
    <?php
}

代码在您的活动子主题(或活动主题)的functions.php 文件中。经过测试并且可以工作。

【讨论】:

  • 哦,是的,我刚刚在我的产品目录页面上进行了测试,它可以工作!
  • 是的,我已接受您的回答,并澄清它在产品目录中有效:)
  • 如果您能在我的主题中支持 AJAX 的单一产品页面中分享它是如何工作的,那就太好了。
  • @chubby 单个产品页面中没有用于正常添加到购物车按钮的 Ajax,因为此添加到购物车使用 $_POST 请求...所以一旦页面重新加载,您可以通过 $_POST 获取产品信息或重定向。
  • 感谢@LoicTheAztec,将对其进行进一步测试。
猜你喜欢
  • 1970-01-01
  • 2018-09-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-07-18
  • 2017-02-21
  • 2021-05-31
相关资源
最近更新 更多