Display price on add to cart button from the functions.php file in Woocommerce

前端 未结 3 1712
醉酒成梦
醉酒成梦 2020-12-19 19:08

Been trying to find something about this, but the solutions I found so far here do not work.

I need, in the single product page, to display the add to cart button li

相关标签:
3条回答
  • 2020-12-19 19:39

    Edit your woocommerce/loop/add-to-cart.php template file like this:

    if ( ! defined( 'ABSPATH' ) ) {
        exit;
    }
    
    global $product;
    
    echo apply_filters( 'woocommerce_loop_add_to_cart_link', // WPCS: XSS ok.
        sprintf( '<a href="%s" data-quantity="%s" class="%s" %s>ADD TO CART - JUST %s</a>',
            esc_url( $product->add_to_cart_url() ),
            esc_attr( isset( $args['quantity'] ) ? $args['quantity'] : 1 ),
            esc_attr( isset( $args['class'] ) ? $args['class'] : 'button' ),
            isset( $args['attributes'] ) ? wc_implode_html_attributes( $args['attributes'] ) : '',
            $product->get_price()
        ),
    $product, $args );
    

    For the single product pages do basically the same in:

    woocommerce/single-product/add-to-cart/simple.php

    And any other template you use in the add-to-cart directory.

    0 讨论(0)
  • 2020-12-19 20:03

    To handle the display of the product price in add-to-cart button for all product prices on shop, other archives pages and single product pages, without any need of overriding templates, using dedicated Woocommerce filter hooks:

    add_filter( 'woocommerce_product_add_to_cart_text', 'custom_add_to_cart_price', 20, 2 ); // Shop and other archives pages
    add_filter( 'woocommerce_product_single_add_to_cart_text', 'custom_add_to_cart_price', 20, 2 ); // Single product pages
    function custom_add_to_cart_price( $button_text, $product ) {
        // Variable products
        if( $product->is_type('variable') ) {
            // shop and archives
            if( ! is_product() ){
                $product_price = wc_price( wc_get_price_to_display( $product, array( 'price' => $product->get_variation_price() ) ) );
                return $button_text . ' - From ' . strip_tags( $product_price );
            } 
            // Single product pages
            else {
                return $button_text;
            }
        } 
        // All other product types
        else {
            $product_price = wc_price( wc_get_price_to_display( $product ) );
            return $button_text . ' - Just ' . strip_tags( $product_price );
        }
    }
    

    Code goes in function.php file of your active child theme (or active theme). Tested and works.


    On shop page:

    On single product pages:

    0 讨论(0)
  • 2020-12-19 20:03

    I would suggest you override the WooCommerce template from the child theme, the file name is add-to-cart.php and can be found at woocommerce > loop.

    At the bottom add the following code, it will be displayed only in product single.

    if (is_single()) {
        echo sprintf(
            '<span>%s %s</span>',
            esc_attr__('JUST', 'woocommerce'),
            esc_attr($product->price)
        );
    }
    
    0 讨论(0)
提交回复
热议问题