wordpress 保存并显示从WooCommerce后端到前端的选定值

hjqgdpho  于 11个月前  发布在  WordPress
关注(0)|答案(1)|浏览(113)

我使用设置数量最小值,最大值和步骤在产品级Woocommerce回答代码实现最小值,最大值和步骤Woocommerce产品的数量。
然后,我尝试添加一个新的选择器与2个选项,并显示它之前添加到购物车按钮。所以,添加到代码:

woocommerce_wp_select( array(
        'id' => 'tip_produs', // required
        'label' => __( 'Unitate produs', 'woocommerce' ), // Text in Label
        'class' => 'tip_produs',
        'options' => array(
            'set'    => 'Vand la set',
            'buc'    => 'Vand la buc',
         ),
        'desc_tip' => 'true',
        'description' => 'Alege daca produsul se vinde la bucata sau la set.',
        'value'         => $tip_produs, // <== POPULATING
));

字符串
选择项出现,但我不知道如何保存值并正确显示。我尝试使用$('#tip_produs:selected').text();,但该选项没有selected="selected"属性.


的数据
要在I use this hook前端上显示它,请使用以下命令进行测试:

add_action( 'woocommerce_after_add_to_cart_quantity', 'bbloomer_echo_qty_front_add_cart' );
function bbloomer_echo_qty_front_add_cart() {
   echo '<span class="qty-suff">text</span>'; 
}

y4ekin9u

y4ekin9u1#

我已经更新了Woocommerce答案中产品级别的最小数量设置代码,最大数量和步骤,包括您的“数量单位”选择器 (显示选择字段,保存所选值并在单个产品页面中显示)
完整的revisited代码:

add_action( 'woocommerce_product_options_pricing', 'wc_qty_add_product_field' );
function wc_qty_add_product_field() {
    global $product_object;

    $values   = $product_object->get_meta('_qty_args');

    echo '</div><div class="options_group qty-unit hide_if_grouped">';

    woocommerce_wp_select( array(
        'id'                => 'qty_unit',
        'label'             => __( 'Product unit', 'woocommerce-quantity-step' ),
        'class'             => 'qty_unit',
        'options' => array(
            'set'    => 'Sold as a set',
            'piece'  => 'Sold per piece',
        ),
        'desc_tip'          => 'true',
        'description'       => __( 'Choose if the product is sold per piece or as a set.', 'woocommerce' ),
    ) );

    echo '</div><div class="options_group quantity hide_if_grouped">
    <style>div.qty-args.hidden { display:none; }</style>';

    woocommerce_wp_checkbox( array( // Checkbox.
        'id'            => 'qty_args',
        'label'         => __( 'Quantity settings', 'woocommerce' ),
        'value'         => empty($values) ? 'no' : 'yes',
        'description'   => __( 'Enable this to show and enable the additional quantity setting fields.', 'woocommerce' ),
    ) );

    echo '<div class="qty-args hidden">';

    woocommerce_wp_text_input( array(
        'id'                => 'qty_min',
        'type'              => 'number',
        'label'             => __( 'Minimum Quantity', 'woocommerce-max-quantity' ),
        'placeholder'       => '',
        'desc_tip'          => 'true',
        'description'       => __( 'Set a minimum allowed quantity limit (a number greater than 0).', 'woocommerce' ),
        'custom_attributes' => array( 'step'  => 'any', 'min'   => '0'),
        'value'             => isset($values['qty_min']) && $values['qty_min'] > 0 ? (int) $values['qty_min'] : 0,
    ) );

    woocommerce_wp_text_input( array(
        'id'                => 'qty_max',
        'type'              => 'number',
        'label'             => __( 'Maximum Quantity', 'woocommerce-max-quantity' ),
        'placeholder'       => '',
        'desc_tip'          => 'true',
        'description'       => __( 'Set the maximum allowed quantity limit (a number greater than 0). Value "-1" is unlimited', 'woocommerce' ),
        'custom_attributes' => array( 'step'  => 'any', 'min'   => '-1'),
        'value'             => isset($values['qty_max']) && $values['qty_max'] > 0 ? (int) $values['qty_max'] : -1,
    ) );

    woocommerce_wp_text_input( array(
        'id'                => 'qty_step',
        'type'              => 'number',
        'label'             => __( 'Quantity step', 'woocommerce-quantity-step' ),
        'placeholder'       => '',
        'desc_tip'          => 'true',
        'description'       => __( 'Optional. Set quantity step  (a number greater than 0)', 'woocommerce' ),
        'custom_attributes' => array( 'step'  => 'any', 'min'   => '1'),
        'value'             => isset($values['qty_step']) && $values['qty_step'] > 1 ? (int) $values['qty_step'] : 1,
    ) );
    echo '</div>';
}

// Show/hide setting fields (admin product pages)
add_action( 'admin_footer', 'product_type_selector_filter_callback' );
function product_type_selector_filter_callback() {
    global $pagenow, $post_type;

    if( in_array($pagenow, array('post-new.php', 'post.php') ) && $post_type === 'product' ) :
    ?>
    <script>
    jQuery(function($){
        if( $('input#qty_args').is(':checked') && $('div.qty-args').hasClass('hidden') ) {
            $('div.qty-args').removeClass('hidden')
        }
        $('input#qty_args').click(function(){
            if( $(this).is(':checked') && $('div.qty-args').hasClass('hidden')) {
                $('div.qty-args').removeClass('hidden');
            } else if( ! $(this).is(':checked') && ! $('div.qty-args').hasClass('hidden')) {
                $('div.qty-args').addClass('hidden');
            }
        });
    });
    </script>
    <?php
    endif;
}

// Save quantity setting fields values
add_action( 'woocommerce_admin_process_product_object', 'wc_save_product_quantity_settings' );
function wc_save_product_quantity_settings( $product ) {
    if ( isset($_POST['qty_unit']) ) {
        $product->update_meta_data( 'qty_unit', esc_attr($_POST['qty_unit']) );
    }

    if ( isset($_POST['qty_args']) ) {
        $values = $product->get_meta('_qty_args');

        $product->update_meta_data( '_qty_args', array(
            'qty_min' => isset($_POST['qty_min']) && $_POST['qty_min'] > 0 ? (int) wc_clean($_POST['qty_min']) : 0,
            'qty_max' => isset($_POST['qty_max']) && $_POST['qty_max'] > 0 ? (int) wc_clean($_POST['qty_max']) : -1,
            'qty_step' => isset($_POST['qty_step']) && $_POST['qty_step'] > 1 ? (int) wc_clean($_POST['qty_step']) : 1,
        ) );
    } else {
        $product->update_meta_data( '_qty_args', array() );
    }
}

// Display quantity unit in single product pages
add_action( 'woocommerce_after_add_to_cart_quantity', 'display_product_selected_quantity_unit' );
function display_product_selected_quantity_unit() {
    global $product;

    $qty_unit = $product->get_meta('qty_unit');

    printf( '<span class="qty-unit">%s</span>', empty($qty_unit) ? 'set' : $qty_unit ); 
}

// The quantity settings in action on front end
add_filter( 'woocommerce_quantity_input_args', 'filter_wc_quantity_input_args', 99, 2 );
function filter_wc_quantity_input_args( $args, $product ) {
    if ( $product->is_type('variation') ) {
        $parent_product = wc_get_product( $product->get_parent_id() );
        $values  = $parent_product->get_meta( '_qty_args' );
    } else {
        $values  = $product->get_meta( '_qty_args' );
    }

    if ( ! empty( $values ) ) {
        // Min value
        if ( isset( $values['qty_min'] ) && $values['qty_min'] > 1 ) {
            $args['min_value'] = $values['qty_min'];

            if( ! is_cart() ) {
                $args['input_value'] = $values['qty_min']; // Starting value
            }
        }

        // Max value
        if ( isset( $values['qty_max'] ) && $values['qty_max'] > 0 ) {
            $args['max_value'] = $values['qty_max'];

            if ( $product->managing_stock() && ! $product->backorders_allowed() ) {
                $args['max_value'] = min( $product->get_stock_quantity(), $args['max_value'] );
            }
        }

        // Step value
        if ( isset( $values['qty_step'] ) && $values['qty_step'] > 1 ) {
            $args['step'] = $values['qty_step'];
        }
    }
    return $args;
}

// Ajax add to cart, set "min quantity" as quantity on shop and archives pages
add_filter( 'woocommerce_loop_add_to_cart_args', 'filter_loop_add_to_cart_quantity_arg', 10, 2 );
function filter_loop_add_to_cart_quantity_arg( $args, $product ) {
    $values  = $product->get_meta( '_qty_args' );

    if ( ! empty( $values ) ) {
        // Min value
        if ( isset( $values['qty_min'] ) && $values['qty_min'] > 1 ) {
            $args['quantity'] = $values['qty_min'];
        }
    }
    return $args;
}

// The quantity settings in action on front end (For variable productsand their variations)
add_filter( 'woocommerce_available_variation', 'filter_wc_available_variation_price_html', 10, 3);
function filter_wc_available_variation_price_html( $data, $product, $variation ) {
    $values  = $product->get_meta( '_qty_args' );

    if ( ! empty( $values ) ) {
        if ( isset( $values['qty_min'] ) && $values['qty_min'] > 1 ) {
            $data['min_qty'] = $values['qty_min'];
        }

        if ( isset( $values['qty_max'] ) && $values['qty_max'] > 0 ) {
            $data['max_qty'] = $values['qty_max'];

            if ( $variation->managing_stock() && ! $variation->backorders_allowed() ) {
                $data['max_qty'] = min( $variation->get_stock_quantity(), $data['max_qty'] );
            }
        }
    }

    return $data;
}

字符串
代码放在你的子主题的functions.php文件中(或插件中)。测试和工作。


的数据


相关问题