简体   繁体   English

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

[英]Get product id in “woocommerce_quantity_input” pluggable function

I am using woocommerce_quantity_input pluggable function to modify the quantity input from a text box to a dropdown on my site.我正在使用woocommerce_quantity_input可插入函数将数量输入从文本框修改为我网站上的下拉列表。

On the cart page, when outputting the quantity input, I need to get the product ID so I can grab an ACF field from the single product page.在购物车页面上,当输出数量输入时,我需要获取产品 ID,以便我可以从单个产品页面中获取 ACF 字段。

My code:我的代码:

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', '', $product ),
            'step'         => apply_filters( 'woocommerce_quantity_input_step', '1', $product ),
        );
    } 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', '1', $product ),
            'max_value'     => apply_filters( 'cw_woocommerce_quantity_input_max', '', $product ),
            'min_value'     => apply_filters( 'cw_woocommerce_quantity_input_min', '', $product ),
        );
    }

    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;
    }
}

This function applies the changes to all quantity inputs, not just those on the shop page, the single product page and the cart page.此功能将更改应用于所有数量输入,而不仅仅是商店页面、单一产品页面和购物车页面上的更改。

Product is passed as as 2nd argument to the woocommerce_quantity_input function.产品作为第二个参数传递给woocommerce_quantity_input函数。

So use it like this:所以像这样使用它:

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..
    }
}

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

相关问题 woocommerce_quantity_input() 在商店页面 woocommerce 上不起作用 - woocommerce_quantity_input() not working on shop page woocommerce woocommerce_quantity_input在添加到购物车循环中不起作用 - woocommerce_quantity_input doesn't work in add to cart loop 如果用户在WooCommerce购物车中将数量更改为0,则获取项目的产品ID - Get The Product Id of An Item If User Changes Quantity to 0 in WooCommerce Cart 构建一个函数来获取WooCommerce中产品ID的产品类别ID - Building a function to get the product category ID of a product ID in WooCommerce 从WooCommerce中的产品中检索库存数量的功能 - Function that retrieves the stock quantity from a product in WooCommerce 要获取总增加的产品数量WooCommerce WordPress - To get total added product quantity WooCommerce WordPress 获取 WooCommerce Ajax 上的产品 ID、名称和数量添加到购物车以显示通知 - Get product id, name and quantity on WooCommerce Ajax added to cart to display a notice 在主题的functions.php文件中获取订购的产品详细信息(ID,数量) - WooCommerce - Get ordered product details (ID, quantity) in functions.php file of theme - WooCommerce 在Woocommerce中按商品ID获取购物车商品数量 - Get the cart item quantity by item id in Woocommerce WooCommerce:在普通页面或帖子上显示给定产品 ID 的库存数量 - WooCommerce: Display stock quantity for a given product ID on normal Pages or Posts
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM