【问题标题】:Woocommerce Redirecting Add to Cart button conditionallyWoocommerce 重定向有条件地添加到购物车按钮
【发布时间】:2014-09-10 05:21:43
【问题描述】:

将某个产品添加到购物车后,我希望将用户重定向到不同的页面。我已经尝试了网络上的几个 sn-ps,但是当我在 functions.php 中输入条件逻辑时,我所有的条件逻辑都返回 null 或 0。我已经尝试了基于页面、产品和类别的条件。这是我从这里的另一个答案中找到的最新 sn-p,它对我不起作用,知道为什么吗?

    add_filter ('add_to_cart_redirect', 'redirect_to_checkout');
    function redirect_to_checkout() {
            global $woocommerce;
            //Get product ID
            $product_id = (int) apply_filters('woocommerce_add_to_cart_product_id', $_POST['product_id']);
            //Check if product ID is in a certain taxonomy
            if( has_term( 'sidestix', 'product_cat', $product_id ) ){
                        //Get cart URL
                        $checkout_url = get_permalink(get_option('woocommerce_checkout_page_id'));
                        //Return the new URL
                        return $checkout_url;
             }
    }

【问题讨论】:

  • Here 你在搜索什么

标签: php wordpress woocommerce add cart


【解决方案1】:

我终于在另一个论坛上找到了一个有效的 sn-p。我可以看到差异,但我不确定它为什么有效。

    add_filter ('add_to_cart_redirect', 'redirect_to_checkout');
    function redirect_to_checkout() {
            global $woocommerce;

            //Get product ID
            $product_id = apply_filters( 'woocommerce_add_to_cart_product_id', absint($_REQUEST['add-to-cart'] ) );
            //Check if product ID is in a certain taxonomy
            if( has_term( 'Philanthropy', 'product_cat', $product_id ) ){
                        //Get cart URL
                        $checkout_url = get_permalink(get_option('woocommerce_checkout_page_id'));
                        //Return the new URL
                    return $checkout_url;
             };
    }

【讨论】:

  • 谢谢!对我来说,这仅在我将“add_to_cart_redirect”更改为“woocommerce_add_to_cart_redirect”并在 WooCommerce > 设置 > 产品 > 显示中禁用“启用 AJAX 添加到档案上的购物车按钮”之后才有效
猜你喜欢
  • 2014-06-01
  • 2013-03-13
  • 1970-01-01
  • 1970-01-01
  • 2018-10-30
  • 1970-01-01
  • 2020-06-08
  • 2018-05-08
相关资源
最近更新 更多