在WooCommerce上,我想为特定类别定制添加到购物车按钮重定向以联系我们页面(当客户点击单个产品页面中的添加到购物车按钮时)。
这是我的代码:
add_filter( 'woocommerce_add_to_cart_redirect', 'rv_redirect_to_url' );
function rv_redirect_to_url() {
global $woocommerce, $post;
$product_id = apply_filters( 'woocommerce_add_to_cart_product_id', absint( $_REQUEST['add-to-cart'] ) );
$rv_woo_redirect_url = get_post_meta( $product_id, '_rv_woo_product_custom_redirect_url', true );
if ( ! empty( $rv_woo_redirect_url ) ) {
wp_redirect( esc_url( $rv_woo_redirect_url ) ); exit;
}
}
如何更改代码以使其仅适用于已定义的产品类别?
答案 0 :(得分:1)
已更新(当定义的产品类别为正确的自定义网址时)
使用附加在 woocommerce_add_to_cart_redirect
过滤器挂钩中的自定义功能,当产品添加到购物车时,将重定向客户(对于已定义的产品类别
add_filter( 'woocommerce_add_to_cart_redirect', 'conditional_add_to_cart_redirection', 99, 1 );
function conditional_add_to_cart_redirection( $url ) {
// ==> HERE define your product category or categories in the array
$category = array( 'clothing', 'music' );
if ( ! isset( $_REQUEST['add-to-cart'] ) ) return $url; // Very important!
// When it's available (on add to cart click), get the product ID
$product_id = absint( $_REQUEST['add-to-cart'] );
// Get the custom url from product post meta data
$custom_url = get_post_meta( $product_id, '_rv_woo_product_custom_redirect_url', true );
// Exit to normal, If the custom URL redirection is not set in the product
if( empty( $custom_url ) ) return $url;
// Custom redirection only for your defined product category(ies)
if( has_term( $category, 'product_cat', $product_id ) ){
// Clear add to cart notice (with the cart link).
wc_clear_notices();
$url = $custom_url; // Updated here
}
return $url;
}
代码放在活动子主题(或主题)的function.php文件中,或者放在任何插件文件中。
代码在Woocommerce 3+上测试并正常工作
添加到购物车重定向将无法使用ajax添加到商店和档案页面上的购物车。因此,您将为商店和档案页面选择选择:
这第二个选项似乎是最好的(因为这只取决于某些产品):
// Conditionally changing add to cart button link and text on shop and archives
add_filter( 'woocommerce_loop_add_to_cart_link', 'replacing_add_to_cart_button', 10, 2 );
function replacing_add_to_cart_button( $button, $product ) {
if( $product->is_type( 'variable-subscription' ) || $product->is_type( 'variable' ) ) return $button;
// ==> HERE define your product category or categories in the array
$category = array( 'clothing', 'music' );
// Check that the custom url from product post meta data is not empty
$custom_url = get_post_meta( $post->ID, '_rv_woo_product_custom_redirect_url', true );
if( empty( $custom_url ) ) return $button;
// Check if the current product has a defined product category(ies)
if( ! has_term( $category, 'product_cat', $post->ID ) ) return $button;
$button_text = __( 'View product', 'woocommerce' );
$button = '<a class="button" href="' . $product->get_permalink() . '">' . $button_text . '</a>';
return $button;
}
代码放在活动子主题(或主题)的function.php文件中,或者放在任何插件文件中。
代码在Woocommerce 3+上测试并正常工作
答案 1 :(得分:0)
如果不打算直接购买产品,那么我会过滤woocommerce_is_purchasable
并将“添加到购物车”按钮替换为您的联系表单的按钮式链接。
function so_46395830_not_purchasable( $is_purchasable, $product ) {
$rv_woo_redirect_url = $product->get_meta( '_rv_woo_product_custom_redirect_url', true );
if ( ! empty( $rv_woo_redirect_url ) ) {
add_action( 'woocommerce_single_product_summary', 'so_46395830_redirect_to_contact' );
}
return $is_purchasable;
}
add_filter( 'woocommerce_is_purchasable', 'so_46395830_not_purchasable', 10, 2 );
function so_46395830_redirect_to_contact( $url ) {
global $product;
$rv_woo_redirect_url = $product->get_meta( '_rv_woo_product_custom_redirect_url', true );
echo sprintf( '<a class="button" href="%s">%s</a>', esc_url( $rv_woo_redirect_url ), __( 'Contact Us for Info', 'your-plugin-textdomain' ) );
}