Woocommerce - 在页面加载时,将产品添加到购物车并自动进入结帐

时间:2018-01-26 04:41:12

标签: php wordpress woocommerce

我想提供一个链接,允许他们在结帐页面上登陆,产品已经在购物车中。

我相信我需要做两件事:

  1. 在页面加载时,运行脚本以自动将产品添加到购物车
  2. 在产品上添加到购物车,运行另一个脚本将此人发送到结帐页面
  3. 我有两个片段,但我不确定它们是否会像这样一起工作,我不知道如何让它在特定页面上工作(可能只是一个空白页面将几乎没有足够长的时间显示添加到购物车并将用户发送到结帐页面

    <?php
    /*
     * goes in theme functions.php or a custom plugin
     **/
    // add item to cart on visit
    add_action( 'template_redirect', 'add_product_to_cart' );
    function add_product_to_cart() {
        if ( ! is_admin() ) {
            $product_id = 21;
            $found = false;
            //check if product already in cart
            if ( sizeof( WC()->cart->get_cart() ) > 0 ) {
                foreach ( WC()->cart->get_cart() as $cart_item_key => $values ) {
                    $_product = $values['data'];
                    if {( $_product->id == $product_id )
                        $found = true;}
                }
                // if product not found, add it
                if {( ! $found )
                    WC()->cart->add_to_cart( $product_id );}
            } else {
                // if no products in cart, add it
                WC()->cart->add_to_cart( $product_id );
            }
        }
    }
    
    /**
     * Redirect users after add to cart.
     */
    function my_custom_add_to_cart_redirect( $url ) {
        $url = WC()->cart->get_checkout_url();
        // $url = wc_get_checkout_url(); // since WC 2.5.0
        return $url;
    }
    add_filter( 'woocommerce_add_to_cart_redirect', 'my_custom_add_to_cart_redirect' );
    

    如果还有更好的方法,请告诉我。

    谢谢, 布赖恩

2 个答案:

答案 0 :(得分:3)

您实际上可以在没有任何其他代码的情况下执行此操作。

WooCommerce支持通过?add-to-cart网址参数向购物车添加商品。

如果您将某人发送到任何网页,然后是网址参数?add-to-cart=21,则会自动将ID为21的产品添加到购物车中。在这种情况下,您可以直接将它们发送到 Checkout 页面。

这是您的自定义网址的样子:

http://example.com/checkout/?add-to-cart=21

这样做比运行脚本要快得多,然后重定向到结帐。

答案 1 :(得分:2)

我建议为通用用例设置此功能,例如将用户发送到example.com?get=21,其中21是您要在加载时添加到购物车的产品ID。要遵循这种方法,您的代码将类似于

add_action( 'template_redirect', 'add_product_to_cart' );
function add_product_to_cart() {

    if ( ! is_admin() && isset( $_GET['get'] ) ) {
        $product_id = $_GET['get'];
        $found = false;
        //check if product already in cart
        if ( sizeof( WC()->cart->get_cart() ) > 0 ) {
            foreach ( WC()->cart->get_cart() as $cart_item_key => $values ) {
                $_product = $values['data'];
                if ( $_product->id == $product_id ) {
                    $found = true;
                }
            }
            // if product not found, add it
            if ( ! $found ){
                WC()->cart->add_to_cart( $product_id );
            }
        } else {
            // if no products in cart, add it
            WC()->cart->add_to_cart( $product_id );
        }

        // Redirect users to checkout after add to cart
        wp_redirect( wc_get_checkout_url() );
        exit;
    }
}

您不需要'woocommerce_add_to_cart_redirect'过滤器及其功能。

现在,如果您只想在特定页面上触发此操作,请说该页面的ID为55,那么该行

if ( ! is_admin() && isset( $_GET['get'] ) ) {

将成为

if ( ! is_admin() && is_page( 55 ) ) {
相关问题