删除订购产品的“添加到购物车”按钮

时间:2019-07-04 06:06:56

标签: php wordpress woocommerce wordpress-theming hook-woocommerce

我正在尝试从订购的产品(用户已下订单的产品)中移除add to cart按钮。例如,当用户结帐1个或多个产品时,当他下订单并仅从该订购产品中删除add to cart按钮时,该按钮应删除临时的而不是永久的。因为我在结帐页面上添加了名为Date Of Event的新字段。

它应该像这样工作:如果用户在结帐页面的结帐期间选择日期7/4/2019 or 9/4/2019并下订单,那么add to cart按钮将仅在用户选择的那天从订购产品中删除结帐期间Date Of Event Field中的日期。

我正在尝试从订购的产品中删除“添加到购物车”按钮,但是我已经尝试了,即使没有订购这些产品,也正在从所有产品中删除“添加到购物车”按钮。

请帮助。

添加了自定义字段代码:

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 hidecart() {
    $found = has_bought_items();
    if (!empty($found)) {
        $event_date = $_POST['date_of_event'];
        if ($found === $event_date) {
            return false;
        }
    }
}

add_filter('woocommerce_is_purchasable', 'hidecart', 10, 1);

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

    $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;
                $event_date = $_POST['date_of_event'];
                // you can fetch your event date stored as order meta
                return $event_date;
            }
        }
    }
    return $event_date;
}

有人可以帮忙吗?

我被困住了。

0 个答案:

没有答案