Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/wordpress/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 基于产品类别拒绝Woocommerce中特定购物车项目的签出_Php_Wordpress_Woocommerce_Cart_Categories - Fatal编程技术网

Php 基于产品类别拒绝Woocommerce中特定购物车项目的签出

Php 基于产品类别拒绝Woocommerce中特定购物车项目的签出,php,wordpress,woocommerce,cart,categories,Php,Wordpress,Woocommerce,Cart,Categories,基于此,我尝试制作自己的代码示例,该示例呈现通知,并在购物车停止时阻止结帐 仅包含特定类别的产品。它致力于预防和错误通知,但在添加其他产品时,它仍然拒绝签出 /** * Renders a notice and prevents checkout if the cart * only contains products in a specific category */ //add_action( 'woocommerce_check_cart_items', 'brown_wc_prev

基于此,我尝试制作自己的代码示例,该示例呈现通知,并在购物车停止时阻止结帐 仅包含特定类别的产品。它致力于预防和错误通知,但在添加其他产品时,它仍然拒绝签出

/**
 * Renders a notice and prevents checkout if the cart
 * only contains products in a specific category
 */
//add_action( 'woocommerce_check_cart_items', 'brown_wc_prevent_checkout_for_category' );

function brown_wc_prevent_checkout_for_category() {

    // set the slug of the category for which we disallow checkout
    $categories = array('drinks','extra-accompaniments');

    foreach ($categories as $category => $value) {
        // 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;
        }

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

            // render a notice to explain why checkout is blocked
            wc_add_notice( sprintf( 'Please choose atleast one bento.', $category ), 'error' );
        }
    }

}

/**
 * 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 brown_wc_is_category_alone_in_cart( $category ) {

    //When the cart is empty, remove warning message that I have set for when the snippet takes effect
    if (!WC()->cart->is_empty()) {
        // All the code
        // 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;

    } else {

        return false;   //  Assume you'd want false here, since the cart is empty

    }

}

代码中存在一些错误,可以避免代码成功运行。您的代码和要求与我的完全不同

您必须为通知设置所需的按钮URL和文本,如“便当”产品类别链接和按钮名称

代码如下:

// Conditional function that check if the non mandatory product categories are in all cart items
function has_not_mandatory_category(){
    // DEFINE HERE the your non mandatory product categories (Ids, slugs or names)
    $categories = array('drinks','extra-accompaniments');

    // Iterrating each item in cart and detecting…
    foreach ( WC()->cart->get_cart() as $cart_item ) {
        $product_id = $cart_item['product_id']; // <== This is the right way (working for product variations too)
        if ( ! has_term( $categories, 'product_cat', $product_id ) )
            return false;
    }
    return true;
}

// Display a message and prevent checkout if the non mandatory product categories are not in all cart items
add_action( 'woocommerce_check_cart_items', 'prevent_checkout_display_notice' ); // for cart and checkout
function prevent_checkout_display_notice() {
    // DEFINE HERE the return button URL and title
    $button_url = '#';
    $button_title = 'Go there';

    // Display message if the non mandatory product categories are not in all cart items
    if ( has_not_mandatory_category() )
        wc_add_notice(
            sprintf( __( 'Please choose at least one bento. <a class="button" href="%s">%s</a>', 'your_theme_domain'),
                $button_url,
                $button_title
            ), 'error'
        );
}
//检查所有购物车项目中是否存在非强制性产品类别的条件函数
函数具有\u非\u必需的\u类别(){
//在此定义您的非强制性产品类别(ID、SLUG或名称)
$categories=数组(“饮料”、“额外伴奏”);
//在购物车中安排每个项目并检测…
foreach(WC()->cart->get_cart()作为$cart_项目){

$product\U id=$cart\U item['product\U id'];//非常感谢。这非常有效,代码更精简。Ps;我拒绝签出的项目比允许签出的项目少。