简体   繁体   English

在 Woocommerce 商店页面上自定义添加到购物车按钮

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

I currently use this function to change the 'add to cart' button on single product pages when it is in a specific category.我目前使用此功能来更改单个产品页面上的“添加到购物车”按钮,当它位于特定类别中时。 I would like this to also change the 'add to cart button' on the shop page below the thumbnails.我希望这也可以更改缩略图下方商店页面上的“添加到购物车按钮”。 Any help would be greatly appreciated.任何帮助将不胜感激。

// 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 );

Below you will find the necessary code to replace add to cart button by a custom 'On Auction Now!'您将在下面找到用自定义“立即拍卖!”替换添加到购物车按钮所需的代码。 button, hiding the product price too…按钮,也隐藏了产品价格...

I have also completely revisited your code, removing templates "price" and "add to cart" button, instead hiding them with CSS, for the 'auction' product category only…我还完全重新审视了您的代码,删除了模板“价格”和“添加到购物车”按钮,而是使用 CSS 将它们隐藏起来,仅适用于“拍卖”产品类别……

The code:编码:

// 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>';
}

Code goes in function.php file of your active child theme (or theme) or also in any plugin file.代码位于活动子主题(或主题)的 function.php 文件或任何插件文件中。

This code is tested and works.此代码经过测试并有效。

I had to customize add to cart button in woocommerce built website, I needed to do this only on shop and category page.我必须在 woocommerce 构建的网站中自定义添加到购物车按钮,我只需要在商店和类别页面上执行此操作。

I did this task with below function.我使用以下功能完成了这项任务。 put this function into your functions.php file and make sure to take backup before do any update, it worked for me and hope the same for you.将此函数放入您的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.

相关问题 Woocommerce商店页面(类别页面模板)添加到购物车按钮行为 - Woocommerce shop page (category page template) Add to cart button behavior Woocommerce - 将商店页面上的“添加到购物车”重定向到产品页面? - Woocommerce - Redirect Add-to-Cart on Shop page to Product Page? 将数量字段添加到 WooCommerce 商店页面上的 Ajax 添加到购物车按钮 - Add a quantity field to Ajax add to cart button on WooCommerce shop page 在 Woocommerce 3 商店页面上的添加到购物车下添加额外按钮 - Add Extra button under Add to cart on shop page of Woocommerce 3 在结帐时添加一个按钮以清空购物车并重定向到Woocommerce中的商店页面 - Add a button in checkout to empty cart and redirect to shop page in Woocommerce WooCommerce商店页面:添加到购物车按钮上的数量输入 - WooCommerce Shop page: Quantity input on add to cart button 在Woocommerce商店页面中更改用于简单产品的购物车按钮 - Change add-to-cart button for simple products in Woocommerce shop page 更改商店页面中 woocommerce 商店中“添加到购物车”按钮的重定向 - Changing the redirect of the “add to cart” button in woocommerce store in Shop page 在 WooCommerce 商店页面上添加数量字段并添加到购物车按钮 - Adding quantity fields and add to cart buton on WooCommerce Shop page 在Woocommerce 3中自定义添加到购物车消息 - Customizing add to cart message in Woocommerce 3
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM