已更新 (适用于Woocommerce 3+)
是的,如果当前客户已经购买了特定定义的产品ID,则可以编写一个条件函数返回“ true”。
此代码位于您的活动子主题或主题的function.php文件中 。
这是条件函数:
function has_bought_items() { $bought = false; // Set HERE ine the array your specific target product IDs $prod_arr = array( '21', '67' ); // Get all customer orders $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" ) ); foreach ( $customer_orders as $customer_order ) { // Updated compatibility with WooCommerce 3+ $order_id = method_exists( $order, 'get_id' ) ? $order->get_id() : $order->id; $order = wc_get_order( $customer_order ); // Iterating through each current customer products bought in the order foreach ($order->get_items() as $item) { // WC 3+ compatibility if ( version_compare( WC_VERSION, '3.0', '<' ) ) $product_id = $item['product_id']; else $product_id = $item->get_product_id(); // Your condition related to your 2 specific products Ids if ( in_array( $product_id, $prod_arr ) ) $bought = true; } } // return "true" if one the specifics products have been bought before by customer return $bought;}此代码已经过测试并且可以工作。
用法:
例如,您可以在一些 WooCommerce模板 中使用它,这些 模板 先前已复制到您的活动子主题或主题:
- “ 商店”页面上 与
add-to-cart
按钮有关的模板是loop/add-to-cart.php
。 - 有关按钮的“ 产品”页面 的模板
add-to-cart
位于single-product/add-to-cart
文件夹中,具体取决于您的产品类型。
这是您可以在 上面的 模板中使用的示例:
// Replace the numbers by your special restricted products IDs$restricted_products = array( '20', '32', '75' );// compatibility with WC +3$product_id = method_exists( $product, 'get_id' ) ? $product->get_id() : $product->id;// customer has NOT already bought a specific product for this restricted productsif ( !has_bought_items() && in_array( $product_id, $restricted_products ) ) { // Displaying an INACTIVE add-to-cart button (With a custom text, style and without the link). // (AND optionally) an explicit message for example.// ALL OTHER PRODUCTS OR RESTRICTED PRODUCTS IF COSTUMER HAS ALREADY BOUGHT SPECIAL PRODUCTS} else { // place for normal Add-To-Cart button pre here}这里 完整的应用实例 ,以 add-to-cart
按钮模板 店铺页 :
<?phpif ( ! defined( 'ABSPATH' ) ) { exit;}global $product;// Replace the numbers by your special restricted products IDs$restricted_products = array( '37', '53', '70' );// compatibility with WC +3$product_id = method_exists( $product, 'get_id' ) ? $product->get_id() : $product->id;if ( !has_bought_items() && in_array( $product_id, $restricted_products ) ) { echo '<a >' . __("Disabled", "your_theme_slug") . '</a>'; echo '<br><span >' . __("Your message goes here…", "your_theme_slug") . '</span>';} else {echo apply_filters( 'woocommerce_loop_add_to_cart_link', sprintf( '<a rel="nofollow" href="%s" data-quantity="%s" data-product_id="%s" data-product_sku="%s" >%s</a>', esc_url( $product->add_to_cart_url() ), esc_attr( isset( $quantity ) ? $quantity : 1 ), esc_attr( $product_id ), esc_attr( $product->get_sku() ), esc_attr( isset( $class ) ? $class : 'button' ), esc_html( $product->add_to_cart_text() ) ),$product );}您将 greyed_button
在 style.css
活动子主题或主题的文件中使用class 设置非活动按钮的样式。与greyed_button-message
类消息相同。



