向 WooCommerce 管理优惠券设置添加自定义多选字段

问题描述 投票:0回答:1

在 Woocommerce 后端“添加 cupon”部分中,我想创建一个名为“免费产品”的额外自定义字段,其中有 2 个选项“是和否”。 如果是,则会显示多产品选择选项,可以在其中为 1 种或多于 1 种产品添加优惠券。

我尝试了以下方法:

// Add custom field to coupon form
function custom_coupon_fields() {
    woocommerce_wp_select( array(
        'id'      => 'free_product',
        'label'   => __( 'Free Product', 'woocommerce' ),
        'options' => array(
            'no'  => __( 'No', 'woocommerce' ),
            'yes' => __( 'Yes', 'woocommerce' ),
        ),
    ) );
}
add_action( 'woocommerce_coupon_options', 'custom_coupon_fields' );

// Save custom field
function save_custom_coupon_fields( $post_id ) {
    $free_product = isset( $_POST['free_product'] ) ? sanitize_text_field( $_POST['free_product'] ) : '';
    update_post_meta( $post_id, 'free_product', $free_product );
}
add_action( 'woocommerce_coupon_options_save', 'save_custom_coupon_fields' );


// Display conditional field
function conditional_coupon_fields() {
    global $post;
    $free_product = get_post_meta( $post->ID, 'free_product', true );

    if ( $free_product === 'yes' ) {
        woocommerce_wp_select( array(
            'id'          => 'multi_select_product',
            'label'       => __( 'Multi-select Product', 'woocommerce' ),
            'options'     => array(
                'product1' => __( 'Product 1', 'woocommerce' ),
                'product2' => __( 'Product 2', 'woocommerce' ),
                // Add more product options as needed
            ),
            'multiple'    => true,
            'desc_tip'    => true,
            'description' => __( 'Select products', 'woocommerce' ),
        ) );
    }
}
add_action( 'woocommerce_coupon_options', 'conditional_coupon_fields' );

// Save conditional field
function save_conditional_coupon_fields( $post_id ) {
    $free_product = isset( $_POST['free_product'] ) ? sanitize_text_field( $_POST['free_product'] ) : '';

    if ( $free_product === 'yes' ) {
        $multi_select_product = isset( $_POST['multi_select_product'] ) ? $_POST['multi_select_product'] : array();
        update_post_meta( $post_id, 'multi_select_product', $multi_select_product );
    }
}
add_action( 'woocommerce_coupon_options_save', 'save_conditional_coupon_fields' );

但它没有按预期工作。

php wordpress woocommerce custom-fields coupon
1个回答
0
投票

您不需要 2 个字段(一个用于启用自定义选项,另一个用于选择一种或多种免费产品。您可以仅使用一个自定义多选字段,如“使用限制”>“产品”中:

enter image description here

因此,如果您为其设置了一种或多种产品,则会启用此自定义选项。

为此目的,您可以将代码替换为:

// Add custom field to coupon form
add_action( 'woocommerce_coupon_options', 'add_coupon_setting_field_free_products' );
function add_coupon_setting_field_free_products() {
    global $post;

    echo '<div class="options_group free-product" style="border-top: 1px solid #eee;">';
    ?>
    <p class="form-field">
        <label><?php _e( 'Free products', 'woocommerce' ); ?></label>
        <select class="wc-product-search" multiple="multiple" style="width: 50%;" name="free_product_ids[]" data-placeholder="<?php esc_attr_e( 'Search for a product&hellip;', 'woocommerce' ); ?>" data-action="woocommerce_json_search_products_and_variations">
            <?php
            $coupon      = new WC_Coupon( $post->ID );
            $product_ids = is_a($coupon, 'WC_Coupon') ? $coupon->get_meta('free_product_ids') : array();

            foreach ( $product_ids as $product_id ) {
                $product = wc_get_product( $product_id );
                if ( is_object( $product ) ) {
                    echo '<option value="' . esc_attr( $product_id ) . '"' . selected( true, true, false ) . '>' . esc_html( wp_strip_all_tags( $product->get_formatted_name() ) ) . '</option>';
                }
            }
            ?>
        </select>
        <?php echo wc_help_tip( __( 'Select the Free Products for this coupon.', 'woocommerce' ) ); ?>
    </p>
    <?php
    echo '</div>';
}

// Save custom field
add_action( 'woocommerce_coupon_options_save', 'save_coupon_setting_field_free_products', 10, 2 );
function save_coupon_setting_field_free_products( $post_id, $coupon ) {
    $product_ids = isset($_POST['free_product_ids']) ? array_filter( array_map('intval', (array) $_POST['free_product_ids']) ) : array();
    $coupon->update_meta_data( 'free_product_ids', $product_ids );
    $coupon->save();
}

代码位于子主题的functions.php 文件中(或插件中)。已测试并有效。

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.