以编程方式为 WooCommerce 中的特定可变产品设置最小、最大和步骤数量

2024-04-05

I use 购物车更新后 WooCommerce 购物车数量不会改变 https://stackoverflow.com/questions/65714608/woocommerce-cart-quantity-wont-change-after-cart-update/65715142#65715142用于自定义某些产品数量参数的应答代码(作为最小、最大和步长参数).

现在我有一个产品 ID 为 27525 的可变产品,我想仅对该产品应用另一个规则,
数量规则详细信息:最小(默认)数量1, 最大数量24和步骤1.

我尝试使用下面的代码,问题是,
如果没有选择变化,则默认/最小数量为25,规则与一般数量设置相同,
如果我选择可用的变体,则默认数量将为 24。

add_filter( 'woocommerce_available_variation', 'variation_quantity_input_args', 10, 2 );
function variation_quantity_input_args( $args, $product ) {
    $sample_product_id = array(27525);

    if (in_array( $product->get_id(), $sample_product_id)) {
        $args['min_qty'] = 1;
        $args['max_qty'] = 24;
        return $args;
    }
}

如何更改代码并将规则应用于变量产品 27525?

$args['min_qty'] = 1; //default and min qty
$args['max_qty'] = 24; // max qty
$args['step'] = 1; // Seems won't work use woocommerce_available_variation, but I want to change the step to 1

The woocommerce_available_variation https://woocommerce.github.io/code-reference/files/woocommerce-includes-class-wc-product-variable.html#source-view.364钩子有WC_Product_Variation 产品对象 as its 第三个参数 and 不是可变产品.

随着woocommerce_available_variation hook 你不能设置 产品步骤.

可用的参数以下是(源代码WooCommerce /includes/class-wc-product-variable.php文件@版本3.0.0):

return apply_filters(
    'woocommerce_available_variation',
    array(
        'attributes'            => $variation->get_variation_attributes(),
        'availability_html'     => wc_get_stock_html( $variation ),
        'backorders_allowed'    => $variation->backorders_allowed(),
        'dimensions'            => $variation->get_dimensions( false ),
        'dimensions_html'       => wc_format_dimensions( $variation->get_dimensions( false ) ),
        'display_price'         => wc_get_price_to_display( $variation ),
        'display_regular_price' => wc_get_price_to_display( $variation, array( 'price' => $variation->get_regular_price() ) ),
        'image'                 => wc_get_product_attachment_props( $variation->get_image_id() ),
        'image_id'              => $variation->get_image_id(),
        'is_downloadable'       => $variation->is_downloadable(),
        'is_in_stock'           => $variation->is_in_stock(),
        'is_purchasable'        => $variation->is_purchasable(),
        'is_sold_individually'  => $variation->is_sold_individually() ? 'yes' : 'no',
        'is_virtual'            => $variation->is_virtual(),
        'max_qty'               => 0 < $variation->get_max_purchase_quantity() ? $variation->get_max_purchase_quantity() : '',
        'min_qty'               => $variation->get_min_purchase_quantity(),
        'price_html'            => $show_variation_price ? '<span class="price">' . $variation->get_price_html() . '</span>' : '',
        'sku'                   => $variation->get_sku(),
        'variation_description' => wc_format_content( $variation->get_description() ),
        'variation_id'          => $variation->get_id(),
        'variation_is_active'   => $variation->variation_is_active(),
        'variation_is_visible'  => $variation->variation_is_visible(),
        'weight'                => $variation->get_weight(),
        'weight_html'           => wc_format_weight( $variation->get_weight() ),
    ),
    $this,
    $variation
);

要设置产品变体的步骤,您需要使用woocommerce_quantity_input_args https://woocommerce.github.io/code-reference/files/woocommerce-includes-wc-template-functions.html#source-view.1727 hook.

如果您想为所有产品变体设置相同的步骤你可以使用这个钩子。

可用的参数以下是(源代码WooCommerce /includes/wc-template-functions.php文件@版本2.5.0):

$defaults = array(
    'input_id'     => uniqid( 'quantity_' ),
    'input_name'   => 'quantity',
    'input_value'  => '1',
    'classes'      => apply_filters( 'woocommerce_quantity_input_classes', array( 'input-text', 'qty', 'text' ), $product ),
    'max_value'    => apply_filters( 'woocommerce_quantity_input_max', -1, $product ),
    'min_value'    => apply_filters( 'woocommerce_quantity_input_min', 0, $product ),
    'step'         => apply_filters( 'woocommerce_quantity_input_step', 1, $product ),
    'pattern'      => apply_filters( 'woocommerce_quantity_input_pattern', has_filter( 'woocommerce_stock_amount', 'intval' ) ? '[0-9]*' : '' ),
    'inputmode'    => apply_filters( 'woocommerce_quantity_input_inputmode', has_filter( 'woocommerce_stock_amount', 'intval' ) ? 'numeric' : '' ),
    'product_name' => $product ? $product->get_title() : '',
    'placeholder'  => apply_filters( 'woocommerce_quantity_input_placeholder', '', $product ),
);

如果您想将产品步骤、最小数量和最大数量作为产品的自定义元进行管理,参见@LoicTheAztec的回答:

  • 在 Woocommerce 中设置产品级别的最小数量、最大数量和步骤 https://stackoverflow.com/questions/62943477/set-quantity-minimum-maximum-and-step-at-product-level-in-woocommerce#62946194

ANSWER

为了回答你的问题,如果您想要父变量产品 ID 等于 27525 的所有产品变体具有以下值:

  • 最小数量: 1
  • 最大数量: 24
  • Step: 1

您可以使用以下功能:

它适用于产品页面、购物车和循环。

// set the min and max quantity for each product variation (based on the variable product id)
add_filter( 'woocommerce_available_variation', 'variation_quantity_input_args', 10, 3 );
function variation_quantity_input_args( $args, $product, $variation ) {
    // set variable product ids
    $sample_product_id = array( 27525 );
    // if the selected variation belongs to one of the variable product ids
    // set the min and max quantity
    if ( in_array( $variation->get_parent_id(), $sample_product_id ) ) {
        $args['min_qty'] = 1;
        $args['max_qty'] = 24;
        return $args;
    }
    return $args;
}


// set the step for specific variable products (and for each product variation)
add_filter( 'woocommerce_quantity_input_args', 'set_step_for_specific_variable_products', 10, 2 );
function set_step_for_specific_variable_products( $args, $product ) {
    // set variable product ids
    $sample_product_id = array( 27525 );
    
    // on the product page
    if ( ! is_cart() ) {
        // if the id of the variable product is in the array
        if ( in_array( $product->get_id(), $sample_product_id ) ) {
            $args['input_value'] = 1;
            $args['min_value'] = 1;
            $args['max_value'] = 24;
            $args['step'] = 1;
        }
    // in the cart
    } else {
        // if the id of the product variation belongs to a variable product present in the array
        if ( in_array( $product->get_parent_id(), $sample_product_id ) ) {
            $args['min_value'] = 1;
            $args['step'] = 1;
        }
    }
    return $args;
}

该代码已经过测试并且可以工作。将其添加到活动主题的functions.php中。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

以编程方式为 WooCommerce 中的特定可变产品设置最小、最大和步骤数量 的相关文章

  • 如何将 JSON 数据从 Android 发送到 php url?

    我想将登录信息从我的应用程序发送到 php url 因为这我的应用程序将崩溃 任何人都可以帮助我解决这个问题 这是我的服务器登录方法 我想将数据发送到此登录方法 Method public method login Parameters 3
  • 将字符串分解为标记,保持引用的子字符串完整

    我不知道我在哪里看到它 但是谁能告诉我如何使用 php 和 regex 来完成这个任务 this is a string that has quoted text inside 我希望能够像这样爆炸它 0 this 1 is 2 a 3 s
  • PHP 启动:无法加载动态库 php5.4.3/ext/php_ffmpeg.dll 不是有效的 Win32 应用程序

    再会 我尝试在 Windows 7 计算机上安装 dll 文件 php ffmpeg 但不断收到此错误 29 Jan 2013 11 37 00 UTC PHP Warning PHP Startup Unable to load dyna
  • 在 PHP 中设置 HTTP 响应代码(在 Apache 下)

    给出以下两种在 PHP 中设置 HTTP 响应代码的方法 具体来说 在 Apache 下 方法一 http response code 404 方法二 header HTTP 1 0 404 Not Found 我的问题是 除了这个事实之外
  • 如何在PHP中获取div中的所有链接

    我想从另一个网站打开一个页面 并提取一个中的所有链接 href div of class layout 2 2 在此页面中 我如何使用 PHP 来做到这一点 我想复制layout 2 2中的每个链接this https url 网页 这是我
  • 如何使用 PHP 查找字符串中字符的序列模式?

    假设我有随机的文本块 EAMoAAQAABwEBAAAAAAAAAAAAAAABAgMFBgcIBAkBAQABBQEBAAAAAAAAAAAAAAAGAgMEBQcBCBAAAQMDAgMEBQcIBQgGCwEAAQACAxEEBSEG
  • 是否可以在 PHP 中使用 file_get_contents 来破坏 CSRF 令牌验证

    在每个会话的表单上使用令牌的 CSRF 预防方法是一种流行的方法 但是 我不明白这种令牌方式如何保护file get contentsPHP 可以获取跨域文件表单的内容 gt 它可以获取表单上的令牌并使用它 那么这种token方式是如何运作
  • CakePHP - 选择性 SSL

    如何对网站的某些部分强制使用 HTTPS 例如登录页面或注册页面 并使用 HTTP 来完成网站的其余部分 我最喜欢的强制转换为 https 的方法是将其作为 php 脚本中的第一件事 它可以在 Joomla 中运行 也可以在 CakePHP
  • 使用 php 脚本的电子邮件管道

    你好 我想将所有电子邮件 到达我的收件箱 转发到 php 脚本并检索电子邮件内容并将其保存在文件中 因此 我正确地添加了具有管道路径的电子邮件转发器 转发地址 电子邮件受保护 cdn cgi l email protection 管道到程序
  • 如何使用 AJAX/jQuery 显示打印内容?

    所以我试图理解整个 AJAX jQuery 的事情 现在 当我单独运行这个 PHP 脚本时 我必须等待并观察轮子旋转 直到循环完成然后加载 while row mysql fetch array res postcode to storm
  • 优化数据可视化 Web 应用程序的性能

    我正在重写 3 年前编写的数据可视化网络工具 从那时起 浏览器的 JavaScript 引擎变得更快 所以我正在考虑将部分工作从服务器转移到客户端 在页面上 数据在表格和地图 或图表 中可视化 它使用相同的数据 但以不同的方式 因此准备显示
  • laravel 5.3 新的 Auth::routes()

    最近开始使用laravel 5 3写博客 但是运行后出现一个问题php artisan make auth 当我运行这个时 它会在我的web php 这是其中的代码 Auth routes Route get home HomeContro
  • 如何在之前的 Facebook 身份验证后自动安全地让用户登录?

    用户抱怨他们必须过于频繁地登录 如果身份验证完全基于 Facebook OAuth 那么用户如何在下次访问该页面时自动登录 用户流程示例 用户点击 使用 Facebook 登录 用户通过 Facebook 进行身份验证并被重定向回网站 用户
  • PHP MVC 应用程序中哪里可以捕获异常?

    我有一个中小型 PHP 应用程序 用于练习 OOP 和 MVC 技能 我有初始化 引导程序调用的文件Router谁打电话控制器 gt 服务层 gt 存储库 数据库 然后将变量发送回视图层 所有依赖项均由 DiC IOC 处理 我创建抽象类
  • PHP 的 mb_internal_encoding 实际上是做什么的?

    根据 PHP 网站 http www php net manual en function mb internal encoding php它这样做 coding 是用于 HTTP 输入的字符编码名称 字符编码转换 HTTP输出字符编码 转
  • 访问 Magento 购物车和/或结帐中的运费

    请注意 这个问题是关于运费 而不是价格 有一个重要的区别 即运输方式为店主支付的费用是多少 而不是客户支付的费用 The shipping tablerate数据库表包括一个cost字段 该字段填充在Mage Shipping Model
  • Microsoft VS Code:当我尝试启动程序时,出现错误“spawn php ENOENT”

    我正在尝试在 Microsoft VS Code 上运行 PHP 代码 当我单击启动时 唯一发生的事情是调试控制台中出现错误 生成 php ENOENT 为了解决这个问题 我将 XDebug 的 dll 文件放入 ext 文件夹中 我将 p
  • shell_exec 的输出被截断为 100 个字符

    当在 shell 中运行以下命令时 curl F file filename http 192 168 0 1 产生以下输出 Accuracy 0 0 1 classification Accuracy 0 0 1 classificati
  • 通过php将mp3转换为ogg

    我有一个网站 用户可以上传音乐并将其转换为 mp3 但我需要 mp3 和 ogg 文件支持才能以 html5 播放音乐 那么 有没有可以将mp3转换为ogg的php脚本呢 使用 ffmpeg 您可以直接从 php 脚本执行命令
  • preg_match 所有以@开头的单词?

    我对正则表达式不太确定 所以我不得不问你 如何用 PHP 判断字符串中是否包含以 开头的单词 例如我有一个像 This is for codeworxx 这样的字符串 我很抱歉 但我没有任何起点 希望你能帮忙 谢谢 萨沙 好的 谢谢你的结果

随机推荐