Im trying to update the mini-cart (cart_fragments) after/on an ajax event. It works like this.

我试图在/ ajax事件之后更新迷你购物车(cart_fragments)。它的工作原理如下。

HTML Trigger:

HTML触发器:

<a id="woomps-button-\'.$nr.\'" class="woomps-button" '.$ajax_url.' data-button-nr="'.$nr.'">

jQuery request:

jQuery请求:

jQuery( document ).on('click', '.woomps-button', function() {
    var nr= jQuery(this).data('button-nr');
    jQuery.ajax({
        url : subpost.ajax_url,
        type : 'post',
        data : {
            action : 'woomps_update_nr',
            security : subpost.security,
            nr: nr
        },
        success : function( response ) {
            window.alert(nr);
            //MAYBE_code to refresh_fragments here
            }
    });
    return false;
});

PHP responder:

PHP响应者:

function woomps_update_choosen_person () {
    $p = $_POST['nr'];
    if ($p == 1) {$x= "This must show in cart";}
    WC()->session->set( 'woomps_limit_by_persons' , $x );

    //MAYBE code to refresh_fragments here
}

And in the mini-cart.php template i have a calculation based on this field.

在mini-cart.php模板中,我有一个基于此字段的计算。

$items_left_start = WC()->session->get( 'woomps_limit_by_persons' )?: 3 ;
//Do something something here

So this works, except I need to refresh the cart like when an item is added to cart. My assumption is that is should be an ajax request from jQuery that i can put in the success block?

所以这是有效的,除了我需要刷新购物车,就像物品被添加到购物车。我的假设是应该是来自jQuery的ajax请求,我可以放入成功块?

The class i (think) I want to fire is this WC_AJAX::get_refreshed_fragments(); But this is fired from an an add_action, so i tried this add_action( 'wp_ajax_nopriv_woocommerce_get_refreshed_fragments', array( 'WC_AJAX', 'get_refreshed_fragments' ) );. But it did not work either.

我想要解雇的类是这个WC_AJAX :: get_refreshed_fragments();但这是从一个add_action发出的,所以我尝试了这个add_action('wp_ajax_nopriv_woocommerce_get_refreshed_fragments',数组('WC_AJAX','get_refreshed_fragments'));.但它也没有用。

I also try to create an jQuery ajax call like it does in the add_to_cart button, but it neither worked.

我也尝试像在add_to_cart按钮中那样创建一个jQuery ajax调用,但它既不起作用。

//MAYBE_code to refresh_fragments here
        var data = {};
        jQuery.post(
            wc_get_refreshed_fragments_params.wc_ajax_url.toString().
            replace( '%%endpoint%%', 'get_refreshed_fragments' ), data,
            function( response ){

            })

        }

I do not completely understand how this works, if anyone have some pointers or a snippet i would so much appriciate it. Been struggeling with this for some time now.

我不完全理解这是如何工作的,如果有人有一些指针或片段我会非常喜欢它。已经有一段时间了。

1 个解决方案

#1


1

After much struggeling this topic on stack helped create the correct code to update mini cart fragments. It was both PHP and jQuery neeeded.

经过多次讨论,这个主题在堆栈上帮助创建了更新迷你购物车碎片的正确代码。这是PHP和jQuery都需要的。

So basically you can call the WC_AJAX::get_refreshed_fragments() at the end of your PHP coode responder; if it comes from an AJAX call. It will not return to your PHP responder code so put it at the end. The PHP respons will end/sent back to jQuery inside the WC_AJAX::get_refreshed_fragments(); so you also need to create some jQuery that responds to this. This i got from the topic:

所以基本上你可以在PHP coode响应器的末尾调用WC_AJAX :: get_refreshed_fragments();如果它来自AJAX调用。它不会返回到你的PHP响应器代码,所以把它放在最后。 PHP响应将在WC_AJAX :: get_refreshed_fragments()内结束/发送回jQuery;所以你还需要创建一些响应这个的jQuery。这是我从主题:

 var fragments = response.fragments;

 if ( fragments ) {

                jQuery.each(fragments, function(key, value) {
                    jQuery(key).replaceWith(value);
                });

            }

更多相关文章

  1. php mail函数一段好的代码
  2. 用于上传多个文件的PHP代码
  3. (phpQuery)对网站产品信息采集代码的优化
  4. 韩顺平_php从入门到精通_视频教程_学习笔记_源代码图解_PPT文档
  5. 【MySQL 技巧分享】 mysql -e 加 v 简化代码
  6. Oracle相当于MySQL代码“插入虚拟”以返回错误消息
  7. 登录使用PHP并´t显示任何html代码
  8. 在代码点火器中从mysql迁移到postgresql
  9. 【动软.Net代码生成器】连接MySQL生成C#的POCO实体类(Model)

随机推荐

  1. 在android中使用OrmLite数据库框架
  2. Android map
  3. Mac 配置Android环境变量( 有图)
  4. Android 编辑框(EditText)属性
  5. 新浪微博Android客户端SSO授权认证缺陷
  6. android中adb命令大全
  7. Android 性能优化的一些方法
  8. 【android乱七八糟】android体系结构
  9. OpenJDK将对Android开发产生怎样的影响?
  10. Android:EditText