微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

在“woocommerce_quantity_input”可插拔函数中获取产品ID

如何解决在“woocommerce_quantity_input”可插拔函数中获取产品ID

我正在使用 woocommerce_quantity_input 可插拔功能数量输入从文本框修改为我网站上的下拉列表。

在购物车页面上,当输出数量输入时,我需要获取产品 ID 以便我可以从单个产品页面获取 ACF 字段。

我的代码

function woocommerce_quantity_input($data = null,$args = array(),$echo = true) {
    global $product;
    $set_quantity_limit = get_field('set_quantity_limit');
    if ( !$data || is_product() ) {
        $defaults = array(
            'input_id'   => '','input_name'   => 'quantity','input_value'   => '1','max_value'     => apply_filters( 'woocommerce_quantity_input_max','',$product ),'min_value'     => apply_filters( 'woocommerce_quantity_input_min','step'         => apply_filters( 'woocommerce_quantity_input_step','1',);
    } else {
        $defaults = array(
            'input_id'   => $data['input_id'],'input_name'   => $data['input_name'],'input_value'   => $data['input_value'],'step'         => apply_filters( 'cw_woocommerce_quantity_input_step','max_value'     => apply_filters( 'cw_woocommerce_quantity_input_max','min_value'     => apply_filters( 'cw_woocommerce_quantity_input_min',);
    }

    if($set_quantity_limit){
        if ( ! $product->is_sold_individually() ) {
            $min = $defaults['min_value'] = 1;
            $max = $defaults['max_value'] = $set_quantity_limit;
            $step = $defaults['step'] = 1;
        }
    } else {
        if ( ! empty( $defaults['min_value'] ) )
            $min = $defaults['min_value'];
        else $min = 1;
    
        if ( ! empty( $defaults['max_value'] ) )
            $max = $defaults['max_value'];
        else $max = 6;
    
        if ( ! empty( $defaults['step'] ) )
            $step = $defaults['step'];
        else $step = 1;
        
    }
    
    $options = '';
    for ( $count = $min; $count <= $max; $count = $count+$step ) {
        $selected = (($count == $defaults['input_value']) ? ' selected' : '');
        $suffix_text_with_count = $count . ( ( $count == 6 ) ? ' - 1 Mastercase' : ' Box - 12 ct.' );
        $options .= '<option value="' . $count . '"'.$selected.'>' . ( ( $set_quantity_limit ) ? $count : $suffix_text_with_count ) . '</option>';
    }
    $string = '<div class="quantity quantity_select" style="' . $defaults['style'] . '">';
        $string .= '<label class="screen-reader-text" for="' . esc_attr( $defaults['input_id'] ) . '">' . _x( 'Quantity','woocommerce' ) . '</label>';
        $string .= '<select ';
        $string .= 'name="' . esc_attr( $defaults['input_name'] ) . '" ';
        $string .= 'title="' . _x( 'Qty','Product Description','woocommerce' ) . '" ';
        $string .= 'class="qty">';
            $string .= $options;
        $string .= '</select>';
    $string .= '</div>';
    
    if ( $echo ) {
        echo $string;
    } else {
        return $string;
    }
}

功能将更改应用于所有数量输入,而不仅仅是商店页面、单一产品页面和购物车页面上的更改。

解决方法

产品作为第二个参数传递给 woocommerce_quantity_input 函数。

所以像这样使用它:

function woocommerce_quantity_input( $args = array(),$product = null,$echo = true ) {
    if ( is_null( $product ) ) {
        $product = $GLOBALS['product'];
    }

    // Is a WC product
    if ( is_a( $product,'WC_Product' ) ) {

        $product_id = $product->get_id();
    
        echo 'Product ID = ' . $product_id;
        // etc..
    }
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。