我在结帐页面上添加了一个名为" Date of event "
的新自定义字段,它工作正常。但是我想做一件事,即“当用户订购单个/多个产品时,隐藏"Add to cart"
按钮并显示不可用的消息,而不是该所选事件日期的按钮。”就像用户选择日期{{1}在结帐期间" 7/2/2019 "
中的}中,然后在他订购该产品后,隐藏“添加到购物车”按钮,并在事件" Date of event field "
的日期显示不可用的消息,而不是按钮。我不知道该怎么做。
哪些钩子和动作将执行此操作。
我在Google上搜索了很多,但没有得到任何答案。
请帮助我。
自定义字段代码:
" 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>';
}
这是我的尝试,因为我不知道该怎么做,也不知道将使用哪个过滤器/操作挂钩。
请帮助我。
答案 0 :(得分:0)
来源:https://wisdmlabs.com/blog/the-right-way-to-hide-add-to-cart-button-in-woocommerce/
Check if a customer has purchased a specific products in 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;
}