当ACF对WooCommerce产品档案有价值时,更改添加到购物车按钮



当产品在WooCommerce产品档案中具有ACF(高级自定义字段(值时,我正试图更改"添加到购物车"按钮。例如,如果我的名为mix_and_match_enabled的字段被打开;查看选项";并链接到产品页面而不是默认的";添加到购物车";按钮

以下是一些示例代码,我认为这是可以做到的,但很可能是错误的。

add_filter( 'woocommerce_loop_add_to_cart_link', 'replace_default_button', 10, 2 );
function replace_default_button( $button, $product ){
$mix_and_match = get_post_meta($post_id, 'mix_and_match_enabled', true);
if ( $mix_and_match ){
$button = '<a href="#" class="button alt">' . __( "View options", "woocommerce" ) . '</a>';
}
return $button;
}

如有任何帮助,我们将不胜感激。谢谢

我相信我已经解决了我的问题。我已将解决方案粘贴在下面。请随时改进此代码。

add_filter( 'woocommerce_loop_add_to_cart_link', 'replace_default_button', 10, 2 );
function replace_default_button( $button, $product ){
$mix_and_match = get_post_meta(get_the_ID(), 'mix_and_match_enabled', true);
if ( $mix_and_match == 1 ){
$button = '<a href="' . get_permalink($product_id) . ' " class="button alt">' . __( "View options", "woocommerce" ) . '</a>';
}
return $button;
}

最新更新