【问题标题】:woocommerce add to cart button on single product pagewoocommerce 在单个产品页面上添加到购物车按钮
【发布时间】:2018-03-05 20:52:21
【问题描述】:

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

    }

}

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

【问题讨论】:

  • 与您想要的相比,您的代码当前在做什么?另外,我认为重定向发生在产品添加到购物车之后。您还希望将其添加到购物车吗?这似乎与您重定向到联系页面的愿望不一致。
  • @helgatheviking 不,我不想将它添加到购物车中,这将是一个特殊订单
  • 在这种情况下,我认为重定向是错误的方法。
  • @YaraJoe 我已经更新了我的代码,你的自定义 url 有一个小错误……现在自定义重定向可以工作了。阅读您的 cmets,您可以使用此答案代码:Customize “Add to cart” button for a specific product category in WooCommerce

标签: php wordpress woocommerce categories product


【解决方案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 添加到购物车。因此,对于商店和档案页面,您将有在这 2 个选项之间进行选择

  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+ 上经过测试并且可以运行

【讨论】:

    【解决方案2】:

    如果该产品不打算直接购买,那么我将过滤 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' ) );
    }
    

    【讨论】:

      猜你喜欢
      • 2021-07-11
      • 2020-11-15
      • 2015-01-15
      • 2018-08-08
      • 2014-11-11
      • 2021-06-09
      • 1970-01-01
      • 1970-01-01
      • 2021-10-04
      相关资源
      最近更新 更多