选择自定义选择数量购物车页面WooCommerce时更新购物车总计(使用Ajax)

时间:2020-05-14 15:42:20

标签: php ajax wordpress woocommerce cart

我正在使用购物车页面上的自定义选择框来选择数量,我需要更新购物车总计,每次用户在Ajax中选择数量时。

我知道我可以触发[name =“ update_cart”]按钮。但是在我的主题设计中,没有update_cart按钮。因此,建议我发送ajax请求以更新选定数量的购物车总数的任何想法。

/**
 * Add to Cart Quantity drop-down - WooCommerce
 *
 *
 */

function woocommerce_quantity_input( $args = array(), $product = null, $echo = true ) {

    if ( is_null( $product ) ) {
        $product = $GLOBALS['product'];
    }

    $defaults = array(
        'input_id' => uniqid( 'quantity_' ),
        'input_name' => 'quantity',
        'input_value' => '1',
        'classes' => apply_filters( 'woocommerce_quantity_input_classes', array( 'input-text', 'qty', 'text' ), $product ),
        'max_value' => apply_filters( 'woocommerce_quantity_input_max', -1, $product ),
        'min_value' => apply_filters( 'woocommerce_quantity_input_min', 0, $product ),
        'step' => apply_filters( 'woocommerce_quantity_input_step', 1, $product ),
        'pattern' => apply_filters( 'woocommerce_quantity_input_pattern', has_filter( 'woocommerce_stock_amount', 'intval' ) ? '[0-9]*' : '' ),
        'inputmode' => apply_filters( 'woocommerce_quantity_input_inputmode', has_filter( 'woocommerce_stock_amount', 'intval' ) ? 'numeric' : '' ),
        'product_name' => $product ? $product->get_title() : '',
    );

    $args = apply_filters( 'woocommerce_quantity_input_args', wp_parse_args( $args, $defaults ), $product );

    // Apply sanity to min/max args - min cannot be lower than 0.
    $args['min_value'] = max( $args['min_value'], 0 );
    // Note: change 20 to whatever you like
    $args['max_value'] = 0 < $args['max_value'] ? $args['max_value'] : 20;

    // Max cannot be lower than min if defined.
    if ( '' !== $args['max_value'] && $args['max_value'] < $args['min_value'] ) {
        $args['max_value'] = $args['min_value'];
    }

    $options = '';

    for ( $count = $args['min_value']; $count <= $args['max_value']; $count = $count + $args['step'] ) {

        // Cart item quantity defined?
        if ( '' !== $args['input_value'] && $args['input_value'] >= 1 && $count == $args['input_value'] ) {
            $selected = 'selected';
        } else $selected = '';

        $options .= '<option value="' . $count . '"' . $selected . '>' . $count . '</option>';

    }

    $string = '<div class="quantity"><select class="custom-cty" name="' . $args['input_name'] . '">' . $options . '</select></div>';

    if ( $echo ) {
        echo $string;
    } else {
        return $string;
    }

}

0 个答案:

没有答案