首页 > 解决方案 > 在 Woocommerce 我的帐户查看订单页面中为自定义状态启用“重新排序”按钮

问题描述

我需要在我的 Woocommerce 网站上请求报价系统,但找不到任何与复合产品插件兼容的系统。因此,我将让客户使用“不发货/报价”发货选项和“请求报价支付网关”正常结账。这样我可以在后端看到报价,批准它们,然后客户可以订购(从技术上重新排序)他们在我的帐户部分的报价。

我得到了 Completed orders 和 Quote Approved 的按钮来显示使用这个:

/**
 * Add order again button in my orders completed actions.
 *
 * @param  array $actions
 * @param  WC_Order $order
 * @return array
 */
function cs_add_order_again_to_my_orders_actions( $actions, $order ) {
    if ( $order->has_status( 'completed' ) ) {
        $actions['order-again'] = array(
            'url'  => wp_nonce_url( add_query_arg( 'order_again', $order->id ) , 'woocommerce-order_again' ),
            'name' => __( 'Order Again', 'woocommerce' )
        );
    }
    return $actions;
}
add_filter( 'woocommerce_my_account_my_orders_actions', 'cs_add_order_again_to_my_orders_actions', 50, 2 );

/**
 * Add Place order button in my orders quote-approved actions.
 *
 * @param  array $actions
 * @param  WC_Order $order
 * @return array
 */
function cs_add_place_order_to_my_orders_actions( $actions, $order ) {
    if ( $order->has_status( 'quote-approved' ) ) {
        $actions['place-order'] = array(
            'url'  => wp_nonce_url( add_query_arg( 'order_again', $order->id ) , 'woocommerce-place_order' ),
            'name' => __( 'place order', 'woocommerce' )
        );
    }
    return $actions;
}
add_filter( 'woocommerce_my_account_my_orders_actions', 'cs_add_place_order_to_my_orders_actions', 50, 2 );

但是我的第二个按钮不起作用,我相信是因为这个:

if ( ! function_exists( 'woocommerce_order_again_button' ) ) {

    /**
     * Display an 'order again' button on the view order page.
     *
     * @param object $order Order.
     */
    function woocommerce_order_again_button( $order ) {
        if ( ! $order || ! $order->has_status( apply_filters( 'woocommerce_valid_order_statuses_for_order_again', array( 'completed' ) ) ) || ! is_user_logged_in() ) {
            return;
        }

        wc_get_template( 'order/order-again.php', array(
            'order' => $order,
        ) );
    }
}

在 woocommerce/includes/wc-template-functions.php

所以我想我只需要添加 'quote-approved' 到

woocommerce_valid_order_statuses_for_order_again 

大批

我尝试使用这个:

//Make order again work for Place order , see below

add_filter('woocommerce_valid_order_statuses_for_order_again', function( $statuses ){

    $statuses = wc_get_order_statuses('completed', 'quote-approved');

    return $statuses;

}, 10, 2);

我在这里找到的:Woocommerce - Allowing Order Again for different statuss

但我无法让它工作。有人知道我在做什么错吗?任何帮助将不胜感激。谢谢!

标签: phpwordpresswoocommercehook-woocommerceorders

解决方案


问题来自wc_get_order_statuses()没有参数的函数,只是给出了所有可用订单状态的索引数组。

相反,您只需要以这种方式添加您的自定义订单状态信息

add_filter( 'woocommerce_valid_order_statuses_for_order_again', 'add_custom_status_for_order_again', 20, 1 );
function add_custom_status_for_order_again( $statuses ){
    $statuses[] = 'quote-approved';

    return $statuses;
}

代码位于您的活动子主题(或活动主题)的 function.php 文件中。测试和工作。


推荐阅读