簡體   English   中英

在 Woocommerce 商店頁面上自定義添加到購物車按鈕

[英]Customizing add to cart buttons on Woocommerce shop page

我目前使用此功能來更改單個產品頁面上的“添加到購物車”按鈕,當它位於特定類別中時。 我希望這也可以更改縮略圖下方商店頁面上的“添加到購物車按鈕”。 任何幫助將不勝感激。

// Display Auction Link When 'auction' is in the category
function so_43372512_maybe_show_auction_link(){
if( has_term( 'auction', 'product_cat' ) ) {
    echo ' <style type="text/css">
                .woocommerce div.product form.cart, .woocommerce div.product p.cart {
                display:none ; }
                .woocommerce div.product p.price, .woocommerce div.product span.price {
                display:none ; }
                .woocommerce div.product p.stock {
                display:none ; }
                .product_meta {
                margin-top:20px;
                }
            </style>';
    echo '<p>Click This Button To View The Lot         </p>';
    global $product; 
    $skusearch = $product->get_sku();
    echo '<a id="auction" style="font-size:100%;color:#fff;padding:.618em 1em;border-radius:3px;background-color:#ed1c24;font-weight:700;" href="https://www.wirebids.com/search?q=' . $skusearch . '&open_closed=open" target="blank">' . __ ( 'On Auction Now!', 'your-plugin' ) . '</a>';
}
}
add_action( 'woocommerce_single_product_summary', 
'so_43372512_maybe_show_auction_link', 35 );

您將在下面找到用自定義“立即拍賣!”替換添加到購物車按鈕所需的代碼。 按鈕,也隱藏了產品價格...

我還完全重新審視了您的代碼,刪除了模板“價格”和“添加到購物車”按鈕,而是使用 CSS 將它們隱藏起來,僅適用於“拍賣”產品類別……

編碼:

// Remove the price on archive pages (like shop) for 'auction' product category
add_action('woocommerce_after_shop_loop_item_title', 'remove_price_from_archives', 9 );
function remove_price_from_archives(){
    global $product, $post;

    // Only for 'auction' product category
    if ( has_term( 'clothing', 'product_cat' ) )
        remove_action('woocommerce_after_shop_loop_item_title', 'woocommerce_template_loop_price', 10);
}

// Replace add to cart button on archive pages (like shop) for 'auction' product category
add_action('woocommerce_after_shop_loop_item', 'replace_add_to_cart_button_in_archives', 9 );
function replace_add_to_cart_button_in_archives() {
    global $product, $post;

    // Only for 'auction' product category
    if ( ! has_term( 'clothing', 'product_cat' ) ) return;

    // remove add to cart button
    remove_action('woocommerce_after_shop_loop_item', 'woocommerce_template_loop_add_to_cart', 10);

    $skusearch = $product->get_sku();
    $style = 'style="font-size:100%;color:#fff;padding:.618em 1em;border-radius:3px;background-color:#ed1c24;font-weight:700;"';
    $href = 'href="https://www.wirebids.com/search?q=' . $skusearch . '&open_closed=open"';
    echo '<div style="margin-top:24px;">
        <a '.$href.' id="auction" '.$style.' target="blank">' . __ ( 'On Auction Now!', 'your-plugin' ) . '</a>
    </div>';
}

// Remove the displayed price and add-to-cart button on single product pages for 'auction' product category
// Replace add to cart by your custom "On Auction Now!" button
add_action( 'woocommerce_single_product_summary', 'remove_the_displayed_price_from_variable_products', 9 );
function remove_the_displayed_price_from_variable_products() {
    global $product, $post;

    // Only for 'auction' product category
    if ( has_term( 'clothing', 'product_cat' ) ){
        // remove product price
        remove_action('woocommerce_single_product_summary', 'woocommerce_template_single_price', 10);
        // remove add-to-cart button
        remove_action('woocommerce_single_product_summary', 'woocommerce_template_single_add_to_cart', 30);
        // Add your custom "On Auction Now!" button
        add_action( 'woocommerce_single_product_summary', 'replace_add_to_cart_by_auction', 30 );
    }
}

// This function displays your custom button replacement in single product pages
function replace_add_to_cart_by_auction(){
    global $product;

    $skusearch = $product->get_sku();
    $style = 'style="font-size:100%;color:#fff;padding:.618em 1em;border-radius:3px;background-color:#ed1c24;font-weight:700;"';
    $href = 'href="https://www.wirebids.com/search?q=' . $skusearch . '&open_closed=open"';

    echo '<p>Click This Button To View The Lot</p>
    <a '.$href.' id="auction" '.$style.' target="blank">' . __ ( 'On Auction Now!', 'your-plugin' ) . '</a>';
}

代碼位於活動子主題(或主題)的 function.php 文件或任何插件文件中。

此代碼經過測試並有效。

我必須在 woocommerce 構建的網站中自定義添加到購物車按鈕,我只需要在商店和類別頁面上執行此操作。

我使用以下功能完成了這項任務。 將此函數放入您的functions.php文件並確保在進行任何更新之前進行備份,它對我有用,希望對你也一樣。

add_filter( 'woocommerce_loop_add_to_cart_link', 'ij_replace_add_to_cart_button', 10, 2 );
function ij_replace_add_to_cart_button( $button, $product ) {
     
    $productid =  $product->id;
    $productslug =  $product->slug;
    $productname =  $product->name;
 
if (is_product_category() || is_shop()) {
     
$button_text = __("More Info", "woocommerce");
$button_link = $product->get_permalink(); 
$button = '<a href="'. $button_link .'"  data-quantity="1" class="button product_type_simple add_to_cart_button ajax_add_to_cart" data-product_id="'. $productid.'" data-product_sku="" aria-label="Add “'.$productname.'” to your cart" rel="nofollow"    data-productslug="'. $productslug.'" >' . $button_text . ' </a>';
return $button;
}
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM