gpt4 book ai didi

php - 如果条件满足(购物车中的有限产品太多)Woocommerce 时重定向到页面的自定义功能

转载 作者:行者123 更新时间:2023-12-04 08:04:23 24 4
gpt4 key购买 nike

我为 WooCommerce 制作了一个自定义插件。我试图限制客户可以从特定类别购买的产品数量。大多数都有效,但是一旦满足条件,我就无法让它触发重定向功能。
这是我的代码:

add_action( 'wp', 'check_limited_products_in_cart');

function check_limited_products_in_cart () {
// holds checks for all products in cart to see if they're in our category
$category_checks = array();
$limited_product_count = 1;

// check each cart item for our category
foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {

$product = $cart_item['data'];
$product_in_cat = false;

// replace 'instant-print' with your category's slug
if ( has_term( 'instant-print', 'product_cat', $product->id ) ) {
$product_in_cat = true;
}

array_push( $category_checks, $product_in_cat );
}

$filtered_array = count(array_filter($category_checks));
if ($filtered_array >= $limited_product_count) {
redirect_limited_products_page(); // this bit seems to be failing
}
}

function redirect_limited_products_page () {
$error_page = '/error';
$target_url = $error_page; // construct a target url
wp_redirect($target_url, 301); // permanent redirect
exit();
}
我认为我的第二个功能是抛出太多重定向。该页面正在翻腾,因此我从未看到任何有用的控制台信息。
我已经尝试过各种迭代,比如尝试在一个 block 中运行整个函数(抛出相同的错误),然后提升 $filtered_array作为全局变量,所以我可以从第二个函数访问它,但我也无法让它工作。
非常感谢任何帮助。
编辑
好一点的进步。
我现在回来了:
add_action( 'woocommerce_before_cart', 'check_limited_products_in_cart');

function check_limited_products_in_cart () {
// holds checks for all products in cart to see if they're in our category
$category_checks = array();
$limited_product_count = 1;

// check each cart item for our category
if( ! is_admin() ) {
foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {

$product = $cart_item['data'];
$product_in_cat = false;

// replace 'instant-print' with your category's slug
if ( has_term( 'instant-print', 'product_cat', $product->id ) ) {
$product_in_cat = true;
}

array_push( $category_checks, $product_in_cat );
}
}

$filtered_array = count(array_filter($category_checks));
if ($filtered_array >= $limited_product_count) {
echo '<p>condition met</p>';
redirect_limited_products_page();
}
}

function redirect_limited_products_page () {
echo '<p>Redirect page</p>';
$error_page = '/error';
$target_url = $error_page; // construct a target url
wp_redirect($target_url, 301); // permanent redirect
exit();
}
在我制作一个全局 wp 函数之前,它会在任何地方渲染并可能解释了重定向循环。
所以现在当我将另一个产品添加到购物车时,我看到 condition met以及 Redirect page但出现以下错误:
Warning: Cannot modify header information - headers already sent by (output started at my-site-url\wp-includes\class.wp-styles.php:290) in my-site-url\wp-includes\pluggable.php on line 1296

Warning: Cannot modify header information - headers already sent by (output started at my-site-url\wp-includes\class.wp-styles.php:290) in my-site-url\wp-includes\pluggable.php on line 1299
所以这感觉更接近了。
** 进一步更新 **
在阅读了有关标题的问题后,我尝试整理我的文件,确保没有空格和格式错误的标签。我删除了关闭的 php 标记并检查了我的 linting。我认为主要的罪魁祸首是重定向函数中的 exho 语句引发了标题问题。你可以在这里看到。
enter image description here
我正在检查当前的受限产品数量与篮子中当前受限产品的数量。现在看来重定向永远不会触发,所以我的假设是 add_action( 'woocommerce_before_cart', ...)可能需要 add_filter ('woocommerce_add_to_cart_validation', ...)这似乎也不起作用。即使条件显示为满足并且不会触发重定向,这些项目也会添加到购物车项目中。所以我不确定我是否试图 Hook 错误的功能。
<?php

$error_page = '/product-limit-reached';

add_action( 'woocommerce_before_cart', 'check_limited_products_in_cart');

function check_limited_products_in_cart () {
// holds checks for all products in cart to see if they're in our category
$category_checks = array();
$limited_product_count = 1;

// check each cart item for our category
if( ! is_admin() ) {
foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {

$product = $cart_item['data'];
$product_in_cat = false;

// replace 'instant-print' with your category's slug
if ( has_term( 'instant-print', 'product_cat', $product->id ) ) {
$product_in_cat = true;
}

array_push( $category_checks, $product_in_cat );
}
}

$filtered_array = count(array_filter($category_checks));
if ($filtered_array >= $limited_product_count) {
echo '<p>condition met</p><p>Limited product count: ' . $limited_product_count. '</p><p>Limited products in cart: ' . $filtered_array. '</p>';
redirect_limited_products_page();
}
}

function redirect_limited_products_page () {
$target_url = $error_page; // construct a target url
wp_redirect($target_url, 301); // permanent redirect
// exit();
}

最佳答案

是的,您需要使用 woocommerce_add_to_cart_validation Hook 。
我们可以在将商品添加到购物车时进行检查。如果检查失败,则商品不会进入购物车,并且会向用户显示错误。
就像这样:

function filter_woocommerce_add_to_cart_validation( $true, $product_id, $request_quantity, $variation_id = '', $request_variation = '' ) { 
// holds checks for all products in cart to see if they're in our category
$category_checks = 0;
$limited_product_count = 1;
$limited_slug = 'instant-print'; //replace 'instant-print' with your category's slug

// check each cart item for our category
if( ! is_admin() ) {
foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
$product = $cart_item['data'];

if ( has_term( $limited_slug, 'product_cat', $product->id ) ) {
$category_checks += $cart_item['quantity'];
}
}
}

// We also check the item that the user is trying to add to cart
if ( has_term( $limited_slug, 'product_cat', $product_id ) ) {
$category_checks += $request_quantity;
}

if ($category_checks >= $limited_product_count) {
$notice = '<p>condition met</p><p>Limited product count: ' . $limited_product_count. '</p><p>Limited products in cart: ' . $category_checks. '</p>';
wc_add_notice( __( $notice, 'textdomain' ), 'error' );
return false;
}

return $true;
};

add_filter( 'woocommerce_add_to_cart_validation', 'filter_woocommerce_add_to_cart_validation', 10, 3 );

关于php - 如果条件满足(购物车中的有限产品太多)Woocommerce 时重定向到页面的自定义功能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66301550/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com