Woocommerce add to cart button redirect to checkout

前端 未结 8 1955
时光取名叫无心
时光取名叫无心 2020-12-05 23:49

I created an ecommerce using the plugin woocommerce. I am selling only a subscription so the \"/cart/\" page is useless. I\'m trying to get rid of it so that when my custome

8条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-12-06 00:22

    On shop page, if you want use ajax and redirect toghether. The second method only when there are some condition, you can use this filter and leave on Woocommerce setting ajax enabled:

    add_filter('woocommerce_loop_add_to_cart_link', array( $this, 'add_quantity_input' ), 4, 2); 
    

    to remove on a class attribute ajax_add_to_cart and change the href value to checkout url page;

    On my template case:

    public function add_quantity_input($text = null, $product = null) {
        global $product, $woocommerce;
    
        if ( $text != null and $product != null  ) {
            if(ismycondition($product->id)) {
                $s = explode('class="', $text);
                $s[2]=str_replace('ajax_add_to_cart', '', $s[2]);
                $text = implode('class="', $s);
    
                $text = preg_replace('//','cart->get_checkout_url().'"$3>', $text);
            }
        }
    
        return $text;
    }
    

    I hope that this help.

提交回复
热议问题