Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ajax/6.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/wordpress/12.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
通过编程方式将Ajax添加到购物车_Ajax_Wordpress_Woocommerce - Fatal编程技术网

通过编程方式将Ajax添加到购物车

通过编程方式将Ajax添加到购物车,ajax,wordpress,woocommerce,Ajax,Wordpress,Woocommerce,我有这个按钮: <a href="http://my-site/checkout/" target="_blank" class="dt-sc-button">bla bla</a> PHP 我是一个javascript高手,所以你能给我指出正确的方向吗,或者给我一个关于我做错了什么的提示 提前谢谢 您可能需要取消/阻止单击链接的默认操作,然后在AJAX调用完成后重定向(这只是一种方法): PHP 正如我在评论中提到的,您几乎可以借用WooCommerce的核心功能 首

我有这个按钮:

<a href="http://my-site/checkout/" target="_blank" class="dt-sc-button">bla bla</a>
PHP

我是一个javascript高手,所以你能给我指出正确的方向吗,或者给我一个关于我做错了什么的提示


提前谢谢

您可能需要取消/阻止单击链接的默认操作,然后在AJAX调用完成后重定向(这只是一种方法):

PHP


正如我在评论中提到的,您几乎可以借用WooCommerce的核心功能

首先,这里是我们将尝试ajaxify的按钮:

<a href="http://local.wordpress.dev/checkout/" class="button test-button">bla bla</a>
现在,我们将编写ajax回调,它几乎是从WooCommerce core一字不差地复制而来,只做了一些小的修改:

add_action('wp_ajax_myajax', 'myajax_callback');
add_action('wp_ajax_nopriv_myajax', 'myajax_callback');

    /**
     * AJAX add to cart.
     */
function myajax_callback() {        
        ob_start();

        //$product_id        = 264;
        $product_id        = 34;
        $quantity          = 1;
        $passed_validation = apply_filters( 'woocommerce_add_to_cart_validation', true, $product_id, $quantity );
        $product_status    = get_post_status( $product_id );

        if ( $passed_validation && WC()->cart->add_to_cart( $product_id, $quantity ) && 'publish' === $product_status ) {

            do_action( 'woocommerce_ajax_added_to_cart', $product_id );

            wc_add_to_cart_message( $product_id );

        } else {

            // If there was an error adding to the cart, redirect to the product page to show any errors
            $data = array(
                'error'       => true,
                'product_url' => apply_filters( 'woocommerce_cart_redirect_after_error', get_permalink( $product_id ), $product_id )
            );

            wp_send_json( $data );

        }

        die();

}
最后是
test.js
的内容:

jQuery(document).ready(function($){     
  $(".test-button").click(function(e){ 
    e.preventDefault();  // Prevent the click from going to the link

    $.ajax({
        url: wc_add_to_cart_params.ajax_url,
        method: 'post',
        data: { 
            'action': 'myajax'
        }
    }).done( function (response) {
          if( response.error != 'undefined' && response.error ){
            //some kind of error processing or just redirect to link
            // might be a good idea to link to the single product page in case JS is disabled
            return true;
          } else {
            window.location.href = SO_TEST_AJAX.checkout_url;
          }
    });

  });

});

目前发生了什么?你收到什么错误了吗?不,我没有。当我检查控制台时,没有警告或错误,我注意到的唯一一件事是:单击按钮后,它加载指向:example.com/checkout的链接,然后直接进入购物车而不添加产品。有意义吗?友好地提醒客户可以想出一些愚蠢的东西。作为专业人士,我们的工作是指导他们。:)也就是说,
$(“.remove all”)
作为选择器与
和上的类不匹配。没有必要重新发明轮子。哦,还有PS-如果JS被禁用,链接只会将某人带到收银台,而不会向购物车添加任何内容。我仍然会得到相同的行为。它确实阻止了按钮的默认操作,但我仍然没有将任何产品添加到Cart中。您确定您的AJAX URL正确指向
admin AJAX.php
?我想是的。老实说,我不知道如何检查AJAX URL是否正确指向
admin AJAX.php
,但我的主题
ajaxurl中有一个变量:'example.com/wp admin/admin AJAX.php
,我将
wp admin/admin AJAX.php
替换为
ajaxurl
,我看不到任何变化。通常情况下,最好通过
wp_localize_script()
ajaxurl
传入,但WooCommerce已经这样做了,因此您可以通过
wc_add_to_cart_params.ajax_url
访问它,只要您在WooCommerce加载其
add to cart.js
script.PS-@rnevius后将自定义脚本排队,您的回调中没有验证。这会刷新页面吗?或者“添加到购物车”将通过ajax实现@helgathevikingAt@mehmoodkhan我不知道5年前的代码是否能正常工作,但是
window.location.href=SO\u TEST\u AJAX.checkout\u url表明,如果添加到购物车部分成功,我的努力是尝试重定向到签出。
add_action('wp_ajax_myajax', 'myajax');
add_action('wp_ajax_nopriv_myajax', 'myajax');

function myajax() {
    $product_id = 264;
    // Avoid using the global $woocommerce object
    WC()->cart->add_to_cart($product_id);
    die();
}
<a href="http://local.wordpress.dev/checkout/" class="button test-button">bla bla</a>
add_action( 'wp_enqueue_scripts', 'so_load_script', 20 );
function so_load_script(){
    wp_enqueue_script( 'so_test', plugins_url( 'js/test.js', __FILE__ ) );
    $i18n = array( 'ajax_url' => admin_url( 'admin-ajax.php' ), 'checkout_url' => get_permalink( wc_get_page_id( 'checkout' ) ) );
    wp_localize_script( 'so_test', 'SO_TEST_AJAX', $i18n );
}
add_action('wp_ajax_myajax', 'myajax_callback');
add_action('wp_ajax_nopriv_myajax', 'myajax_callback');

    /**
     * AJAX add to cart.
     */
function myajax_callback() {        
        ob_start();

        //$product_id        = 264;
        $product_id        = 34;
        $quantity          = 1;
        $passed_validation = apply_filters( 'woocommerce_add_to_cart_validation', true, $product_id, $quantity );
        $product_status    = get_post_status( $product_id );

        if ( $passed_validation && WC()->cart->add_to_cart( $product_id, $quantity ) && 'publish' === $product_status ) {

            do_action( 'woocommerce_ajax_added_to_cart', $product_id );

            wc_add_to_cart_message( $product_id );

        } else {

            // If there was an error adding to the cart, redirect to the product page to show any errors
            $data = array(
                'error'       => true,
                'product_url' => apply_filters( 'woocommerce_cart_redirect_after_error', get_permalink( $product_id ), $product_id )
            );

            wp_send_json( $data );

        }

        die();

}
jQuery(document).ready(function($){     
  $(".test-button").click(function(e){ 
    e.preventDefault();  // Prevent the click from going to the link

    $.ajax({
        url: wc_add_to_cart_params.ajax_url,
        method: 'post',
        data: { 
            'action': 'myajax'
        }
    }).done( function (response) {
          if( response.error != 'undefined' && response.error ){
            //some kind of error processing or just redirect to link
            // might be a good idea to link to the single product page in case JS is disabled
            return true;
          } else {
            window.location.href = SO_TEST_AJAX.checkout_url;
          }
    });

  });

});