在商店WooCommerce上显示添加到购物车按钮

时间:2019-07-19 作者:Sim Taster

请告诉我这两个选项的相关帮助:

1) 如何显示“添加到购物车”按钮而不是“查看产品”按钮?

2) 如何在左侧显示每个产品要添加的单位(数字以“+”和“-”)以及右侧的“添加到购物车”按钮?

如果你知道一个插件或一些php代码在工作。

链接:https://erboristerialofficinale.it/shop/

提前谢谢你

S

2 个回复
SO网友:Tanmay Patel

Point 1: 要更改产品档案上的“VEDI PRODOTTO”(查看产品)按钮,而不是“Add To Cart”(添加到购物车)按钮:将下面的代码放入functions.php 文件

remove_action( \'woocommerce_after_shop_loop_item\', \'woocommerce_template_loop_add_to_cart\' );
add_action(\'woocommerce_after_shop_loop_item\', \'add_a_custom_button\', 5 );
function add_a_custom_button() {
    global $product;
    if( $product->is_type(\'variable\') || $product->is_type(\'grouped\') ) return;
    echo \'<div style="margin-bottom:10px;">
        <a class="button custom-button" href="\' . esc_attr( $product->get_permalink() ) . \'">\' . __(\'VEDI PRODOTTO\') . \'</a>
    </div>\';
}
Point 2: 在左侧显示每个产品要添加的单位(数字以“+”和“-”)并在右侧显示“添加到购物车”按钮。输入以下代码functions.php 文件并在中添加CSS代码style.css 文件

// 1. Show Buttons

add_action( \'woocommerce_before_add_to_cart_quantity\', \'bbloomer_display_quantity_plus\' );

function bbloomer_display_quantity_plus() {
   echo \'<button type="button" class="plus" >+</button>\';
}

add_action( \'woocommerce_after_add_to_cart_quantity\', \'bbloomer_display_quantity_minus\' );

function bbloomer_display_quantity_minus() {
   echo \'<button type="button" class="minus" >-</button>\';
}


// 2. Trigger jQuery script

add_action( \'wp_footer\', \'bbloomer_add_cart_quantity_plus_minus\' );

function bbloomer_add_cart_quantity_plus_minus() {
   // Only run this on the single product page
   if ( ! is_product() ) return;
   ?>
      <script type="text/javascript">

      jQuery(document).ready(function($){   

         $(\'form.cart\').on( \'click\', \'button.plus, button.minus\', function() {

            // Get current quantity values
            var qty = $( this ).closest( \'form.cart\' ).find( \'.qty\' );
            var val   = parseFloat(qty.val());
            var max = parseFloat(qty.attr( \'max\' ));
            var min = parseFloat(qty.attr( \'min\' ));
            var step = parseFloat(qty.attr( \'step\' ));

            // Change the value if plus or minus
            if ( $( this ).is( \'.plus\' ) ) {
               if ( max && ( max <= val ) ) {
                  qty.val( max );
               } else {
                  qty.val( val + step );
               }
            } else {
               if ( min && ( min >= val ) ) {
                  qty.val( min );
               } else if ( val > 1 ) {
                  qty.val( val - step );
               }
            }

         });

      });

      </script>
   <?php
}
CSS代码

.woocommerce div.product .entry-summary .cart div.quantity{
    float: none;
    margin: 0;
    display: inline-block;
}
.woocommerce div.product form.cart .button {
    vertical-align: middle;
    float: none;
}

SO网友:vikas jain

Add this code in function.php

function vk_shop_page_add_quantity_field() {
    /** @var WC_Product $product */
    $product = wc_get_product( get_the_ID() );
    if ( ! $product->is_sold_individually() && \'variable\' != $product->get_type() && $product->is_purchasable() ) {
        woocommerce_quantity_input( array( \'min_value\' => 1, \'max_value\' => $product->backorders_allowed() ? \'\' : $product->get_stock_quantity() ) );
    }
}

add_action( \'woocommerce_after_shop_loop_item\', \'vk_shop_page_add_quantity_field\', 12 );


/**
 * Add required JavaScript.
 */
function vk_shop_page_quantity_add_to_cart_handler() {
    wc_enqueue_js( \'
        $(".woocommerce .products").on("click", ".quantity input", function() {
            return false;
        });
        $(".woocommerce .products").on("change input", ".quantity .qty", function() {
            var add_to_cart_button = $(this).parents( ".product" ).find(".add_to_cart_button");
            // For AJAX add-to-cart actions
            add_to_cart_button.data("quantity", $(this).val());
            // For non-AJAX add-to-cart actions
            add_to_cart_button.attr("href", "?add-to-cart=" + add_to_cart_button.attr("data-product_id") + "&quantity=" + $(this).val());
        });
        // Trigger on Enter press
        $(".woocommerce .products").on("keypress", ".quantity .qty", function(e) {
            if ((e.which||e.keyCode) === 13) {
                $( this ).parents(".product").find(".add_to_cart_button").trigger("click");
            }
        });
    \' );
}
add_action( \'init\', \'vk_shop_page_quantity_add_to_cart_handler\' );

Happy coding...

相关推荐