感谢页面重定向以获取WooCommerce订单商品中的特定产品ID

马西奥·维耶拉

我的产品之一需要一个特定的“谢谢”页面。

这是我的代码。产品ID为1813,类别为gimnasio-mental我确实不需要,也不想在此代码中包括类别,因此,如果可以简化它,甚至更好!

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( 1813 ); // or an empty array if not used
    // Define the product categories (can be IDs, slugs or names)
    $product_categories = array( 'gimnasio-mental' ); // 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( '/gracias-gimnasio-mental/' ) );
        exit;
    }
}
7uc1f3r

您可以使用woocommerce_thankyou动作挂钩template_redirect

通过代码中添加的注释标签进行说明

function action_woocommerce_thankyou( $order_id ) {
    if( ! $order_id ) {
        return;
    }

    // Instannce of the WC_Order Object
    $order = wc_get_order( $order_id ); 

    // Is a WC_Order
    if ( is_a( $order, 'WC_Order' ) ) {
        // False
        $redirection = false;
        
        // Loop through order items
        foreach ( $order->get_items() as $item_key => $item ) {
            // Product ID(s)
            $product_ids = array( $item->get_product_id(), $item->get_variation_id() );
            
            // Product ID in array
            if ( in_array( 1813, $product_ids ) ) {
                $redirection = true;
            }
        }
    }

    // Make the custom redirection when a targeted product has been found in the order
    if ( $redirection ) {
        wp_safe_redirect( home_url( '/gracias-gimnasio-mental/' ) );
        exit;
    }
}
add_action( 'woocommerce_thankyou', 'action_woocommerce_thankyou', 10, 1 );

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

回显特定产品ID的woocommerce库存数量

来自分类Dev

在WooCommerce订单管理页面中按订单商品SKU或ID搜索

来自分类Dev

在Woocommerce单个产品页面中显示特定产品标签的自定义内容

来自分类Dev

重定向联系表格以感谢页面

来自分类Dev

WooCommerce在“谢谢”页面中获取订单数量并重定向

来自分类Dev

检查用户是否在WooCommerce中购买了特定产品

来自分类Dev

WooCommerce为特定产品ID添加到购物车的自定义重定向

来自分类Dev

当WooCommerce产品搜索返回“找不到产品”时,重定向到特定页面

来自分类Dev

Woocommerce REST API排除订单端点中的特定产品

来自分类Dev

在WooCommerce结帐中制作特定产品类别所需的订单注释

来自分类Dev

WooCommerce自定义缺货文本+特定产品ID

来自分类Dev

禁用Woocommerce购物车结帐和订单中特定产品的商品名称链接

来自分类Dev

WooCommerce中特定产品的自动更改订单状态为已完成

来自分类Dev

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

来自分类Dev

在WooCommerce中为带有特定产品属性术语的变体制作必填订单注释字段

来自分类Dev

在Woocommerce中每天获取按产品ID进行的订单计数

来自分类Dev

避免只针对特定产品的WooCommerce新订单通知

来自分类Dev

根据WooCommerce订单付款中的特定产品标签限制付款网关

来自分类Dev

在WooCommerce中创建订单时获取订单商品的库存状态

来自分类Dev

在WooCommerce结帐中更改特定产品的付款方式标题

来自分类Dev

请参考WordPress中的特定产品ID / PHP中的WooCommerce

来自分类Dev

获取具有特定产品的magento扩展中的订单列表

来自分类Dev

WooCommerce获取具有给定产品ID的产品属性

来自分类Dev

WooCommerce在“谢谢”页面中获取订单数量并重定向

来自分类Dev

在 Woocommerce 产品页面中隐藏特定产品标签的按钮

来自分类Dev

在 Woocommerce 的自动完成订单流程中排除特定产品

来自分类Dev

限制用户在woocommerce中再次购买特定产品

来自分类Dev

从所有产品中获取 Woocommerce 中特定产品属性的术语名称数组

来自分类Dev

对于 WooCommerce 订单上具有已完成状态的特定产品,请从特定自定义页面更改信息

Related 相关文章

  1. 1

    回显特定产品ID的woocommerce库存数量

  2. 2

    在WooCommerce订单管理页面中按订单商品SKU或ID搜索

  3. 3

    在Woocommerce单个产品页面中显示特定产品标签的自定义内容

  4. 4

    重定向联系表格以感谢页面

  5. 5

    WooCommerce在“谢谢”页面中获取订单数量并重定向

  6. 6

    检查用户是否在WooCommerce中购买了特定产品

  7. 7

    WooCommerce为特定产品ID添加到购物车的自定义重定向

  8. 8

    当WooCommerce产品搜索返回“找不到产品”时,重定向到特定页面

  9. 9

    Woocommerce REST API排除订单端点中的特定产品

  10. 10

    在WooCommerce结帐中制作特定产品类别所需的订单注释

  11. 11

    WooCommerce自定义缺货文本+特定产品ID

  12. 12

    禁用Woocommerce购物车结帐和订单中特定产品的商品名称链接

  13. 13

    WooCommerce中特定产品的自动更改订单状态为已完成

  14. 14

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

  15. 15

    在WooCommerce中为带有特定产品属性术语的变体制作必填订单注释字段

  16. 16

    在Woocommerce中每天获取按产品ID进行的订单计数

  17. 17

    避免只针对特定产品的WooCommerce新订单通知

  18. 18

    根据WooCommerce订单付款中的特定产品标签限制付款网关

  19. 19

    在WooCommerce中创建订单时获取订单商品的库存状态

  20. 20

    在WooCommerce结帐中更改特定产品的付款方式标题

  21. 21

    请参考WordPress中的特定产品ID / PHP中的WooCommerce

  22. 22

    获取具有特定产品的magento扩展中的订单列表

  23. 23

    WooCommerce获取具有给定产品ID的产品属性

  24. 24

    WooCommerce在“谢谢”页面中获取订单数量并重定向

  25. 25

    在 Woocommerce 产品页面中隐藏特定产品标签的按钮

  26. 26

    在 Woocommerce 的自动完成订单流程中排除特定产品

  27. 27

    限制用户在woocommerce中再次购买特定产品

  28. 28

    从所有产品中获取 Woocommerce 中特定产品属性的术语名称数组

  29. 29

    对于 WooCommerce 订单上具有已完成状态的特定产品,请从特定自定义页面更改信息

热门标签

归档