【问题标题】:Auto enable virtual option by default for a specific product type in WooCommerce默认情况下,为 WooCommerce 中的特定产品类型自动启用虚拟选项
【发布时间】:2019-12-29 11:48:51
【问题描述】:

我有名为“预订”的 Woocommerce 产品类型,我想做的是在创建时自动将其标记为“虚拟”。

基于 "Automatically enable virtual and downloadable product settings" 答案代码,下面的代码将所有产品设置为虚拟,而我只希望“预订”产品类型默认为虚拟,而不是简单或可变产品:

add_action( 'woocommerce_product_options_general_product_data', 'enable_virtual_option' );
function enable_virtual_option(){

?>

        <script>
            (function($){
                $('input[name=_virtual]').prop('checked', true);

            })(jQuery);
        </script>

        <?php

}

【问题讨论】:

    标签: php jquery wordpress woocommerce hook-woocommerce


    【解决方案1】:

    以下将为特定产品类型自动启用虚拟复选框(此处为“预订”类型)

    add_action( 'woocommerce_product_options_general_product_data', 'hiding_and_set_product_settings' );
    function hiding_and_set_product_settings(){
        global $post, $product_object;
    
        if ( $product_object->is_type('booking') ){
            ?>
            <script>
                (function($){
                    $('input[name=_virtual]').prop('checked', true);
                })(jQuery);
            </script>
    
            <?php
        }
    }
    

    代码进入您的活动子主题(或活动主题)的 function.php 文件中。经过测试并且可以工作。

    【讨论】:

      猜你喜欢
      • 2019-01-08
      • 2019-05-19
      • 2021-07-24
      • 2021-01-29
      • 2018-05-29
      • 2019-05-04
      • 2017-06-29
      • 2016-09-04
      • 2023-01-25
      相关资源
      最近更新 更多