如果已购买产品并显示 MP3 播放器,则删除 WooCommerce 单个产品页面上的添加到购物车按钮

Remove add to cart button on WooCommerce single product page if product has been purchased and show MP3 player instead

我们有一家 WooCommerce 商店,可以向注册用户出售文件 (MP3/MP4),并使用户能够在其帐户的“下载”部分播放这些文件(我们已修改 )。

我们面临的挑战是,我们想让用户也能在他们已经购买的产品的特定产品页面上播放 MP3(而不是“添加到购物车”按钮,我们希望向播放器显示用户是否已登录并且之前购买过此产品)。

我已经找到了关于如何防止用户再次购买相同产品的各种解决方案,但是我不知道如何结合订单-downloads.php 中的代码和产品模板来执行此操作。

到目前为止,这是我的代码尝试,但不幸的是没有得到想要的结果。谁能给我指出正确的方向?

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){
       global $product;
       $downloads = array();
       $user_id = get_current_user_id();
       $downloads = wc_get_customer_available_downloads($user_id);

           if (!empty($downloads)) {
               foreach ($downloads as $download) {
               if ($download['product_id'] === $product->get_id()) {
                 $thelink = $download['download_url'];
               }
        }
    }
     /* $add_to_cart_url = site_url($thelink); */
        $button_text =  __('[audio src='.$thelink.']', '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;
}

有多种方法可以在 WooCommerce 中 hide/replace 添加到购物车按钮。这是找出最适合您的问题的钩子的问题。

由于您不只是想隐藏添加到购物车按钮,而是想根据一些条件来执行此操作,我们将首先应用一个执行多项检查的自定义函数:

  • 如果用户已登录
  • 如果产品类型简单
  • 如果产品已经购买
  • 如果产品包含可下载的 mp3 文件

如果满足所有这些条件,我们 return 文件 url,如果不满足,我们 return 一个空字符串:

function multiple_checks() {
    // Initialize
    $url_file = '';

    // Retrieve the current user object.
    $current_user = wp_get_current_user();

    if ( $current_user->ID == 0 ) return $url_file;

    global $product;
    
    // Is a WC product
    if ( is_a( $product, 'WC_Product' ) ) {
        // Only for simple products
        if ( $product->get_type() != 'simple' ) return $url_file;

        // Has bought
        if ( wc_customer_bought_product( $current_user->user_email, $current_user->ID, $product->get_id() ) ) {
            // NOT has downloads
            if ( ! $product->is_downloadable() ) {
                return $url_file;
            } else {
                // Loop through WC_Product_Download objects
                foreach ( $product->get_downloads() as $key_download_id => $download ) {
                    // Only for MP3 file
                    if ( $download->get_file_extension() == 'mp3' ) {                       
                        // Get url
                        $url_file = $download->get_file();

                        // Break loop
                        break;
                    }
                }
            }
        }
    }

    return $url_file;
}

然后只需从所需的页面和所需的显示中调用函数即可。


对于单品页面:

function action_woocommerce_single_product_summary() {
    // Call function
    $url_file = multiple_checks();

    // NOT empty
    if ( ! empty ( $url_file ) ) { 
        // Hide prices
        remove_action('woocommerce_single_product_summary', 'woocommerce_template_single_price', 10 );

        // Removing add to cart button and quantities
        remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_add_to_cart', 30 );

        // New ouput
        echo "<div id='content' class='fancybox-hide' style='min-width:450px; min-height:250px;'>";
        echo do_shortcode( '[audio src="' . $url_file . '"]' );
        echo "</div>";
        echo "<a href='#content' class='ari-fancybox'>Listen to MP3</a>";
    }
}
add_action( 'woocommerce_single_product_summary', 'action_woocommerce_single_product_summary', 29 );

对于商店和档案页面:

function filter_woocommerce_loop_add_to_cart_link( $sprintf, $product, $args ) {
    // Call function
    $url_file = multiple_checks();

    // NOT empty
    if ( ! empty ( $url_file ) ) { 
        // Setting
        $button_text = __( 'Your text', 'woocommerce' );
        
        // Single product Url
        $button_link = $product->get_permalink();
        
        // New link + text
        $sprintf = sprintf(
            '<a href="%s" class="%s">%s</a>',
            esc_url( $button_link ),
            esc_attr( isset( $args['class'] ) ? $args['class'] : 'button' ),
            esc_html( $button_text )
        );
    }
    
    return $sprintf;
}
add_filter( 'woocommerce_loop_add_to_cart_link', 'filter_woocommerce_loop_add_to_cart_link', 10, 3 );