简体   繁体   中英

WooCommerce $product->get_stock_quantity(); breaking cart page

I have edited the quantity-input.php template file inside WooCommerce templates to create my own input buttons. The problem is that I have used $product->get_stock_quantity(); to set the max attribute on the input. This works fine on all the pages apart from the cart page where it breaks the html and displays this error;

Notice: Trying to get property of non-object

Why is this breaking the cart page but none of the others?

here is the full template code;

if ( $max_value && $min_value === $max_value ) {
    ?>
    <div class="quantity hidden">
        <input type="hidden" id="<?php echo esc_attr( $input_id ); ?>" class="qty" name="<?php echo esc_attr( $input_name ); ?>" value="<?php echo esc_attr( $min_value ); ?>" />
    </div>
    <?php
} else {
    /* translators: %s: Quantity. */
    $labelledby = ! empty( $args['product_name'] ) ? sprintf( __( '%s quantity', 'woocommerce' ), strip_tags( $args['product_name'] ) ) : '';
    ?>
    <div class="quantity">
        <label class="screen-reader-text" for="<?php echo esc_attr( $input_id ); ?>"><?php esc_html_e( 'Quantity', 'woocommerce' ); ?></label>
        <div class="increment">
            <span></span><span></span>
        </div>

        <?php global $product ?>

        <input
            type="text"
            id="<?php echo esc_attr( $input_id ); ?>"
            class="input-text qty text"
            step="<?php echo esc_attr( $step ); ?>"
            min="<?php echo esc_attr( $min_value ); ?>"
            max="<?php echo $product->get_stock_quantity();?>"
            name="<?php echo esc_attr( $input_name ); ?>"
            value="<?php echo esc_attr( $input_value ); ?>"
            title="<?php echo esc_attr_x( 'Qty', 'Product quantity input tooltip', 'woocommerce' ); ?>"
            size="4"
            pattern="<?php echo esc_attr( $pattern ); ?>"
            inputmode="<?php echo esc_attr( $inputmode ); ?>"
            aria-labelledby="<?php echo esc_attr( $labelledby ); ?>" />
            <div class="decrement">
                <span></span>
            </div>
    </div>
    <?php
}

It looks like that $product doesn't contain a (valid) object.

You can check if $product is a object by doing

if (is_object($product) )
   echo 'Yes a object';
 else
   echo 'Sorry no object';     

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM