自定义添加到购物车按钮,如果客户以前购买过该产品 [英] Custom add to cart button, if the customer has bought previously the product

查看:46
本文介绍了自定义添加到购物车按钮,如果客户以前购买过该产品的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 WooCommerce 中,如果客户之前购买过该产品,我需要知道是否有任何选项可以更改添加到购物车按钮.

In WooCommerce, I need to know if there's any option to change the add to cart button, if the customer has bought previously the product.

我们通过 WooCommerce & 销售在线课程会员资格,所以这个想法是,如果客户还没有购买课程,他必须看到立即购买"按钮.但如果他购买了课程,他应该会看到一个立即查看"按钮,每个产品(课程)都有一个自定义链接.

We're selling online courses via WooCommerce & Membership, so the idea is that if the customer hasn't bought the course, he has to see the "Shop now" button. But if he has bought the course, he should see a "View now" button, with a custom link for each product (course).

我怎样才能做到这一点?

How can I achieve this?

谢谢.

推荐答案

这是一个功能齐全且经过测试的自定义功能,将显示在商店页面和存档 woocommerce 页面上,自定义添加到购物车(文本 + 链接),对于已经购买产品的登录客户:

Here is a fully functional and tested custom function that will display on shop pages and archives woocommerce pages, a custom add-to-cart (text + link), for logged in customers that have already bought the products:

add_filter( 'woocommerce_loop_add_to_cart_link', 'customizing_add_to_cart_button', 10, 2 );
function customizing_add_to_cart_button( $link, $product ){

    $bought = false;

    if( is_user_logged_in() ){

        $customer_orders = get_posts( array(
            'numberposts' => -1,
            'meta_key'    => '_customer_user',
            'meta_value'  => get_current_user_id(),
            'post_type'   => 'shop_order', // WC orders post type
            'post_status' => 'wc-completed' // Only orders with status "completed"
        ) );

        // Going through each current customer orders
        foreach ( $customer_orders as $customer_order ) {
            $order = wc_get_order( $customer_order->ID );
            // Going through each current customer order items
            foreach($order->get_items() as $item_id => $item_values){
                if($item_values['product_id'] == $product->id){
                    $bought = true;
                    break;
                }
            }
        }
    }

    if($bought){

        // ==> SET HERE YOUR
        // CUSTOM ADD TO CART text and link
        $add_to_cart_url = site_url('/custom_link/');
        $button_text =  __('View now', 'woocommerce');

    } else {

        $add_to_cart_url = $product->add_to_cart_url();
        $button_text =  $product->add_to_cart_text();

    }

    $link = sprintf( '<a href="%s" rel="nofollow" data-product_id="%s" data-product_sku="%s" data-quantity="%s" class="button product_type_%s">%s</a>',
        esc_url( $add_to_cart_url ),
        esc_attr( $product->id ),
        esc_attr( $product->get_sku() ),
        esc_attr( isset( $quantity ) ? $quantity : 1 ),
        esc_attr( $product->product_type ),
        esc_html( $button_text )
    );

    return $link;
}

此代码位于活动子主题(或主题)的 function.php 文件或任何插件文件中.

如果您想为某些特定产品、产品数组或产品类别自定义添加到购物车按钮,则必须按特定产品添加条件,这样(这是上面的代码):

If you want to customize the add-to-cart button for some specific products, or an array of products or a product category, you will have to add a condition by specific product, this way (this is an extract of the code above):

    if($bought){

        // for the product ID 45 (for example)
        if( $product->id == 45 ){
            $add_to_cart_url = site_url('/custom-link/product-45/');
            $button_text =  __('View now product 45', 'woocommerce');
        }

        // for the product ID 64 (for example)
        if( $product->id == 64){
            $add_to_cart_url = site_url('/custom-link/product-64/');
            $button_text =  __('View now product 64', 'woocommerce');
        }

        // for an array of product IDs (for example)
        $product_ids = array(89, 92, 124);
        if( in_array( $product->id, $product_ids ) ){
            $add_to_cart_url = site_url('/custom-link/some-products/');
            $button_text =  __('View now some products', 'woocommerce');
        }
        // for a product category
        // set here your product category ID, slug or name (or an array of values)
        $category = 'My category'; // Here a category name for example
        if( has_term( $category, 'product_cat', $product->id ) ){
            $add_to_cart_url = site_url('/custom-link/my-category/');
            $button_text =  __('View now from my category', 'woocommerce');
        }
    } else {

        $add_to_cart_url = $product->add_to_cart_url();
        $button_text =  $product->add_to_cart_text();

    }

这篇关于自定义添加到购物车按钮,如果客户以前购买过该产品的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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