更改“添加到购物车"Woocommerce 中购买产品的按钮 [英] Change "add to cart" button for purchased products in Woocommerce

查看:70
本文介绍了更改“添加到购物车"Woocommerce 中购买产品的按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想做的是:

客户购买产品.它不会显示添加到购物车",而是显示查看数字产品"并具有指向特定页面的自定义链接.

A customer buys a product. Instead of "add to cart" it would show "See Digital Product" and have a custom link to a specific page.

我正在查看 WooCommerce 的数据库,并试图弄清楚我如何知道某件商品已被购买,以便我可以弄清楚如何让函数自动执行此操作:

I was looking in the database for WooCommerce and trying to figure out how I would know an item is already purchased so I can figure out how to make a function do this automatically:

SELECT * FROM wp_woocommerce_payment_tokens

SELECT * FROM wp_woocommerce_payment_tokens

token_id    gateway_id  token   user_id     type    is_default 

SELECT * FROM wp_woocommerce_order_items

SELECT * FROM wp_woocommerce_order_items

order_item_id   order_item_name     order_item_type     order_id 

但我无法弄清楚他们的逻辑或 WordPress 中的正确功能来实现这一目标.

But I couldn't figure out their logic yet or the right function in WordPress to make this happen.

我在网上能找到的唯一功能是重定向,但这仅适用于您购买商品时,而不是您返回已购买商品的页面时:

The only function I could find online is for redirects but this is only right when you purchase the item, not if you go back to the page where you already bought the item:

add_action( 'template_redirect', 'wc_custom_redirect_after_purchase' );
function wc_custom_redirect_after_purchase() {
   global $wp;

   if ( is_checkout() && ! empty( $wp->query_vars['order-received'] ) ) {
        wp_redirect( get_permalink( get_page_by_title( "About" )->ID ) );
        exit;
    }
} 

请引导我走向正确的方向.

Please lead me in the right direction.

推荐答案

以下函数将检查客户是否已经购买了产品,如果是这种情况,添加到购物车按钮将被自定义See Digital产品"链接按钮到关于"页面.

The following functions will check if a customer has already bought a product and if it is the case the add to cart button will be replaced by a custom "See Digital Product" linked button to "About" page.

第一个函数代码由wc_customer_bought_product() 源代码组成,以更轻松的方式.该代码将仅检查已登录的用户,对于未登录的用户返回 false.

The first function code is made from wc_customer_bought_product() source code, in a lighter way. The code will check for logged in users only, returning false for unlogged users.

function has_bought_item( $product_id ) {
    global $wpdb;

    if( ! is_user_logged_in() )
        return false;

    $customer_id = get_current_user_id();
    $statuses      = array_map( 'esc_sql', wc_get_is_paid_statuses() );

    // Count the number of products
    $count_query = $wpdb->get_var( "
        SELECT COUNT(woim.meta_value) FROM {$wpdb->prefix}posts AS p
        INNER JOIN {$wpdb->prefix}postmeta AS pm ON p.ID = pm.post_id
        INNER JOIN {$wpdb->prefix}woocommerce_order_items AS woi ON p.ID = woi.order_id
        INNER JOIN {$wpdb->prefix}woocommerce_order_itemmeta AS woim ON woi.order_item_id = woim.order_item_id
        WHERE p.post_status IN ( 'wc-" . implode( "','wc-", $statuses ) . "' )
        AND pm.meta_key = '_customer_user' AND pm.meta_value = $customer_id
        AND woim.meta_key IN ( '_product_id', '_variation_id' )
        AND woim.meta_value = $product_id
    " );

    // Return true boolean value if count is higher than 0, if not false
    return $count_query > 0 ? true : false;
}

// Shop and archives - Replace add to cart ajax button to a custom linked button
add_filter( 'woocommerce_loop_add_to_cart_link', 'replace_loop_add_to_cart', 20, 2 );
function replace_loop_add_to_cart( $html, $product ) {
    if( has_bought_item( $product->get_id() ) ) {
        $page = get_page_by_title( "About" );
        if ( ! is_object($page) )
            return $html;
        $link = get_permalink( $page->ID );
        $text = __("See Digital Product", "woocommerce");
        $html = '<a href="' . $link . '" class="button alt add_to_cart_button">' . $text . '</a>';
    }
    return $html;
}

// Single products: Replacing the button add to cart by a custom button on single product pages
add_action( 'woocommerce_single_product_summary', 'replace_single_add_to_cart_button', 1 );
function replace_single_add_to_cart_button() {
    global $product;

    if( has_bought_item( $product->get_id() ) && get_page_by_title( "About" ) ) {
        // For variable product types (keeping attribute select fields)
        if( $product->is_type( 'variable' ) ) {
            remove_action( 'woocommerce_single_variation', 'woocommerce_single_variation_add_to_cart_button', 20 );
            add_action( 'woocommerce_single_product_summary', 'custom_single_add_to_cart_button', 20 );
        }
        // For all other product types
        else {
            remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_add_to_cart', 30 );
            add_action( 'woocommerce_single_product_summary', 'custom_single_add_to_cart_button', 30 );
        }
    }
}

// Utility function: displays a custom  button replacement
function custom_single_add_to_cart_button() {
    $page = get_page_by_title( "About" );
    if ( ! is_object($page) )
        return;
    $link = get_permalink( $page->ID );
    $text = __("See Digital Product", "woocommerce");
    echo '<a href="' . $link . '" class="button alt add_to_cart_button">' . $text . '</a>';
}

代码位于活动子主题(或活动主题)的 function.php 文件中.经测试有效.

Code goes in function.php file of your active child theme (or active theme). Tested and works.

基于:

这篇关于更改“添加到购物车"Woocommerce 中购买产品的按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆