我的 WooCommerce 购物车中有两种不同的产品。一个是门票,另一个是用户上传文件之前必须支付的费用(由我创建的重力表单处理)。
目前,如果我将带有重力表的页面链接放在“已收到订单”页面上,如果有人只购买一张门票,他们会看到此链接,这会让人感到困惑。
有没有办法在购买完成后根据所购买的产品拥有唯一的确认页面?
如果没有,是否存在条件标签或某种 Hook 或过滤器,如果购买“收费产品”(可能基于产品 ID 或类别 ID),则仅在“已收到订单”页面上显示指向重力表的链接?
我确实找到了这个代码片段:
https://sozot.com/how-to-hook-into-woocommerce-to-trigger-something-after-an-order-is-placed/
但是当我尝试这个时:
add_action('woocommerce_payment_complete', 'custom_process_order', 10, 1);
function custom_process_order($order_id) {
$order = new WC_Order( $order_id );
$myuser_id = (int)$order->user_id;
$user_info = get_userdata($myuser_id);
$items = $order->get_items();
foreach ($items as $item) {
if ($item['product_id']==154) {
echo '<h3>If you are submitting a script, please <a href="http://www.ashlandnewplays.org/wp/submit-a-script/step-2/">click here to submit your script</a>.</h3>';
}
}
return $order_id;
}
“订单详细信息”屏幕上没有显示任何内容。奇怪的是,如果我尝试使用 wp_redirect 并强制重定向页面,它“有效”,但它会破坏页面并导致结帐页面中网站的一些奇怪嵌入。
最佳答案
经过不断的努力,我终于找到了我要找的东西,所以我想我会把它发布出来。就我而言,我想自定义订单详细信息/确认页面,其中包含表单链接,但仅限于购买特定产品时。
如果您想要类似的内容,请将其放入 order_details.php 模板中:
global $woocommerce;
$order = new WC_Order( $order_id );
/* This two lines above should already exist, but I have them here so you can
see where to put the code */
foreach($order->get_items() as $item) {
$_product = get_product($item['product_id']);
if ($item['product_id']==154) {
// Do what you want here..replace the product ID with your product ID
}
}
我还通过在 if 语句中插入 wp_redirect 进行了测试,它似乎工作得很好,所以我想您可以使用一堆 if/elseif 语句自定义此代码,这些语句根据购买的产品重定向到不同的登陆页面!希望这对某人有帮助!
关于php - 基于产品或类别的有条件 WooCommerce 确认/订单收到页面?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21460979/