I have a WooCommerce store with WooCommerce Brands and Advanced Custom Fields (ACF) plugins.
I need to be able to "Close" a brand by disabling the "Add to Cart" button. I don't want it to disappear, I only want to disable the button.
I have started by created a custom field using Advanced Custom Fields and assigned it to product_brand
custom taxonomy used by WooCommerce Brands plugin.
My custom field slug is: close_store
Type: Checkbox
Options: Open (Default value) | Closed
When I go to edit a "Brand" I can see my custom field and when I select "Closed" I need it to then disable the "Add to Cart" buttons for that particular brand.
Is there anyone who can help with this ? We had some code created which is below but it does not work, yet.
Possible Cross Reference: Disabling Add to Cart Button for Specific WooCommerce Products
The above looks to do a similar thing but uses "Labels" as the closing criteria and not a custom field. There may be some cross reference here in terms of how the function may need to work.
Possible Help According to the Brands plugin, they use the below hook to output data on a single product page. At the moment, my custom fields do NOT show on a single product page. I think this could be the reason why the below code may not be working too.
add_action( ‘woocommerce_single_product_summary’
Below is the code:
// 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
} }
--------------------------------->
UPDATE - Possible Help
Provided by ACF Theme Code Pro Plugin
Taxonomy Term Variables
<?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; ?>