programing

WooCommerce 3의 상점 페이지에 있는 상품 페이지에 더 연결된 읽기로 카트에 추가하기 버튼 바꾸기

stoneblock 2023. 10. 16. 21:28

WooCommerce 3의 상점 페이지에 있는 상품 페이지에 더 연결된 읽기로 카트에 추가하기 버튼 바꾸기

저는 우커머스를 사용하고 있는데 다음과 같은 문제가 있습니다.

  • 상품들은 홈페이지에 가격과 함께 진열되어 있고 카트에 추가 버튼이 있습니다.
  • 카트에 추가 버튼은 카트 페이지로 리디렉션됩니다.
  • 각 제품의 이미지가 제품 페이지로 리디렉션됩니다.

중요한 것은 카트에 제품을 추가하기 전에 고객이 제품에 대한 설명을 읽을 수 있도록 하는 것입니다.

홈페이지에서 카트에 추가 버튼이 나타날 각 제품의 페이지로 리디렉션하기 위해 카트에 추가 버튼을 더 읽기로 대체할 수 있는 방법이 있습니까?

장바구니에 추가하기 버튼을 교체하면 wocommerce 3+에 대한 제품 및 아카이브 페이지에 대한 링크가 나타납니다.

add_filter( 'woocommerce_loop_add_to_cart_link', 'replacing_add_to_cart_button', 10, 2 );
function replacing_add_to_cart_button( $button, $product  ) {
    $button_text = __("View product", "woocommerce");
    $button = '<a class="button" href="' . $product->get_permalink() . '">' . $button_text . '</a>';

    return $button;
}

코드가 작동합니다.활성 하위 테마(또는 테마)의 php 파일 또는 플러그인 파일에 있습니다.

이 코드는 WooCommerce 3+에서 테스트되어 작동합니다.버튼 텍스트를 사용자 정의할 수 있으며 다음과 같은 것을 얻을 수 있습니다.

enter image description here

이 코드는 작동중입니다.하지만 내 주제를 위해 이 장소에서.woocommerce/include/class-wc-product-simple.php

// changes the "select options" text. Forget who to give credit to for this.
add_filter( 'woocommerce_product_add_to_cart_text', function( $text ) {
    global $product;
    if ( $product->is_type( 'variable' ) ) {
        $text = $product->is_purchasable() ? __( 'More Options', 'woocommerce' ) : __( 'Read more', 'woocommerce' );
    }
    return $text;
}, 10 );

/**
 * remove add to cart buttons on shop archive page
 */

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( 'simple' ) ) {
        $button_text = __("View product", "woocommerce");
        $button = '<a class="button" href="' . $product->get_permalink() . '">' .
        $button_text . '</a>';
    }
    return $button;
}

기능에 추가합니다.테마 폴더의 php 파일

remove_action( 'woocommerce_after_shop_loop_item', 'woocommerce_template_loop_add_to_cart');
remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_add_to_cart');

도움이 될 경우를 대비해, 가변 제품은 어차피 아카이브 페이지에 "카트에 추가"되지 않기 때문에, 이 솔루션을 간단한 제품에만 적용할 수 있었습니다.이렇게 하면 제품에 더 많은 옵션이 있다는 것이 더 명확해질 수 있습니다(변수가변적인 경우).또한 아래 예제에서 "선택 옵션"의 텍스트를 "더 많은 옵션"으로 변경했습니다(단일 제품 페이지의 url을 보고도 모든 제품을 구매할 수 있는 것은 아니기 때문에 이 스레드에 대한 또 다른 비주제적인 아이디어입니다).

// changes the "select options" text. Forget who to give credit to for this.
add_filter( 'woocommerce_product_add_to_cart_text', function( $text ) {
global $product;
if ( $product->is_type( 'variable' ) ) {
    $text = $product->is_purchasable() ? __( 'More Options', 'woocommerce' ) : __( 'Read more', 'woocommerce' );
}
return $text;
}, 10 );

/**
 * remove add to cart buttons on shop archive page
 */

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( 'simple' ) ) {
$button_text = __("View product", "woocommerce");
$button = '<a class="button" href="' . $product->get_permalink() . '">' . 
$button_text . '</a>';
}
return $button;
}

저는 기부 제품을 가지고 있던 것과 같은 문제가 발생했습니다. 따라서 사용자가 원하는 금액의 기부를 할 수 있으므로 상점 페이지에서 해당 제품에 대한 카트 추가 버튼을 "상세 정보"로 교체하여 사용자가 기부할 수 있는 제품 한 페이지로 리디렉션했습니다.저는 이 코드를 사용했습니다.코드는 테마 또는 하위 테마의 기능에 들어갑니다.php 파일

function filter_woocommerce_loop_add_to_cart_link( $quantity,$product ) {  
$product_id = $product->get_id();
$title = $product->get_title();
$sku = $product->get_sku();
if($product_id == get_option( 'woocommerce_donations_product_id' )){
    //var_dump($title);
    //var_dump($sku);
    //var_dump($quantity);
    $simpleURL = get_permalink();
    //var_dump($simpleURL);
    $quantity='<a href="'.$simpleURL.'" data-quantity="1" class="product_type_simple ajax_add_to_cart" data-product_id="'.$product_id.'" data-product_sku="'.$sku.'" aria-label="Read more about “'.$title.'”" rel="nofollow"><span class="filter-popup">Détails</span></a>';
    //var_dump($quantity);
}
return $quantity;
//exit();
};
// add the filter 
add_filter( 'woocommerce_loop_add_to_cart_link','filter_woocommerce_loop_add_to_cart_link', 10, 2 );

언급URL : https://stackoverflow.com/questions/46111205/replace-add-to-cart-button-with-a-read-more-linked-to-product-page-on-shop-pages