gpt4 book ai didi

php - woocommerce 添加到单个产品页面上的购物车按钮

转载 作者:行者123 更新时间:2023-12-05 04:09:13 26 4
gpt4 key购买 nike

在 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;

}

}

如何更改我的代码以使其仅适用于定义的产品类别?

最佳答案

已更新(定义产品类别时的正确自定义 url)

使用 Hook 在 woocommerce_add_to_cart_redirect 过滤器 Hook 中的自定义函数,当产品添加到购物车时,它将重定向客户(对于定义的产品类别(是)):

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+ 上测试并有效


Add to cart redirection will not work with ajax add to cart on shop and archives pages. So you will have to choose between that 2 options for shop and archives pages:

  1. 在商店和存档页面(WC 设置 > 产品 > 展示)禁用 ajax 添加到购物车。
  2. 添加以下代码以将添加到购物车按钮替换为链接到产品的按钮:

第二个选项似乎是最好的(因为这仅对某些产品有条件):

// 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+ 上测试并有效

关于php - woocommerce 添加到单个产品页面上的购物车按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46395830/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com