【问题标题】:Conditional WooCommerce Order Received text on based on Product or Category?有条件的 WooCommerce 订单收到基于产品或类别的文本?
【发布时间】:2018-06-25 11:38:25
【问题描述】:

对于 Woocommerce,我找到了这段代码,但我需要以产品 ID 为条件:

add_action( 'woocommerce_thankyou', 'custom_thankyou_text', 1, 0);
function custom_thankyou_text(){
    echo '<p class="thankyou-custom-text">If you do not see the download button(s) below, please refresh the page. Processing can take a few minutes.</p>';
}

如何根据他们购买的产品显示特定的 WooCommerce 感谢页面文本?

我还找到了适合的这个条件示例(没有重定向,我不需要):

add_action( 'template_redirect', 'wc_custom_redirect_after_purchase' );
function wc_custom_redirect_after_purchase() {
    if ( ! is_wc_endpoint_url( 'order-received' ) ) return;

    // Define the product IDs in this array
    $product_ids = array( 37, 25, 50 ); // or an empty array if not used
    // Define the product categories (can be IDs, slugs or names)
    $product_categories = array( 'clothing' ); // or an empty array if not used
    $redirection = false;

    global $wp;
    $order_id =  intval( str_replace( 'checkout/order-received/', '', $wp->request ) ); // Order ID
    $order = wc_get_order( $order_id ); // Get an instance of the WC_Order Object

    // Iterating through order items and finding targeted products
    foreach( $order->get_items() as $item ){
        if( in_array( $item->get_product_id(), $product_ids ) || has_term( $product_categories, 'product_cat', $item->get_product_id() ) ) {
            $redirection = true;
            break;
        }
    }

    // Make the custom redirection when a targeted product has been found in the order
    if( $redirection ){
        wp_redirect( home_url( '/your-page/' ) );
        exit;
    }
}

有没有办法将两者结合起来以获得所需的结果?

【问题讨论】:

    标签: php wordpress woocommerce categories product


    【解决方案1】:

    这可以在下面的示例中轻松完成,您必须在其中定义目标产品 ID 和产品类别(用于测试)。所以这个例子会显示一条自定义消息:

    • 特定产品 ID
    • 针对特定产品类别
    • 对于所有其他情况

    代码:

    add_action( 'woocommerce_thankyou', 'custom_thankyou_text', 1, 1);
    function custom_thankyou_text( $order_id ){
        // HERE Define Your product IDs below
        $product_id1 = 30;
        $product_id2 = 40;
    
        // HERE Define Your product category (ID, slug or name)
        $category = array('clothing');
    
        // Get the WC_Order object (an instance)
        $order = wc_get_order( $order_id );
        $product_ids = array();
        $has_category = false;
    
        // Loop through the order items
        foreach( $order->get_items() as $item ){
            // PRODUCT ID: Store the product ID in an array
            $product_ids[] = $item->get_product_id(); 
    
            // PRODUCT CATEGORY
            if( has_term( $category, 'product_cat', $item->get_product_id() ) )
                $has_category = true;
        }
        // For first product ID 
        if( in_array( $product_id1, $product_ids ) ){
            echo '<p class="thankyou-custom-text">Custom message for Product ID .'.$product_id1.'</p>';
        }
        // For Second product ID 
        elseif( in_array( $product_id2, $product_ids ) ){
            echo '<p class="thankyou-custom-text">Custom message for Product ID .'.$product_id1.'</p>';
        }
        // For product category 
        elseif( $has_category ){
            echo '<p class="thankyou-custom-text">Custom message for Product Category.</p>';
        } 
        // For all other cases
        else {
            echo '<p class="thankyou-custom-text">If you do not see the download button(s) below, please refresh the page. Processing can take a few minutes.</p>';
        }
    }
    

    代码进入活动子主题(或活动主题)的 function.php 文件中。

    经过测试并且可以工作

    【讨论】:

    • 效果很好。非常感谢。
    • 完成,抱歉不知道我必须这样做:)。我读到它是关于设置优先级的,但不确定应该是什么。我的文字如下所示link。如何将其向上移动,如屏幕截图所示?谢谢@LoicTheAztec
    • @Radu 更新了我的答案……只需在add_action() 函数(第一行代码)中将10 替换为1……就可以了。
    猜你喜欢
    • 1970-01-01
    • 2021-04-02
    • 2015-07-16
    • 1970-01-01
    • 2022-01-02
    • 2021-05-19
    • 1970-01-01
    • 2017-01-28
    • 2016-07-05
    相关资源
    最近更新 更多