Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/75.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 获取添加到购物车中的商品id、名称和数量,以显示通知_Php_Jquery_Ajax_Wordpress_Woocommerce - Fatal编程技术网

Php 获取添加到购物车中的商品id、名称和数量,以显示通知

Php 获取添加到购物车中的商品id、名称和数量,以显示通知,php,jquery,ajax,wordpress,woocommerce,Php,Jquery,Ajax,Wordpress,Woocommerce,浏览了一大堆类似的问题,到目前为止都没有成功 我想在常规页面上显示一个WC通知,命名最后添加到购物车的项目 通知已启动并正在运行,但是,到目前为止,我无法识别添加到购物车的最后一个项目的ID 我试过这个 $items = WC()->cart->get_cart(); $ids = array(); foreach($items as $item => $values) { $_product = $values['data']->

浏览了一大堆类似的问题,到目前为止都没有成功

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

通知已启动并正在运行,但是,到目前为止,我无法识别添加到购物车的最后一个项目的ID

我试过这个

    $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参数,但它不能通过$\u GET读取


有人知道如何检索通过WC hook/jQuery添加的最后一个项目的产品ID吗?

对于Ajax
添加到购物车的委托事件。

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

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

//将产品名称作为数据参数添加到Ajax“添加到购物车”按钮
添加过滤器(“woocommerce\u loop\u add\u to\u cart\u args”、“filter\u wc\u loop\u add\u to\u cart\u args”,20,2);
函数过滤器\u wc\u循环\u添加到\u购物车\u参数($args,$product){
如果($product->supports('ajax\u add\u to\u cart')&&&$product->is\u purchable()&&&$product->is\u in\u stock()){
$args['attributes']['data-product_name']=$product->get_name();
}
返回$args;
}
//在添加到购物车的Ajax上,显示带有产品名称(和产品id)的灯箱
添加动作('wp_footer'、'ajax_添加了_到_cart_弹出式_脚本');
函数ajax\u添加到\u购物车\u弹出式\u脚本(){
?>
jQuery(函数($){
//关于“添加到购物车”现场活动
$(document.body).on('added_to_cart',函数(a、b、c、d){
var prod_id=d.data('product_id'),//获取产品名称
prod_qty=d.data('quantity'),//获取数量
prod_name=d.data('product_name');//获取产品名称
喷火({
标题:“”,
文本:prod_name+'('+prod_id+'),
showCancelButton:true,
confirmButtonColor:“#000”,
取消按钮颜色:“#3085d6”,
confirmButtonText:“”,
cancelButtonText:'
})。然后((结果)=>{
if(result.value){
window.location.href='';
}
});
});
});

@LoicTheAztec:非常感谢你的广泛建议!虽然刚刚注意到你删除了你的编辑…但是我还是为你的努力捐款。我仍然需要弄清楚如何将$product\u id转换为我的通知。我已经更改了我的答案,更关注于你正在尝试做的事情。很漂亮!通过woocommerce\u loop\u广告传递argsd_to_cart_args(一个我以前从未听说过的钩子)这是一个非常好的解决方案。谢谢你,伙计!@LoicTheAztec,我直接尝试了这段代码,弹出窗口工作正常,但我看到未定义值的原因是什么?我很想听听你的意见。@LoicTheAztec,这在商店或存档页面中有效,但如何使其在单个产品页面上工作?@ccmanz这不适用于单个产品页面,因为它只适用于单个产品页面jax添加到购物车。在单个产品添加到购物车时,页面会重新加载,因此不需要。好的,谢谢@LoicTheAztec!
// 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
}