在WooCommerce常规设置页面上插入复选框以购买过滤器

时间:2019-08-29 09:07:06

标签: php wordpress woocommerce product hook-woocommerce

我正在尝试在General settings WooCommerce页面上创建一个额外的选项,但是无法正常工作,因此我尝试使用advanced tab,它似乎可以正常工作。

此处的目标是创建一个复选框选项,该选项通过为is_purchasable应用过滤器来启用目录模式。

但是,我无法确定的是,如果该复选框标记为已保存设置,如何为woocommerce_is_purchasable应用和保存过滤器。

这是我到目前为止所得到的:

add_filter( 'woocommerce_get_sections_advanced', 'catalog_mode_add_section' );
add_filter( 'woocommerce_get_settings_advanced', 'catalog_mode_all_settings', 10, 2 );

function catalog_mode_add_section( $sections ) {

    $sections['catalog-mode'] = __( 'Catalog Mode', 'text-domain' );
    return $sections;

}

function catalog_mode_all_settings( $settings, $current_section ) {

    if ( $current_section == 'catalog-mode' ) {
        $settings_catalog_options = array();

        // Add Title to the Settings
        $settings_catalog_options[] = array( 'name' => __( 'WooCommerce Catalog Mode', 'text-domain' ), 'type' => 'title', 'desc' => __( 'This turns WooCommerce into a catalog.', 'text-domain' ), 'id' => 'catalog_mode' );

        // Add second text field option
        $settings_catalog_options[] = array(
            'name'     => __( 'Catalog Mode', 'text-domain' ),
            'id'       => 'catalog_mode',
            'type'     => 'checkbox',
        );

        $settings_catalog_options[] = array( 'type' => 'sectionend', 'id' => 'catalog_mode' );
        return $settings_catalog_options;

    } else {
        return $settings;
    }
}

我现在迷路了。 有人吗?

1 个答案:

答案 0 :(得分:0)

您的代码中有一个小错误,其中每个设置组件都需要一个唯一的标识符(id)。我已经更新了您的代码,现在保存了您的自定义选项。

add_filter( 'woocommerce_get_sections_advanced', 'catalog_mode_add_section' );
function catalog_mode_add_section( $sections ) {
    $sections['catalog-mode'] = __( 'Catalog Mode', 'text-domain' );

    return $sections;

}

add_filter( 'woocommerce_get_settings_advanced', 'catalog_mode_all_settings', 10, 2 );
function catalog_mode_all_settings( $settings, $current_section ) {

    if ( $current_section == 'catalog-mode' ) {

        $settings_catalog_options = array();

        // Add Title to the Settings
        $settings_catalog_options[] = array(
            'name' => __( 'WooCommerce Catalog Mode', 'text-domain' ),
            'type' => 'title',
            'desc' => __( 'This turns WooCommerce into a catalog.', 'text-domain' ),
            'id'   => 'wc_catalog_mode_title'
        );

        // Add second text field option
        $settings_catalog_options[] = array(
            'name' => __( 'Catalog Mode', 'text-domain' ),
            'type' => 'checkbox',
            'id'   => 'wc_catalog_mode',
        );

        $settings_catalog_options[] = array(
            'type' => 'sectionend',
            'id'   => 'wc_catalog_mode_end'
        );

        return $settings_catalog_options;
    }
    return $settings;
}

然后在woocommerce_is_purchasablewoocommerce_variation_is_purchasable过滤器中,将以这种方式使用它:

add_filter('woocommerce_is_purchasable', 'product_is_purchasable_filter_callback', 10, 2 );
add_filter( 'woocommerce_variation_is_purchasable', 'product_is_purchasable_filter_callback', 10, 2 );
function product_is_purchasable_filter_callback( $purchasable, $product ) {
    if( 'yes' === get_option('wc_catalog_mode') ) {
        $purchasable = false;
    }
    return $purchasable;
}

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

  

您可以使用“产品”部分而不是“高级”部分来替换钩子:

     
      
  • woocommerce_get_sections_products
  •   
  • woocommerce_get_settings_products
  •