2017-09-24 2 views
1

WooCommerce에서 특정 카테고리에 대한 사용자 정의 장바구니 버튼 리디렉션을 통해 당사에 연락 할 수 있습니다 (고객이 단일 제품 페이지의 장바구니 추가 버튼을 클릭 할 때).woocommerce woocommerce wooocommerce 단일 제품 페이지의 버튼

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 필터 후크에 걸려 (가 정의 된 제품 카테고리가있어 올바른 사용자 정의 URL) 제품 카테고리 (들))는 :

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 Fi를 간다 (또는 테마) 또는 모든 플러그인 파일에 포함되어 있습니다.

코드 Woocommerce 3+ 테스트 및


는 아약스와 함께 작동 상점 및 아카이브 페이지에서 장바구니에 추가하지 않습니다 카트 재 지정에 담기 작동합니다. 그래서 당신은 상점 아카이브 페이지가 2 개 옵션 사이에서 선택을 이있을 것이다 :

  1. 안 아약스 추가 - 투 - 카트 상점 및 아카이브 페이지 (WC 설정> 제품> 디스플레이).

이 두번째 옵션은 (이 일부 제품에 조건부로) 가장 좋은 것 같다 :

  • 는 제품에 연결된 버튼, 장바구니 버튼에 추가를 교체하려면 다음 코드를 추가

    // 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+ 테스트 및 제품이 크게 구입하는 것은 아닙니다 경우 내가 woocommerce_is_purchasable를 필터링하고 대체 할 것이다,

  • 0

    작동합니다 추가 형태.

    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')); 
    } 
    
    관련 문제