php - 仅当强制类别的产品在购物车中时才允许结帐

标签 php wordpress woocommerce categories cart

如果客户的购物车中没有特定的产品类别,我想阻止他们进行结帐。我还想用错误信息告诉他们,他们需要添加某个产品。我找到了一些代码,但无法正常工作。我已将它作为代码片段添加到我的 Wordpress 安装中,但遗憾的是它不起作用并且即使我已打开调试也没有错误消息。这是我在 Github 中找到的代码,可能需要修改才能使其正常工作:

function sv_wc_prevent_checkout_for_category() {

    // set the slug of the category for which we disallow checkout
    $category = 'sibling';

    // get the product category
    $product_cat = get_term_by( 'slug', $category, 'product_cat' );

    // sanity check to prevent fatals if the term doesn't exist
    if ( is_wp_error( $product_cat ) ) {
        return;
    }

    $category_name = '<a href="' . get_term_link( $category, 'product_cat' ) . '">' . $product_cat->name . '</a>';

    // check if this category is the only thing in the cart
    if ( sv_wc_is_category_alone_in_cart( $category ) ) {

        // render a notice to explain why checkout is blocked
        wc_add_notice( sprintf( 'Hi there! Looks like your cart only contains products from the %1$s category &ndash; you must purchase a product from another category to check out.', $category_name ), 'error' );
    }
}
add_action( 'woocommerce_check_cart_items', 'sv_wc_prevent_checkout_for_category' );

/**
 * Checks if a cart contains exclusively products in a given category
 * 
 * @param string $category the slug of the product category
 * @return bool - true if the cart only contains the given category
 */
function sv_wc_is_category_alone_in_cart( $category ) {

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

        // if a product is not in our category, bail out since we know the category is not alone
        if ( ! has_term( $category, 'product_cat', $cart_item['data']->id ) ) {
            return false;
        }
    }

    // if we're here, all items in the cart are in our category
    return true;
}

因此,如果“同级”类别是购物车中的唯一商品,我希望停止结帐(带有错误消息)。我有一个“标准”类别,该类别必须在客户结账前放在购物篮中。希望这是有道理的。

最佳答案

在这里,您有一个可以解决问题的解决方案。特别有两个主要功能(最后一个):

  1. 第一个功能 (N°3) 在购物车中有商品但不是必需的商品类别时在购物车页面上显示您的消息。还在强制性产品存档页面上显示消息(当客户从结帐重定向时很有用,见下文)
  2. 第二个功能(N°4)在客户尝试结帐时将客户重定向到产品强制类别存档页面,并且他的购物车没有缺少强制产品类别。

Define before your mandatory category slug in your_mandatory_category_slug() function.

这是代码:

// Function that define the mandatory product category
 function your_mandatory_category_slug(){

     // DEFINE HERE the SLUG of the needed product category
    $category = 'clothing';
    return $category;
 }


// Conditional function that returns true if the mandatory product category is in cart
function has_mandatory_category(){
    $category_needed = your_mandatory_category_slug();
    $has_cat = false;

    // Iterrating each item in cart and detecting…
    foreach ( WC()->cart->get_cart() as $item ) {

        // Detects if the needed product category is in cart items
        if ( has_term($category_needed, 'product_cat', $item['product_id'] ) ) {
            $has_cat = true;
            break;
        }
    }
    return $has_cat;
 }


// Function that display a message if there is not in cart a mandatory product category
function mandatory_category_display_message() {
        $category_needed = your_mandatory_category_slug();

    // check that cart is not empty (for cart and product category archives)
    if( !WC()->cart->is_empty() && ( is_cart() || is_product_category( $category_needed ) ) ){
        $category_obj = get_term_by( 'slug', $category_needed, 'product_cat' );
        if ( is_wp_error( $category_obj ) ) return;

        // Display message when product category is not in cart items
        if ( !has_mandatory_category() ) {
            $category_name = $category_obj->name;
            $category_url = get_term_link( $category_needed, 'product_cat' );

            // render a notice to explain why checkout is blocked
            wc_add_notice( sprintf( __( '<strong>Reminder:</strong> You have to add in your cart, a product from "%1$s" category, to be allowed to check out. Please return <a href="%2$s"> here to "%1$s" product page</a>', 'your_theme_domain'), $category_name, $category_url ), 'error' );
        }
    }
}
add_action( 'woocommerce_before_main_content', 'mandatory_category_display_message', 30 ); // for product mandatory category archives pages
add_action( 'woocommerce_check_cart_items', 'mandatory_category_display_message' ); // for cat page


// Function that redirect from checkout to mandatory product category archives pages
function mandatory_category_checkout_redirect() {

    // If cart is not empty on checkout page
    if( !WC()->cart->is_empty() && is_checkout() ){
        $category_needed = your_mandatory_category_slug();

        // If missing product category => redirect to the products category page
        if ( !has_mandatory_category() )
            wp_redirect( get_term_link( $category_needed, 'product_cat' ) );
    }
}
add_action('template_redirect', 'mandatory_category_checkout_redirect');

这会出现在您的事件子主题(或主题)的 function.php 文件中或任何插件文件中。

此代码已经过测试且功能齐全。

关于php - 仅当强制类别的产品在购物车中时才允许结帐,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39644645/

相关文章:

php - 有多个地址的客户

php - 在 wordpress 主题的标题中替换 php if else 以显示图像和文本而不是 or

javascript - Woocommerce 在感谢页面上获取订单并传递数据 javascript 代码段

php - 当注册客户尚未在 WooCommerce 中购买时显示自定义文本

php - 组织编程竞赛 - 如何编译和执行

php - MYSQL 多重计数和求和

php - 在 laravel 中运行测试时无法找到 [App\Models\User] 的工厂

php - WordPress 侧边栏小部件在更新 MySQL 数据库中的链接时被禁用

php - Woocommerce 多个结账页面

php - 在 WooCommerce 管理产品列表中仅显示登录作者的产品