【问题标题】:Custom add to cart button, if the customer has bought previously the product自定义添加到购物车按钮,如果客户之前购买过产品
【发布时间】:2017-01-16 20:20:18
【问题描述】:

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

我们通过 WooCommerce & Membership 销售在线课程,因此我们的想法是,如果客户还没有购买课程,他必须看到“立即购买”按钮。但如果他购买了课程,他应该会看到一个“立即查看”按钮,其中包含每个产品(课程)的自定义链接。

我怎样才能做到这一点?

谢谢。

【问题讨论】:

    标签: php wordpress woocommerce product orders


    【解决方案1】:

    这是一个功能齐全且经过测试的自定义功能,将显示在商店页面和存档 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();
    
        }
    

    【讨论】:

      【解决方案2】:

      下面的解决方案将它添加到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 )
              );
      
          }
      

      【讨论】:

      • 哇,谢谢!!还有一个问题,一旦使用此代码,我可以在哪里更改应该重定向按钮的 url?是否可以为每个产品设置不同的链接,我该怎么做?
      • 什么链接?它应该去哪里?链接可以放在这里esc_url( $product_url ),但我们必须知道它是什么链接?
      猜你喜欢
      • 2020-09-01
      • 2015-05-24
      • 2015-12-04
      • 1970-01-01
      • 2018-06-03
      • 1970-01-01
      • 2019-02-20
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多