簡體   English   中英

如果用戶未登錄 Woocommerce,請避免將特定產品類別添加到購物車

[英]Avoid add to cart for specific product categories if user is unlogged in Woocommerce

在 Woocommerce 中,我試圖為未登錄的用戶禁用將特定產品類別添加到購物車的功能。最近幾天我正在尋找解決方案,在刪除最后一個代碼后感到沮喪,我發現了這個,但也不做這項工作。

我正在使用某個插件(TZ 產品標簽)在其他頁面上顯示產品(所以不僅在類別和產品頁面上(我知道如何禁用))

add_action( 'woocommerce_after_shop_loop_item', 'remove_add_to_cart_buttons', 1 );

function remove_add_to_cart_buttons() {
    // replace a_category and another_category with the slugs of the categories you'd like to have the button removed from
    if( is_product_category( array( 'gekoelde-bier', 'bierkoerier'))) { 
        remove_action( 'woocommerce_after_shop_loop_item', 'woocommerce_template_loop_add_to_cart' );            
    // add_filter( 'woocommerce_is_purchasable', false );
    }
}

參考自https://gist.github.com/rynaldos/560c621714b9680433cddf18e6a50305

我最好的猜測是在按下“添加到購物車”按鈕時檢查產品的類別,並基於該產品是否可以添加到購物車。

提前致謝。

條件標簽is_product_category()僅針對產品類別存檔頁面。 相反,您可以使用 WordPress 條件函數has_term()

兩種方法可以避免未登錄用戶將特定產品添加到購物車……

1) 使用添加到購物車驗證鈎子:

// Avoid add to cart conditionally
add_filter( 'woocommerce_add_to_cart_validation', 'avoid_add_to_cart_conditionally', 20, 3 );
function avoid_add_to_cart_conditionally( $passed, $product_id, $quantity) {
    // HERE your product categories (can be IDs, slugs or names terms)
    $terms = array( 'gekoelde-bier', 'bierkoerier');
    
    if( has_terms( $terms, 'product_cat', $product_id ) && ! is_user_logged_in() ){
        // Displaying a custom notice (optional)
        wc_add_notice( __('Only logged in users are allowed to purchase this item. Please register.'), 'error' );
        
        $passed = false;
    }
    
    return $passed;
}

代碼位於活動子主題(或活動主題)的 functions.php 文件中。 測試和工作。

在此處輸入圖片說明


2)使用is_purchasable產品屬性(它將刪除添加到購物車按鈕)

add_filter('woocommerce_is_purchasable','conditional_purchasable_products', 20, 2);
function conditional_purchasable_products( $is_purchasable, $product ) {
    // HERE your product categories (can be IDs, slugs or names terms)
    $terms = array( 'gekoelde-bier', 'bierkoerier');
    
    $product_id = $product->get_id(); // The product ID

    if( has_terms( $terms, 'product_cat', $product_id ) && ! is_user_logged_in() ){
        $is_purchasable = false;
    }

    return $is_purchasable;
}

代碼位於活動子主題(或活動主題)的 functions.php 文件中。 測試和工作。


也針對父產品類別術語。

您將使用以下自定義條件函數來替換has_term() Wordpress 函數:

// Custom conditional function that handle parent product categories too
function has_product_categories( $categories, $product_id = 0 ) {
    $parent_term_ids = $categories_ids = array(); // Initializing
    $taxonomy        = 'product_cat';
    $product_id      = $product_id == 0 ? get_the_id() : $product_id;

    if( is_string( $categories ) ) {
        $categories = (array) $categories; // Convert string to array
    }

    // Convert categories term names and slugs to categories term ids
    foreach ( $categories as $category ){
        $result = (array) term_exists( $category, $taxonomy );
        if ( ! empty( $result ) ) {
            $categories_ids[] = reset($result);
        }
    }

    // Loop through the current product category terms to get only parent main category term
    foreach( get_the_terms( $product_id, $taxonomy ) as $term ){
        if( $term->parent > 0 ){
            $parent_term_ids[] = $term->parent; // Set the parent product category
            $parent_term_ids[] = $term->term_id; // (and the child)
        } else {
            $parent_term_ids[] = $term->term_id; // It is the Main category term and we set it.
        }
    }
    return array_intersect( $categories_ids, array_unique($parent_term_ids) ) ? true : false;
}

然后對於兩個掛鈎函數,您將替換以下行:

if( has_terms( $terms, 'product_cat', $product_id ) && ! is_user_logged_in() ){
    

通過這一行:

if( has_product_categories( $terms, $product_id ) && ! is_user_logged_in() ){

代碼位於活動子主題(或活動主題)的 functions.php 文件中。 測試和工作。

woocommerce_is_purchasable過濾器可以完成這項工作。 它檢查產品是否可購買。 如果不可購買,“添加到購物車”按鈕將被移除且無法購買。

因此,在禁用購買功能之前,您需要在此處檢查用戶是否已登錄,以及產品是否屬於某個類別。

你可以這樣做:

function wpso9800_remove_cart_button( $is_purchasable, $product ) {
    //when logged in
    if ( is_user_logged_in() ) {
        return $is_purchasable;
    }

    //get categories
    $categories = get_the_terms( $product->id, 'product_cat');
    $my_terms_ids = array ( 1, 2 );//product category IDs

    if ($categories && !is_wp_error($categories)) {
        foreach ($categories as $cat) {
            if ( in_array($cat->term_id, $my_terms_ids ) ) {
                return false;
            }
            return $is_purchasable;
        }
    }
}
add_filter('woocommerce_is_purchasable','wpso9800_remove_cart_button', 10, 2);

**這是經過測試和工作的。

注意:默認情況下, $is_purchasable設置為true 當您想關閉購買時,您需要返回false

我正在使用選項 #2,因為需要登錄列出的父類別。 我非常小心地准確轉移,但對於我的網站,它給了我死亡的白屏。 我將此代碼直接放在“設置”>“PHP 插入器”下列出的“我的自定義函數”插件中。 任何人都可以看到問題嗎?

add_filter('woocommerce_is_purchasable','conditional_purchasable_products', 20, 2);
function conditional_purchasable_products( $is_purchasable, $product) {
$terms= array( 'memberships', 'our-vendors', 'programs', 'uncategoriezed', 'unpublished-products');
    
$product_id= $product->get_id(); // The product ID

if( has_product_categories ( $categories, $product_id= 0) && ! is_user_logged_in() ){
        $is_purchasable= false;
    }
return $is_purchasable;
}

function has_product_categories ( $categories, $product_id = 0) {
    $parent_term_ids = $categories_ids = array(); 
    $product_id = $product_id == 0 ? get_the_id() : $product_id;
if( is_string( $categories ) ) {
        $categories = (array) $categories; 
foreach ( $categories as $category ){
        $result = (array) term_exists( $category, $taxonomy) ;
        if( ! empty( $result ) ) {
            $categories_ids[] = reset($result);
        }
    }
foreach( get_the_terms( $product_id, $taxonomy) as $term){
        if( $term->parent > 0){
            $parent_term_ids[] = $term->parent;
            $parent_term_ids[] = $term->term_id; 
    } else{
            $parent_term_ids[] = $term->term_id; 
    }
}
    return array_intersect( $categories_ids, array_unique($parent_term_ids) ) ? true: false;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM