mirror of
https://gh.wpcy.net/https://github.com/mainwp/Code-Snippets-Functions.git
synced 2026-04-29 11:32:21 +08:00
https://stackoverflow.com/questions/78515982/replace-loop-add-to-cart-button-with-a-button-linked-to-the-product-and-differen/78516367
42 lines
1.6 KiB
Text
42 lines
1.6 KiB
Text
// Settings function: Define for each categories, the corresponding button text to display
|
|
function get_category_button_text() {
|
|
return array(
|
|
array(
|
|
'terms' => array('hoodies', 'pants'), // Accept multiple categories term Ids, term slugs or term names
|
|
'text' => __('Button Text 1', 'woocommerce') // Button text
|
|
),
|
|
array(
|
|
'terms' => array('tshirts', 'shirts'),
|
|
'text' => __('Button Text 2', 'woocommerce')
|
|
),
|
|
array(
|
|
'terms' => array('accessories'),
|
|
'text' => __('Button Text 3', 'woocommerce')
|
|
),
|
|
array(
|
|
'terms' => array('music', 'video'),
|
|
'text' => __('Button Text 4', 'woocommerce')
|
|
),
|
|
array(
|
|
'terms' => array('furniture'),
|
|
'text' => __('Button Text 5', 'woocommerce')
|
|
),
|
|
);
|
|
}
|
|
|
|
// Replace Loop Add to Cart with a button linked to the product
|
|
add_filter( 'woocommerce_loop_add_to_cart_link', 'replacing_add_to_cart_button', 10, 2 );
|
|
function replacing_add_to_cart_button( $button, $product ) {
|
|
$taxonomy = 'product_cat';
|
|
$product_id = $product->get_id();
|
|
$button_text = __('View product', 'woocommerce'); // The default text
|
|
|
|
// Loop through defined category terms / text pairs
|
|
foreach( get_category_button_text() as $values ) {
|
|
if ( has_term($values['terms'], $taxonomy, $product_id ) ) {
|
|
$button_text = $values['text'];
|
|
break;
|
|
}
|
|
}
|
|
return sprintf('<a href="%s" class="button addtocartbutton">%s</a>', $product->get_permalink(), $button_text );
|
|
}
|