如果订单商品属于特定商品类别,则结帐重定向后的Woocommerce - php

我需要这样做,以便当人们在我们的网上商店中下订单时,他们将重定向到“我的帐户”,但前提是类别是XXX,XXX,XXX

但不幸的是我似乎无法正常工作

我尝试使用&& is_product_category('Category x','Category x','Category x')

// REDIRECT AFTER PLACE ORDER BUTTON!
add_action( 'woocommerce_thankyou', 'KSVS_redirect_custom');
function KSVS_redirect_custom( $order_id ){
    $order = new WC_Order( $order_id );

    $url = 'https://kanselvvilselv.dk/min-konto/';

    if ( $order->status != 'failed' ) {
        wp_redirect($url);
        exit;
    }
}

它无需输入&& is_product_category('Category x','Category x','Category x')就可以工作,但是随后却在不应使用的类别上工作。

参考方案

以下代码使用专用的template_redirect挂钩和WordPress has_term()条件函数(与产品类别一起使用),将在结帐后将客户重定向到“我的帐户”部分,当他们的订单包含来自已定义产品类别的商品时:

add_action( 'template_redirect', 'order_received_redirection_to_my_account' );
function order_received_redirection_to_my_account() {
    // Only on "Order received" page
    if( is_wc_endpoint_url('order-received') ) {
        global $wp;

        // HERE below define your product categories in the array
        $categories = array('Tshirts', 'Hoodies', 'Glasses');

        $order = wc_get_order( absint($wp->query_vars['order-received']) ); // Get the Order Object
        $category_found = false;

        // Loop theough order items
        foreach( $order->get_items() as $item ){
            if( has_term( $categories, 'product_cat', $item->get_product_id() ) ) {
                $category_found = true;
                break;
            }
        }

        if( $category_found ) {
            // My account redirection url
            $my_account_redirect_url = get_permalink( get_option('woocommerce_myaccount_page_id') );
            wp_redirect( $my_account_redirect_url );
            exit(); // Always exit
        }
    }
}

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

PHP strtotime困境 - php

有人可以解释为什么这在我的服务器上输出为true吗?date_default_timezone_set('Europe/Bucharest'); var_dump( strtotime('29.03.2015 03:00', time()) === strtotime('29.03.2015 04:00�…

PHP-全局变量的性能和内存问题 - php

假设情况:我在php中运行一个复杂的站点,并且我使用了很多全局变量。我可以将变量存储在现有的全局范围内,例如$_REQUEST['userInfo'],$_REQUEST['foo']和$_REQUEST['bar']等,然后将许多不同的内容放入请求范围内(这将是适当的用法,因为这些数据指的是要求自…

php-casperjs获取内部文本 - php

我正在为casperjs使用php包装器-https://github.com/alwex/php-casperjs我正在网上自动化一些重复的工作,我需要访问一个项目的innerText,但是我尚不清楚如何从casperjs浏览器访问dom。我认为在js中我会var arr = document.querySelector('label.input…

php:拆分字符串,直到第一次出现数字 - php

我有像cream 100G sup 5mg Children 我想在第一次出现数字之前将其拆分。所以结果应该是array( array('cream','100G'), array('sup','5mg Children') ); 可以告诉我如何为此创建图案吗?我试过了list(…

将大字符串分成多个小字符串-PHP - php

我从数据库中获取了一个长字符串,我需要对其进行解析,以使其不包含一个大字符串,而是多个,其中每个字符串都有2个字符。让我们以示例为例:我连接到表,获取此字符串:B1C1F4G6H4I7J1J8L5O6P2Q1R6T5U8V1Z5,之后,我必须对字符串进行解析,因此:B1 C1 F4 G6 H4 I7 J1 J8 L5 O6 P2 Q1 R6 T5 U8 V1…