【发布时间】:2020-11-10 20:08:13
【问题描述】:
我有一个 WooCommerce 商店,其中包含 WooCommerce 品牌和高级自定义字段 (ACF) 插件。
我需要能够通过禁用“添加到购物车”按钮来“关闭”品牌。我不想让它消失,我只想禁用按钮。
我首先使用高级自定义字段创建了一个自定义字段,并将其分配给 WooCommerce Brands 插件使用的 product_brand 自定义分类法。
我的自定义字段 slug 是: close_store
类型: 复选框
选项: 打开(默认值)|关闭
当我去编辑“品牌”时,我可以看到我的自定义字段,当我选择“已关闭”时,我需要它来禁用该特定品牌的“添加到购物车”按钮。
有人可以帮忙吗?我们在下面创建了一些代码,但它还不起作用。
可能的交叉参考: Disabling Add to Cart Button for Specific WooCommerce Products
上面看起来做了类似的事情,但使用“标签”作为结束条件,而不是自定义字段。就函数可能需要如何工作而言,这里可能有一些交叉引用。
可能的帮助 根据 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 插件提供
分类术语变量
<?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; ?>
【问题讨论】:
标签: php wordpress woocommerce advanced-custom-fields taxonomy-terms