2017-09-24 241 views
1

在WooCommerce上,我希望自定義添加到購物車按鈕重定向,以便與特定類別聯繫我們頁面(當客戶點擊單品頁面中的添加購物車按鈕時)。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

您的代碼與您希望執行的操作相比,目前正在執行什麼操作?此外,我認爲*產品添加到購物車後發生重定向。你是否還希望將它添加到購物車中?這似乎與您重定向聯繫頁面的願望不符。 – helgatheviking

+0

@helgatheviking不,我不想將它添加到購物車,這將是一個特殊的訂單 –

+0

在這種情況下,我認爲重定向是錯誤的方法。 – helgatheviking

回答

1

更新

使用自定義功能在woocommerce_add_to_cart_redirect過濾鉤子鉤住(當它是一個定義的產品類別右邊的自定義網址),當一個產品被添加到購物車(用於定義將重定向客戶產品類別(IES)

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+測試和工程


添加到購物車重定向不會與阿賈克斯的工作添加到購物車在商店和檔案網頁。所以,你將有那2個選項的商店和檔案頁面之間進行選擇:

  1. 禁用添加AJAX到購物車在店,檔案頁面(WC設置>產品>顯示)。
  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+測試和工程

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')); 
} 
相關問題