将WooCommerce的“添加到购物车”替换为“下载”按钮

时间:2021-01-09 作者:dada

我有一个Wordprees网站,我在我的网站上卖书(产品ID 769),我想用woocommerce取代“woocommerce”;“添加到购物车”;带“的按钮”;下载“;按钮if

用户已登录并购买(产品ID“769”)用户将发现;下载“;“的按钮瞬间”;“添加到购物车”;产品单页中的按钮

1 个回复
SO网友:Joshua Pierson

可购买woocommerce挂钩

 add_filter("woocommerce_is_purchasable", function($product, $isPurchasable)
 {
     if(!$isPurchasable)
         return $isPurchasable;

     $productId = $product->get_id();
     $user = wp_get_current_user();
     if($user->exists() && $productId == 769)
     {
         $userId = $user->ID;
         $userEmail = $user->user_email;

         if(wc_customer_bought_product($userEmail, $userId, $productId))
         {
             //block the purchase so the user sees read more instead of add to cart
             $isPurchasable = false;
         }
     }

     return $isPurchasable;
 });
钩住init字段,如果当前页面是产品ID,那么我们需要显示:无“添加到购物车”按钮,而是添加一个“下载”按钮。如果您有权访问主题生成器,请在单个产品上添加一个按钮,并将其隐藏在开头。这将使这一过程更加容易。

 add_action("init", function()
 {
     global $product;
     if(is_product() && $product->get_id() == 769)
     {
         $className = "add_to_cart_button";
         $downloadLink = ""; // your link here

         printf(
             "<script>
                  let element = document.getElementByClassName(\\"%s\\");
                  element.style.display=\\"none\\";

                  let link = document.createElement(\\"a\\");
                  link.classList.add(\\"button\\");
                  link.setAttribute(\\"href\\", \\"%s\\");
                  link.textContent = \\"download\\";

                  let parent = element.parentElement;
                  parent.insertAdjacentElement(\\"afterend\\", link);
              </script>",
              $className,
              $downloadLink
         );
     }
 });
ps:如果您希望用户在将项目添加到购物车之前登录,请执行以下操作

    add_filter("woocommerce_is_purchasable", function($product, $isPurchasable)
    {
        if(!$isPurchasable)
            return $isPurchasable;

        $productId = $product->get_id();
        $user = wp_get_current_user();

        //block the user from even adding it to cart before logging in
        if($productId == 769 && !$user->exists())
            return false;

        if($user->exists() && $productId == 769)
        {
            $userId = $user->ID;
            $userEmail = $user->user_email;

            if(wc_customer_bought_product($userEmail, $userId, $productId))
            {
                //block the purchase so the user sees read more instead of add to cart
                $isPurchasable = false;
            }
        }

        return $isPurchasable;
    });
ps2:我自己并没有在头脑中运行这段代码。应该足够近才能跑。将检查它是否在我的系统上运行。

新代码:刚刚做了一次测试运行,它正常工作了

            add_filter("woocommerce_is_purchasable", function($isPurchasable, $product)
            {
                if(!$isPurchasable)
                    return $isPurchasable;

                $productId = $product->get_id();
                $user = wp_get_current_user();
                if($user->exists() && $productId == 796)
                {
                    $userId = $user->ID;
                    $userEmail = $user->user_email;

                    if(wc_customer_bought_product($userEmail, $userId, $productId))
                    {
                        //block the purchase so the user sees read more instead of add to cart
                        $isPurchasable = false;
                    }
                }

                return $isPurchasable;
            }, 10, 2);

            add_filter("woocommerce_is_purchasable", function($isPurchasable, $product)
            {
                if(!$isPurchasable)
                    return $isPurchasable;

                $productId = $product->get_id();
                $user = wp_get_current_user();

                //dont allow non existing users to buy
                if(!$user->exists() && $productId == 796)
                    $isPurchasable = false;

                return $isPurchasable;
            }, 10, 2);

            add_action("woocommerce_after_single_product", function()
            {
                global $post;
                $product = wc_get_product($post->ID);
                
                if(empty($product) || is_null($product))
                    return;

                if($product->get_id() != 796)
                    return;

                $user = wp_get_current_user();
                if(!$user->exists())
                    return;

                if(!wc_customer_bought_product($user->user_email, $user->ID, $product->get_id()))
                    return;

                $productId = $product->get_id();
                $downloadLink = "#"; // your link here

                echo sprintf(
                    "<script>
                        let element = document.getElementById(\\"product-%d\\");
                        let form = element.getElementsByTagName(\\"form\\")[0];
                        let addToCartButton = form.getElementsByTagName(\\"button\\")[0];

                        element = addToCartButton;
                        let classList = element.classList;

                        element.style.display=\\"none\\";

                        let link = document.createElement(\\"a\\");
                        link.classList = classList;
                        link.setAttribute(\\"href\\", \\"%s\\");
                        link.textContent = \\"download\\";

                        let parent = element.parentElement;
                        parent.insertAdjacentElement(\\"afterend\\", link);
                    </script>",
                    $productId,
                    $downloadLink
                );
            });

相关推荐

在我的帐户中输出自定义页面的PHP代码

几周来我一直在努力解决这个问题。我向woocommerce我的帐户页面添加了一些自定义端点。除了一个外,其他所有的都与一个短代码相链接,工作正常。你应该链接到一个普通的wordpress页面,在我的账户页面中显示它。我不知道如何编写php,现在它所做的只是在一个空屏幕上打开该页面,而不是在我的帐户页面中。这是我想到的代码,但是{}中的部分肯定是错误的(因为它打开时https://new.holistic-horse-training.com/forum-links):add_action( \'wooco