禁用 WooCommerce 产品添加到购物车以获得特定的自定义分类术语元值

Disable WooCommerce product add to cart for specific custom taxonomy term meta value

我有一个带有 WooCommerce 品牌和高级自定义字段 (ACF) 插件的 WooCommerce 商店。

我需要能够通过禁用“添加到购物车”按钮来“关闭”品牌。我不想让它消失,我只想禁用按钮。

我首先使用高级自定义字段创建了一个自定义字段,并将其分配给 WooCommerce Brands 插件使用的 product_brand 自定义分类法。

我的自定义字段 slug 是: close_store
类型:复选框
选项:打开(默认值)|关闭

当我去编辑“品牌”时,我可以看到我的自定义字段,当我 select“关闭”时,我需要它来禁用该特定品牌的“添加到购物车”按钮。

有人可以帮忙吗?我们在下面创建了一些代码,但它还不起作用。

可能的交叉引用:

上面看起来做了类似的事情,但使用“标签”作为结束条件而不是自定义字段。就功能可能需要如何工作而言,这里可能有一些交叉参考。

可能的帮助 根据 Brands 插件,他们使用下面的钩子在单个产品页面上输出数据。目前,我的自定义字段不会显示在单个产品页面上。我认为这可能是以下代码可能无法正常工作的原因。

add_action( ‘woocommerce_single_product_summary’

代码如下:

// Custom function to get the brand store status for a product
function get_brand_store_status( $product ) {
    // get the WP_Term object for "product_brand" taxonomy within a product
    $term = wp_get_post_terms( $product->get_id(), 'product_brand' );

    // Return the term meta data for "close_store" metakey
    return get_field( 'close_store', $term_id_prefixed );

// Replace add to cart button by a linked button to the product in Shop and archives pages
add_filter( 'woocommerce_loop_add_to_cart_link', 'replace_loop_add_to_cart_button', 10, 2 );
function replace_loop_add_to_cart_button( $button, $product  ) {
    // Not for variable products, when store is closed
    if( ! $product->is_type( 'variable' ) && 'Closed' === get_brand_store_status( $product ) ) {
        // Button text here
        $button_text = __( "View product", "woocommerce" );

        return '<a class="button" href="' . $product->get_permalink() . '">' . $button_text . '</a>';
    }
    return $button;
}

// Replacing the single product button add to cart by a custom button when store is closed
add_action( 'woocommerce_single_product_summary', 'replace_single_add_to_cart_button', 1 );
function replace_single_add_to_cart_button() {
    global $product;

    // Only when store is closed
    if( 'Closed' === get_brand_store_status( $product ) ) {

        // For variable product types (keeping attribute select fields)
        if( $product->is_type( 'variable' ) ) {
            remove_action( 'woocommerce_single_variation', 'woocommerce_single_variation_add_to_cart_button', 20 );
            add_action( 'woocommerce_single_variation', 'custom_product_button', 20 );
        }
        // For all other product types
        else {
            remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_add_to_cart', 30 );
            add_action( 'woocommerce_single_product_summary', 'custom_product_button', 30 );
        }
    }
}

// The custom replacement button function for single product pages
function custom_product_button(){
    // HERE your custom button text
    $button_text = __( "Not available", "woocommerce" );
    ?>
    <a class="button disabled off" href="#"><?php echo $button_text; ?></a>
    <script>
    jQuery(function($){
        $('a.off').click(function(e){
            e.preventDefault();
        });
    });
    </script>
    <?php
} }

-------------------------------->

更新 - 可能的帮助

由ACF Theme Code Pro Plugin提供

分类术语变量

<?php
// Define taxonomy prefix eg. 'category'
// Use 'term' for all taxonomies
$taxonomy_prefix = 'product_brand';

// Define term ID
// Replace NULL with ID of term to be queried eg '123' 
$term_id = NULL;

// Example: Get the term ID in a term archive template 
// $term_id = get_queried_object_id();

// Define prefixed term ID
$term_id_prefixed = $taxonomy_prefix .'_'. $term_id;
?>



<?php $close_store_checked_values = get_field( 'close_store', $term_id_prefixed ); ?>
<?php if ( $close_store_checked_values ) : ?>
    <?php foreach ( $close_store_checked_values as $close_store_value ): ?>
        <?php echo esc_html( $close_store_value ); ?>
    <?php endforeach; ?>
<?php endif; ?>

使您的代码在 WooCommerce 产品中用于自定义分类术语 using ACF plugin 的附加复选框自定义字段的正确方法是:

// Custom function to get the custom taxonomy term store status for a product
function is_store_closed( $product_id, $taxonomy = 'product_brand' ) {
    $terms = wp_get_post_terms( $product_id, $taxonomy );

    if ( ! empty($terms) ) {
        $term  = reset($terms);
        
        if( is_a($term, 'WP_Term') ) {
            // Gives an array for checkbox or radio button ACF field
            $value = (array) get_field( 'close_store', $term );
            return reset($value) !== 'Open' ? true : false;
        }

    }
    return false;
}

// Replace add to cart button by a linked button to the product in Shop and archives pages
add_filter( 'woocommerce_loop_add_to_cart_link', 'replace_loop_add_to_cart_button', 10, 2 );
function replace_loop_add_to_cart_button( $button, $product  ) {
    // Not for variable products, when store is closed
    if( ! $product->is_type( 'variable' ) && is_store_closed( $product->get_id() ) ) {
        return sprintf( '<a class="button" href="%s">%s</a>', $product->get_permalink(), __( "View product", "woocommerce" ) );
    }
    return $button;
}

// Replacing the single product button add to cart by a custom button when store is closed
add_action( 'woocommerce_single_product_summary', 'replace_single_add_to_cart_button', 1 );
function replace_single_add_to_cart_button() {
    global $product;

    // Only when store is closed
    if( is_store_closed( $product->get_id() ) ) {

        // For variable product types (keeping attribute select fields)
        if( $product->is_type( 'variable' ) ) {
            remove_action( 'woocommerce_single_variation', 'woocommerce_single_variation_add_to_cart_button', 20 );
            add_action( 'woocommerce_single_variation', 'custom_product_button', 20 );
        }
        // For all other product types
        else {
            remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_add_to_cart', 30 );
            add_action( 'woocommerce_single_product_summary', 'custom_product_button', 30 );
        }
    }
}

function custom_product_button(){
    // Display button
    printf( '<a class="button disabled">%s</a>', __( "Not available", "woocommerce" ) );
}

代码进入您的活动子主题(或活动主题)的 function.php 文件。已测试并有效。

A​​CF 文档:Adding fields to a taxonomy term