如果客户以前购买过产品,则自定义添加到购物车按钮

时间:2017-01-16 20:20:18

标签: php wordpress woocommerce product orders

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

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

我怎样才能做到这一点?

感谢。

2 个答案:

答案 0 :(得分:2)

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

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($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();

    }

答案 1 :(得分:1)

下面的解决方案将其添加到functions.php并且应该可以正常工作。当订单完成时,按钮将被更改。

add_filter('woocommerce_loop_add_to_cart_link','add_to_cart_link_customer_has_bought');

    function add_to_cart_link_customer_has_bought() {

        global $product;

        if( empty( $product->id ) ){

            $wc_pf = new WC_Product_Factory();
            $product = $wc_pf->get_product( $id );

        }

        $current_user = wp_get_current_user();

        if( wc_customer_bought_product( $current_user->user_email, $current_user->ID, $product->id ) ){

            $product_url = get_permalink();
            $button_label =  "View Product";  

        } else {

            $product_url =  $product->add_to_cart_url();  
            $button_label = $product->add_to_cart_text();

        };

        echo sprintf( '<a href="%s" rel="nofollow" data-product_id="%s" data-product_sku="%s" data-quantity="%s" class="button %s product_type_%s">%s</a>',
            esc_url( $product_url ),
            esc_attr( $product->id ),
            esc_attr( $product->get_sku() ),
            esc_attr( isset( $quantity ) ? $quantity : 1 ),
            $product->is_purchasable() && $product->is_in_stock() ? 'add_to_cart_button' : '',
            esc_attr( $product->product_type ),
            esc_html( $button_label )
        );

    }
相关问题