WooCommerce Shop page: Quantity input on add to cart button

北战南征 提交于 2019-12-12 05:37:55

问题


I'm using the Divi theme with WooCommerce plugin. In shop page, I would like to have the quantities adjustments directly on add to cart button .

My web site

Presently you have to click on the product to go in to single product page and only then is it possible to add to cart.

I have tried to figure out what produces the list of products so that I can modify it, but as you probably guessed I'm very new to this.

How can I add that functionality the to shop page products?


回答1:


Advice: Is better to use a child theme, just to avoid losing the changes your are going to do when theme get updated. So I assume you will use now a child theme.

Check in WooCommerce > Settings > products (tab) > Display (sub tab) that you have correctly set the behavior you want:

In this active child theme, you will find a function.php file. If not, you will copy it from your parent theme removing all code inside, except the <?php tag at the beginning (if it exist) and same thing for ?> at the end (if it exist).

Once done, you are going to use woocommerce_loop_add_to_cart_link filter hook to add quantity to your add-to-cart button (for simple products). You will paste this code snippet inside it:

add_filter( 'woocommerce_loop_add_to_cart_link', 'quantity_inputs_for_woocommerce_loop_add_to_cart_link', 10, 2 );
function quantity_inputs_for_woocommerce_loop_add_to_cart_link( $html, $product ) {
    if ( $product && $product->is_type( 'simple' ) && $product->is_purchasable() && $product->is_in_stock() && ! $product->is_sold_individually() ) {
        $html = '<form action="' . esc_url( $product->add_to_cart_url() ) . '" class="cart" method="post" enctype="multipart/form-data">';
        $html .= woocommerce_quantity_input( array(), $product, false );
        $html .= '<button type="submit" class="button alt">' . esc_html( $product->add_to_cart_text() ) . '</button>';
        $html .= '</form>';
    }
    return $html;
}

You can also customize this code to better feet your needs…

Reference: Override loop template and show quantities next to add to cart buttons




回答2:


Woocommerce documentation has APIs has well to feel. Recently they upgraded to WP JSON API integration with 2.6 version. You have two options here.

1) Make an order via the api call
2) Make and ajax request to change quantity easily by

http://domain.com/?add-to-cart=Product-Id&variation_id=136&quantity=2

Example:

http://domain.com/?add-to-cart=97&variation_id=136&attribute_pa_sizes=xl

If you don't have any variation or options, you can easily do it via

http://domain.com/?add-to-cart=Id&&quantity=Qty



回答3:


So what I ended up doing was adding the single page add to cart action to a hook for the shop page loop as follows:

add_action('woocommerce_after_shop_loop_item',
           'woocommerce_template_single_add_to_cart', 30);

Thanks.



来源:https://stackoverflow.com/questions/37968332/woocommerce-shop-page-quantity-input-on-add-to-cart-button

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!