【问题标题】:Custom add to cart button URL for specific product in WooCommerce?WooCommerce 中特定产品的自定义添加到购物车按钮 URL?
【发布时间】:2022-04-27 04:15:16
【问题描述】:

我希望用户在单击“添加到购物车”按钮时被定向到某个 URL。目前我有这个:

/**
 * Set a custom add to cart URL to redirect to
 * @return string
 */
  function custom_add_to_cart_redirect() { 
return 'http://www.yourdomain.com/your-page/'; 
}
add_filter( 'woocommerce_add_to_cart_redirect', 
'custom_add_to_cart_redirect' );

但是,这会将所有添加到购物车的按钮重定向,我怎样才能使 URL 特定于产品?谢谢

【问题讨论】:

    标签: php wordpress woocommerce cart hook-woocommerce


    【解决方案1】:
    add_filter( 'woocommerce_add_to_cart_redirect', 'productbase_redirect_on_add_to_cart' );
    function productbase_redirect_on_add_to_cart() {
    
        //Get product ID
        if ( isset( $_POST['add-to-cart'] ) ) {
            $product_id = (int) $_POST['add-to-cart'] ;             
        }
        // set redirect with product id here
    
    }
    

    【讨论】:

      【解决方案2】:

      当满足这些特定条件时,您需要使用一些条件逻辑来更改返回的 url。当添加到购物车的产品 ID 为 999 时,以下将更改 url。

      add_filter( 'woocommerce_add_to_cart_redirect', 'custom_add_to_cart_redirect' );
      
      function custom_add_to_cart_redirect( $url ) {
      
          // The product ID you want a specific redirect for
          $matched_id = 999; 
      
          // If product ID matches, modify redirect
          if ( isset( $_POST['add-to-cart'] ) ) && $matched_id = (int) $_POST['add-to-cart'] ) {            
            $url = 'http://www.yourdomain.com/your-page/'; 
          }
          // always return a value
          return $url;
      
      }
      

      【讨论】:

        猜你喜欢
        • 2020-09-01
        • 2019-12-13
        • 2022-01-03
        • 2015-12-04
        • 2018-02-10
        • 2019-07-16
        • 2021-06-10
        • 2015-05-27
        • 2023-04-10
        相关资源
        最近更新 更多