简体   繁体   English

WooCommerce $ product-> get_stock_quantity(); 打破购物车页面

[英]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. 我已经在WooCommerce模板中编辑了quantity-input.php模板文件,以创建自己的输入按钮。 The problem is that I have used $product->get_stock_quantity(); 问题是我使用了$ product-> get_stock_quantity();。 to set the max attribute on the input. 在输入上设置max属性。 This works fine on all the pages apart from the cart page where it breaks the html and displays this error; 这在除购物车页面之外的所有页面上均能正常工作,在该页面上它会中断HTML并显示此错误;

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. 看起来$ product不包含(有效)对象。

You can check if $product is a object by doing 您可以通过执行以下操作检查$ product是否为对象:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 WooCommerce 产品 get_stock_quantity() 方法返回 null - WooCommerce Product get_stock_quantity() method return null Woocommerce 未捕获错误:在 null 上调用成员函数 get_stock_quantity() - Woocommerce Uncaught Error: Call to a member function get_stock_quantity() on null 如何从woocommerce的购物车页面获取特定产品数量 - how to get particular product quantity from the cart page in the woocommerce 在 WooCommerce 产品页面中显示数量变化的产品总数和购物车小计 - Display product total and cart subtotal on quantity change in WooCommerce product page 产品库存数量 - 购物车中的产品数量 - Product Stock Quantity - No of Products in Cart WooCommerce $ _product-&gt; is_visible()错误-cart.php - WooCommerce $_product->is_visible() error - cart.php 如果用户在WooCommerce购物车中将数量更改为0,则获取项目的产品ID - Get The Product Id of An Item If User Changes Quantity to 0 in WooCommerce Cart WooCommerce:仅获取定制产品的购物车商品数量 - WooCommerce: Get the cart item quantity of a customized product only Woocommerce购物车中的“最小和步骤”数量和批发角色的单个产品页面 - Min and Steps quantity in Woocommerce cart and single product page for wholesale role WooCommerce更新产品库存数量和状态 3 - Update product stock quantity and status in WooCommerce 3
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM