首页 > 解决方案 > 在订购产品后隐藏“添加到购物车”按钮 1 天,对于用户订购的产品

问题描述

我在结帐页面中添加了一个新的自定义字段,名为" Date of event ",它工作正常。但是我想做一件事,那就是“当用户订购单个/多个产品时,隐藏"Add to cart"按钮并显示不可用消息而不是显示所选事件日期的按钮。”就像用户在结帐期间选择日期" 7/2/2019 "" Date of event field "然后在他订购之后产品,隐藏“添加到购物车”按钮并显示不可用消息而不是" 7/2/2019 "事件日期按钮。我不知道该怎么做。
哪些钩子和动作会做到这一点。
我用谷歌搜索了很多,但没有得到任何答案。

请帮我。

自定义字段代码:

add_action('woocommerce_after_checkout_billing_form', 'date_of_event_field');

function date_of_event_field($checkout){

    echo '<div id="date_of_event_field" class="margin-top-20">';

        woocommerce_form_field( 'date_of_event', array(
            'type'          => 'date',
            'class'         => array('my-field-class form-row-wide'),
            'label'         => __('Date Of Event'),
            'required'      => true,
        ), $checkout->get_value( 'date_of_event' ));

    echo '</div>';

}

隐藏添加到购物车按钮并显示消息而不是按钮的代码:

function make_product_unavailable( $_product, $order ) {
    if( $order->id == $_product ){
        remove_action( 'woocommerce_after_shop_loop_item', 'woocommerce_template_loop_add_to_cart');
        remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_add_to_cart');
    }
}

这是我的尝试,因为我不知道该怎么做,也不知道将使用哪个过滤器/动作挂钩。

请帮我。

标签: wordpresswoocommercee-commercehook-woocommercewoocommerce-rest-api

解决方案


来源:https ://wisdmlabs.com/blog/the-right-way-to-hide-add-to-cart-button-in-woocommerce/

检查客户是否在 WooCommerce 中购买了特定产品

注意:以下代码未经测试,但如果根据您的要求修改某些部分,它将起作用..

add_filter( 'woocommerce_is_purchasable', 'hidecart',10,1);
function hidecart(){
 $found = has_bought_items();
 if(!empty($found["order_date"])){
    $current_date = date("d/m/Y");
    if($found["order_date"] == $current_date && $found["product_id"] == get_the_ID()){
        return false;
    }
 }
}

function has_bought_items() 
{
 $bought = false;
 $order_date =  '';
 //get product id if single or for shop page u will have to retrieve in some other way
$product_id = get_the_ID();

// 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', '<' ) ) 
            $order_product_id = $item['product_id'];
        else
            $order_product_id = $item->get_product_id();

        // Your condition related to your 2 specific products Ids
        if ( $product_id == $product_id) ) {
            $bought = true;
            $order_date = $order->order_date;
            // you can fetch your event date stored as order meta
            $arr = array("product_id"=>$product_id,"order_date"=>$order_date,"bought"=>$bought);
            return $arr;
        }
      }
    }
 $arr = array("product_id"=>"","order_date"=>$order_date,"bought"=>$bought);
 return $order_date;
 }

推荐阅读