【问题标题】:Woocommerce - On page load, add product to cart and proceed to Checkout automaticallyWoocommerce - 在页面加载时,将产品添加到购物车并自动进行结帐
【发布时间】:2018-01-26 04:41:11
【问题描述】:

我想给一个人提供一个链接,让他们可以直接登陆结帐页面,并且产品已经在他们的购物车中。

我相信我需要做两件事:

  1. 在页面加载时,运行脚本以自动将产品添加到购物车
  2. 在将产品添加到购物车时,运行另一个脚本将用户发送到结帐页面

我每个都有两个 sn-ps,但我不确定它们是否会像这样一起工作,我不确定如何让它在特定页面上工作(可能只是一个空白页面几乎不会显示足够长的时间以添加到购物车并将用户发送到结帐页面)

<?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' );

如果还有更好的方法可以做到这一点,请告诉我。

谢谢, 布赖恩

【问题讨论】:

    标签: php wordpress woocommerce


    【解决方案1】:

    您实际上可以在没有任何额外代码的情况下做到这一点。

    WooCommerce 支持通过 ?add-to-cart URL 参数将商品添加到购物车。

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

    这是您的自定义 URL 的样子:

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

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

    【讨论】:

    • @Brian 这个方法简单又好用,当然大家都知道这个方法。您遵循的代码还会检查产品是否已经是购物车,如果是,请不要添加它。
    【解决方案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 ) ) {
    

    【讨论】:

    • 非常感谢您花时间回答我的问题!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-08-30
    • 2014-11-21
    • 1970-01-01
    • 1970-01-01
    • 2019-05-04
    • 2021-08-22
    • 2016-09-28
    相关资源
    最近更新 更多