隐藏在 Woocommerce 中默认选择具有独特变体的可变产品下拉菜单

Hide variable product dropdown that has a unique variation selected by default in Woocommerce(隐藏在 Woocommerce 中默认选择具有独特变体的可变产品下拉菜单)
本文介绍了隐藏在 Woocommerce 中默认选择具有独特变体的可变产品下拉菜单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我有一个独特的问题.我正在使用他们无法支持请求的插件.我需要将变体拆分为单独的项目,但是如果我复制和粘贴并将它们变成一个简单的产品,那么我无法同步产品的计数以跟踪库存.作为一种解决方法,我需要能够禁用不需要的变体,只保留我确实需要的变体.但这是我遇到麻烦的地方.如果我启用了一个变体,那么我不想显示下拉列表,而是希望它在 UI 上看起来像一个简单的产品.我尝试了一切,但无法让它工作.

I have a unique issue. I am using a plugin where they are not able to support the request. I need to split out variations into separate items, but if I copy and paste and turn them into a simple product, then I can't sync the count for the product to track inventory stock. As a workaround I needed to be able to disable the variations I do not need, keeping only the one that I do need. But here is where I am having trouble. If I have one variation enabled, then I do not want to show the dropdown, and instead want it to look like a simple product on the UI. I tried everything and cannot get it to work.

我什至尝试使用

add_filter( 'woocommerce_hide_invisible_variations', '__return_true', 10, 3 );

没有成功,因为即使计数为 0、价格为 0 且项目已禁用,它们仍可见且未隐藏.

with no success as they are visible and not hidden even though the counts are 0, the price is 0, and the item is disabled.

如何显示没有下拉菜单的产品页面?更进一步;我删除了所有变体,但我需要保留的变体除外.由于同步插件,我需要将其保持在变化模式.如何在不显示任何下拉列表的情况下显示它?

How can I show the product page with no drop-down? Take it one step further; I delete all variations except the one that I need to keep. I need to keep it in variations mode due to the plugin that syncs. How do I display it without any dropdowns showing?

示例逻辑:

如果产品类型是变体并且启用计数 == 1 则特殊 ui 显示,否则正常.

If product type is a variation and enabled count == 1 then special ui display, else normal.

谢谢.

推荐答案

重要提示:代码仅适用当唯一变体被选择为默认表单值时:

IMPORTANT: The code only works when the unique variation is selected as default form value:

以下代码将隐藏仅启用一个变体并默认选择的变量产品、属性下拉列表和所选变体价格:

The following code will hide from variable products that have only one variation enabled and selected by default, the attribute dropdown and the selected variation price:

add_action( 'woocommerce_before_add_to_cart_form', 'single_unique_variation_ui', 10 );
function single_unique_variation_ui(){
    global $product;

    if( ! $product->is_type('variable') )
        return; // Only variable products

    $available_variations = $product->get_available_variations(); // Get available variations
    $default_attributes   = $product->get_default_attributes();   // Get default attributes

    // Only for a unique selected variation by default
    if( ! ( sizeof($available_variations) == 1 && sizeof($default_attributes) == 1 ) )
        return;

    // Get the unique variation
    $variation = reset($available_variations);

    // Loop through
    if( reset($variation['attributes']) == reset($default_attributes) ) :
    // Styles
    ?>
    <style>
        div.woocommerce-variation-price, table.variations { display:none; }
    </style>
    <?php
    endif;
}

代码位于活动子主题(或活动主题)的 function.php 文件中.经测试有效.

Code goes in function.php file of your active child theme (or active theme). Tested and works.

没有代码 (正常的woocommerce行为):

使用代码 (隐藏属性下拉列表和价格):

它将为您提供与简单产品相同的用户界面

It will give you the same UI than simple products

这篇关于隐藏在 Woocommerce 中默认选择具有独特变体的可变产品下拉菜单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

DeepL的翻译效果还是很强大的,如果我们要用php实现DeepL翻译调用,该怎么办呢?以下是代码示例,希望能够帮到需要的朋友。 在这里需要注意,这个DeepL的账户和api申请比较难,不支持中国大陆申请,需要拥有香港或者海外信用卡才行,没账号的话,目前某宝可以
PHP通过phpspreadsheet导入Excel日期,导入系统后,全部变为了4开头的几位数字,这是为什么呢?原因很简单,将Excel的时间设置问文本,我们就能看到该日期本来的数值,上图对应的数值为: 要怎么解决呢?进行数据转换就行,这里可以封装方法,或者用第三方的
mediatemple - can#39;t send email using codeigniter(mediatemple - 无法使用 codeigniter 发送电子邮件)
Laravel Gmail Configuration Error(Laravel Gmail 配置错误)
Problem with using PHPMailer for SMTP(将 PHPMailer 用于 SMTP 的问题)
Issue on how to setup SMTP using PHPMailer in GoDaddy server(关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题)