在商店页面上为每个产品添加数量字段-WooCommerce

时间:2013-12-10 作者:devjohn

我想在商店页面上为每个产品添加数量字段。我用过

<input type="number" step="1" min="1" name="quantity" value="" title="Qty" class="input-text qty text"/>
woocommerce/模板/循环/添加到购物车中的代码。php

但其无法在提交时添加数量。请给我任何建议。提前感谢

3 个回复
最合适的回答,由SO网友:Bainternet 整理而成

下面是我如何修改循环/添加到购物车的。php来实现这一点:

<?php
/**
 * Loop Add to Cart
 *
 * @author      WooThemes
 * @package     WooCommerce/Templates
 * @version     1.6.4
 */

if ( ! defined( \'ABSPATH\' ) ) exit; // Exit if accessed directly

global $product;
?>

<?php if ( ! $product->is_in_stock() ) : ?>

    <a href="<?php echo apply_filters( \'out_of_stock_add_to_cart_url\', get_permalink( $product->id ) ); ?>" class="button"><?php echo apply_filters( \'out_of_stock_add_to_cart_text\', __( \'Read More\', \'woocommerce\' ) ); ?></a>

<?php else : ?>

    <?php
        $link = array(
            \'url\'   => \'\',
            \'label\' => \'\',
            \'class\' => \'\'
        );

        $handler = apply_filters( \'woocommerce_add_to_cart_handler\', $product->product_type, $product );

        switch ( $handler ) {
            case "variable" :
                $link[\'url\']    = apply_filters( \'variable_add_to_cart_url\', get_permalink( $product->id ) );
                $link[\'label\']  = apply_filters( \'variable_add_to_cart_text\', __( \'Select options\', \'woocommerce\' ) );
            break;
            case "grouped" :
                $link[\'url\']    = apply_filters( \'grouped_add_to_cart_url\', get_permalink( $product->id ) );
                $link[\'label\']  = apply_filters( \'grouped_add_to_cart_text\', __( \'View options\', \'woocommerce\' ) );
            break;
            case "external" :
                $link[\'url\']    = apply_filters( \'external_add_to_cart_url\', get_permalink( $product->id ) );
                $link[\'label\']  = apply_filters( \'external_add_to_cart_text\', __( \'Read More\', \'woocommerce\' ) );
            break;
            default :
                if ( $product->is_purchasable() ) {
                    $link[\'url\']    = apply_filters( \'add_to_cart_url\', esc_url( $product->add_to_cart_url() ) );
                    $link[\'label\']  = apply_filters( \'add_to_cart_text\', __( \'Add to cart\', \'woocommerce\' ) );
                    $link[\'class\']  = apply_filters( \'add_to_cart_class\', \'add_to_cart_button\' );
                } else {
                    $link[\'url\']    = apply_filters( \'not_purchasable_url\', get_permalink( $product->id ) );
                    $link[\'label\']  = apply_filters( \'not_purchasable_text\', __( \'Read More\', \'woocommerce\' ) );
                }
            break;
        }
        if ( $product->product_type == \'simple\' ) {

            ?>
            <form action="<?php echo esc_url( $product->add_to_cart_url() ); ?>" class="cart" method="post" enctype=\'multipart/form-data\'>

                <?php woocommerce_quantity_input(); ?>

                <button type="submit" class="button alt"><?php echo $label; ?></button>

            </form>
            <?php

        } else {
            echo apply_filters( \'woocommerce_loop_add_to_cart_link\', sprintf(\'<a href="%s" rel="nofollow" data-product_id="%s" data-product_sku="%s" class="%s button product_type_%s">%s</a>\', esc_url( $link[\'url\'] ), esc_attr( $product->id ), esc_attr( $product->get_sku() ), esc_attr( $link[\'class\'] ), esc_attr( $product->product_type ), esc_html( $link[\'label\'] ) ), $product, $link );
        }
    ?>

<?php endif; ?>
注意:这仅对简单产品有效。

SO网友:Claudio Sanches

如果您更喜欢使用ajax,可以使用:

创建wp-content/themes/your-theme/templates/loop/add-to-cart.php 使用文件:

<?php
/**
 * Custom Loop Add to Cart.
 *
 * Template with quantity and ajax.
 */

if ( ! defined( \'ABSPATH\' ) ) exit; // Exit if accessed directly.

global $product;
?>

<?php if ( ! $product->is_in_stock() ) : ?>

    <a href="<?php echo apply_filters( \'out_of_stock_add_to_cart_url\', get_permalink( $product->id ) ); ?>" class="button"><?php echo apply_filters( \'out_of_stock_add_to_cart_text\', __( \'Read More\', \'woocommerce\' ) ); ?></a>

<?php else : ?>

    <?php
        $link = array(
            \'url\'   => \'\',
            \'label\' => \'\',
            \'class\' => \'\'
        );

        switch ( $product->product_type ) {
            case "variable" :
                $link[\'url\']    = apply_filters( \'variable_add_to_cart_url\', get_permalink( $product->id ) );
                $link[\'label\']  = apply_filters( \'variable_add_to_cart_text\', __( \'Select options\', \'woocommerce\' ) );
            break;
            case "grouped" :
                $link[\'url\']    = apply_filters( \'grouped_add_to_cart_url\', get_permalink( $product->id ) );
                $link[\'label\']  = apply_filters( \'grouped_add_to_cart_text\', __( \'View options\', \'woocommerce\' ) );
            break;
            case "external" :
                $link[\'url\']    = apply_filters( \'external_add_to_cart_url\', get_permalink( $product->id ) );
                $link[\'label\']  = apply_filters( \'external_add_to_cart_text\', __( \'Read More\', \'woocommerce\' ) );
            break;
            default :
                if ( $product->is_purchasable() ) {
                    $link[\'url\']    = apply_filters( \'add_to_cart_url\', esc_url( $product->add_to_cart_url() ) );
                    $link[\'label\']  = apply_filters( \'add_to_cart_text\', __( \'Add to cart\', \'woocommerce\' ) );
                    $link[\'class\']  = apply_filters( \'add_to_cart_class\', \'add_to_cart_button\' );
                } else {
                    $link[\'url\']    = apply_filters( \'not_purchasable_url\', get_permalink( $product->id ) );
                    $link[\'label\']  = apply_filters( \'not_purchasable_text\', __( \'Read More\', \'woocommerce\' ) );
                }
            break;
        }

        // If there is a simple product.
        if ( $product->product_type == \'simple\' ) {
            ?>
            <form action="<?php echo esc_url( $product->add_to_cart_url() ); ?>" class="cart" method="post" enctype="multipart/form-data">
                <?php
                    // Displays the quantity box.
                    woocommerce_quantity_input();

                    // Display the submit button.
                    echo sprintf( \'<button type="submit" data-product_id="%s" data-product_sku="%s" data-quantity="1" class="%s button product_type_simple">%s</button>\', esc_attr( $product->id ), esc_attr( $product->get_sku() ), esc_attr( $link[\'class\'] ), esc_html( $link[\'label\'] ) );
                ?>
            </form>
            <?php
        } else {
          echo apply_filters( \'woocommerce_loop_add_to_cart_link\', sprintf(\'<a href="%s" rel="nofollow" data-product_id="%s" data-product_sku="%s" class="%s button product_type_%s">%s</a>\', esc_url( $link[\'url\'] ), esc_attr( $product->id ), esc_attr( $product->get_sku() ), esc_attr( $link[\'class\'] ), esc_attr( $product->product_type ), esc_html( $link[\'label\'] ) ), $product, $link );
        }

    ?>

<?php endif; ?>
在你的主题中functions.php 粘贴:

function cs_wc_loop_add_to_cart_scripts() {
    if ( is_shop() || is_product_category() || is_product_tag() || is_product() ) : ?>

<script>
    jQuery(document).ready(function($) {
        $(document).on( \'change\', \'.quantity .qty\', function() {
            $(this).parent(\'.quantity\').next(\'.add_to_cart_button\').attr(\'data-quantity\', $(this).val());
        });
    });
</script>

    <?php endif;
}

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

SO网友:webaware

截取“添加到购物车”按钮,并替换为单个产品页面上使用的按钮。

/**
* start the customisation
*/
add_action(\'woocommerce_before_shop_loop\', function() {
    add_filter(\'woocommerce_loop_add_to_cart_link\', \'wpse_125946_add_to_cart\', 10, 3);
});

/**
* customise Add to Cart link/button for product loop
* @param string $button
* @param object $product
* @param array $link
* @return string
*/
function wpse_125946_add_to_cart($button, $product, $link) {
    // not for variable, grouped or external products
    if (!in_array($product->product_type, array(\'variable\', \'grouped\', \'external\'))) {
        // only if can be purchased
        if ($product->is_purchasable()) {
            // show qty +/- with button
            ob_start();
            woocommerce_simple_add_to_cart();
            $button = ob_get_clean();
        }
    }

    return $button;
}
更好的是,这样做support add-to-cart with AJAX -- 代码在中可用this gist.

结束

相关推荐

private functions in plugins

我开发了两个插件,其中一个功能相同(相同的名称,相同的功能)。当试图激活两个插件时,Wordpress会抛出一个错误,因为它不允许我以相同的名称定义函数两次。有没有一种方法可以使这个函数只对插件私有,而不使用面向对象编程,也不简单地重命名函数?我不想使用OOP,因为我首先要学习它。此外,我不想重命名该函数,因为我可能也想在其他插件中使用它,而重命名感觉不太合适。