php - 在 woocommerce_loop_add_to_cart_link Hook 后添加Quantity_inputs而不调用它

标签 php wordpress woocommerce hook-woocommerce

我在 function.php 中添加了一段代码,用于在所有页面中添加到购物车后添加数量部分。

但我不想将其也添加到愿望 list 页面中,

所以我这样做:

add_filter( 'woocommerce_loop_add_to_cart_link', 'quantity_inputs_for_woocommerce_loop_add_to_cart_link', 10, 2 );
function quantity_inputs_for_woocommerce_loop_add_to_cart_link( $html, $product ) {
    if (!is_page('wishlist')) {
    if ( $product && $product->is_type( 'simple' ) && $product->is_purchasable() && $product->is_in_stock() && ! $product->is_sold_individually() ) {
        $html = '<form action="' . esc_url( $product->add_to_cart_url() ) . '" class="cart" method="post" enctype="multipart/form-data">';
        // Access the cart items
        $in_cart = false;
        foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
            $product_id = $cart_item['product_id'];
            $quantity = $cart_item['quantity'];
            if($product_id == $product->get_ID()){
                $in_cart = true;
                break;
            }
        }
        // If we have match update quantity
        if($in_cart) {
            $html .= woocommerce_quantity_input( array('input_value' => $quantity), $product, false );
        } else {
            $html .= woocommerce_quantity_input( array(), $product, false );
        }
        $html .= '<button type="submit" class="button alt">' . esc_html( $product->add_to_cart_text() ) . '</button>';
        $html .= '</form>';
    }
    return $html;
}
}

它有效,但问题是它删除了愿望 list 页面上的“添加到购物车”按钮

我做错了什么? 我可以在不调用 woocommerce_loop_add_to_cart_link Hook 的情况下做到这一点

因为如果我将其从过滤器中删除,该按钮会再次出现

最佳答案

您需要像这样在底部添加return $html;

add_filter( 'woocommerce_loop_add_to_cart_link', 'quantity_inputs_for_woocommerce_loop_add_to_cart_link', 10, 2 );
function quantity_inputs_for_woocommerce_loop_add_to_cart_link( $html, $product ) {
    if (!is_page('wishlist')) {
        if ( $product && $product->is_type( 'simple' ) && $product->is_purchasable() && $product->is_in_stock() && ! $product->is_sold_individually() ) {
            $html = '<form action="' . esc_url( $product->add_to_cart_url() ) . '" class="cart" method="post" enctype="multipart/form-data">';
            // Access the cart items
            $in_cart = false;
            foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
                $product_id = $cart_item['product_id'];
                $quantity = $cart_item['quantity'];
                if($product_id == $product->get_ID()){
                    $in_cart = true;
                    break;
                }
            }
            // If we have match update quantity
            if($in_cart) {
                $html .= woocommerce_quantity_input( array('input_value' => $quantity), $product, false );
            } else {
                $html .= woocommerce_quantity_input( array(), $product, false );
            }
            $html .= '<button type="submit" class="button alt">' . esc_html( $product->add_to_cart_text() ) . '</button>';
            $html .= '</form>';
        }
        return $html;
    }
    return $html;
}

关于php - 在 woocommerce_loop_add_to_cart_link Hook 后添加Quantity_inputs而不调用它,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72875615/

相关文章:

php - 如何在 PHP (CSS & Jquery) Video.js WordPress 插件中添加额外链接?

php - Laravel 5 - 使用 Mockery 模拟 Eloquent 模型

php - 使用php将excel数据导入数据库表

mysql - WordPress 自定义类型帖子下一个上一个连接表

wordpress - 让 .htaccess 阻止子域,除非来自其他某些子域(对于 wp-admin 安全 WordPress)

php - Woocommerce 自定义用户输入字段

php - AJAX 间隔函数对服务器有害吗?

php - wordpress csv 文件批量导入帮助

wordpress - Woocommerce:如何为每个购买的商品生成唯一 ID?

php - 使 WooCommerce 结帐运输字段可见并删除 "Ship to different address?"复选框