Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
346 views
in Technique[技术] by (71.8m points)

javascript - Change quantity in mini-cart

I would like to be able to change product quantity in the mini-cart. I can't get it to work. I have done some customizations and I am new to PHP. I've seen solutions including Javascript but they haven't worked. Anyone have a solution?

I did manage to get the quantity to show with woocommerce_quantity_input, but I can't get it to update the product.

<?php

defined( 'ABSPATH' ) || exit;

if ( ! function_exists( 'elementor_pro_render_mini_cart_item' ) ) {
    function elementor_pro_render_mini_cart_item( $cart_item_key, $cart_item ) {
        $_product = apply_filters( 'woocommerce_cart_item_product', $cart_item['data'], $cart_item, $cart_item_key );
        $is_product_visible = ( $_product && $_product->exists() && $cart_item['quantity'] > 0 && apply_filters( 'woocommerce_widget_cart_item_visible', true, $cart_item, $cart_item_key ) );

        if ( ! $is_product_visible ) {
            return;
        }

        $product_id = apply_filters( 'woocommerce_cart_item_product_id', $cart_item['product_id'], $cart_item, $cart_item_key );
        $product_price = apply_filters( 'woocommerce_cart_item_price', WC()->cart->get_product_price( $_product ), $cart_item, $cart_item_key );
        $product_permalink = apply_filters( 'woocommerce_cart_item_permalink', $_product->is_visible() ? $_product->get_permalink( $cart_item ) : '', $cart_item, $cart_item_key );
        ?>
        <div class="elementor-menu-cart__product woocommerce-cart-form__cart-item <?php echo esc_attr( apply_filters( 'woocommerce_cart_item_class', 'cart_item', $cart_item, $cart_item_key ) ); ?>">

            <div class="elementor-menu-cart__product-image product-thumbnail">
                <?php
                $thumbnail = apply_filters( 'woocommerce_cart_item_thumbnail', $_product->get_image(), $cart_item, $cart_item_key );

                if ( ! $product_permalink ) :
                    echo wp_kses_post( $thumbnail );
                else :
                    printf( '<a href="%s">%s</a>', esc_url( $product_permalink ), wp_kses_post( $thumbnail ) );
                endif;
                ?>
            </div>

            <div class="elementor-menu-cart__product-name product-name" data-title="<?php esc_attr_e( 'Product', 'elementor-pro' ); ?>">
                <?php
                if ( ! $product_permalink ) :
                    echo wp_kses_post( apply_filters( 'woocommerce_cart_item_name', $_product->get_name(), $cart_item, $cart_item_key ) . '&nbsp;' );
                else :
                    echo wp_kses_post( apply_filters( 'woocommerce_cart_item_name', sprintf( '<a href="%s">%s</a>', esc_url( $product_permalink ), $_product->get_name() ), $cart_item, $cart_item_key ) );
                endif;

                do_action( 'woocommerce_after_cart_item_name', $cart_item, $cart_item_key );
                // Meta data.
                echo wc_get_formatted_cart_item_data( $cart_item ); // PHPCS: XSS ok.
                ?>
            </div>
            <div class="elementor-menu-cart__product-price product-price" data-title="<?php esc_attr_e( 'Price', 'elementor-pro' ); ?>">
                <?php 
                    echo woocommerce_quantity_input( array('input_value' => $cart_item['quantity']),$cart_item['data'], false );
                ?>
                <?php echo apply_filters( 'woocommerce_widget_cart_item_quantity', '<span class="quantity">' . sprintf( '%s &times; %s', $cart_item['quantity'], $product_price ) . '</span>', $cart_item, $cart_item_key ); ?>
                
            </div>

            <div class="elementor-menu-cart__product-remove product-remove">
                
                <?php
                echo apply_filters( 'woocommerce_cart_item_remove_link', sprintf(
                    '<a href="%s" aria-label="%s" data-product_id="%s" data-cart_item_key="%s" data-product_sku="%s"></a>',
                    esc_url( wc_get_cart_remove_url( $cart_item_key ) ),
                    __( 'Remove this item', 'elementor-pro' ),
                    esc_attr( $product_id ),
                    esc_attr( $cart_item_key ),
                    esc_attr( $_product->get_sku() )
                ), $cart_item_key );
                ?>
            </div>
        </div>
        <?php
    }
}

$cart_items = WC()->cart->get_cart();

if ( empty( $cart_items ) ) { ?>
    <div class="woocommerce-mini-cart__empty-message"><?php esc_attr_e( 'No products in the cart.', 'elementor-pro' ); ?></div>
<?php } else { ?>
    <div class="elementor-menu-cart__products woocommerce-mini-cart cart woocommerce-cart-form__contents">
        <?php
        do_action( 'woocommerce_before_mini_cart_contents' );
        foreach ( $cart_items as $cart_item_key => $cart_item ) {
            elementor_pro_render_mini_cart_item( $cart_item_key, $cart_item );
        }

        do_action( 'woocommerce_mini_cart_contents' );
        ?>
        
    </div>

    
    <div class="mini-cart-footer">
        <div class="elementor-menu-cart__subtotal">
        <div class="woo_total_left"><strong><?php echo __( 'Total', 'woocommerce' ); // phpcs:ignore WordPress.WP.I18n ?>:</strong></div> <div class="woo_total_right"><?php echo WC()->cart->get_cart_subtotal(); ?></div>
    </div>
    <div class="elementor-menu-cart__footer-buttons">
        <a href="<?php echo esc_url( wc_get_checkout_url() ); ?>" class="elementor-button elementor-button--checkout elementor-size-md">
            <span class="elementor-button-text"><?php echo __( 'Checkout', 'woocommerce' ); // phpcs:ignore WordPress.WP.I18n ?></span>
        </a>
    </div>
    </div>
    <?php
} // empty( $cart_items )

?>
question from:https://stackoverflow.com/questions/66048399/change-quantity-in-mini-cart

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...