wordpress 类别变更标签缺货Woocommerce

cygmwpex  于 2022-11-22  发布在  WordPress
关注(0)|答案(1)|浏览(151)

我需要添加一个css样式类,只用于没有库存的“标签”产品。
原始代码示例:

<label class="radio-label sw-radio-variation sw-radio-variation-2" title="l" for="l_2">

我要的代码:

<label class="out-stock radio-label sw-radio-variation sw-radio-variation-2" title="l" for="l_2">

如你所见添加到CSS代码缺货
谢谢
编辑:我的product-variation.php是

<?php 

/*

** Product variation hook

*/

add_filter( 'woocommerce_dropdown_variation_attribute_options_html', 'sw_woocommerce_custom_variation', 10, 2 );

function sw_woocommerce_custom_variation( $html, $args ){

    $options   = $args['options'];

    $product   = $args['product'];

    $attribute = $args['attribute'];

    $name      = $args['name'] ? $args['name'] : 'attribute_' . sanitize_title( $attribute );

    $id        = $args['id'] ? $args['id'] : sanitize_title( $attribute );

    $class     = $args['class'];


    if ( empty( $options ) && ! empty( $product ) && ! empty( $attribute ) ) {

        $attributes = $product->get_variation_attributes();

        $options    = $attributes[ $attribute ];

    }

    $html = '';

    if ( ! empty( $options ) ) {

        $html .= '<div class="sw-custom-variation">';   

        if ( $product && taxonomy_exists( $attribute ) ) {

            // Get terms if this is a taxonomy - ordered. We need the names too.

            $terms = wc_get_product_terms( $product->get_id(), $attribute, array( 'fields' => 'all' ) );                                

            foreach ( $terms as $key => $term ) {

                $color          = get_term_meta( $term->term_id, 'sw_variation_color', true );

                $active         = ( checked( sanitize_title( $args['selected'] ), $term->slug, false ) ) ? ' selected' : '';

                $attributes = ( preg_match( '/color|colors/', $attribute, $match ) && $color != '' ) ? 'class="variation-color" style="background: '. esc_attr( $color ) .'"' : '';


                if ( in_array( $term->slug, $options ) ) {

                    $html .= '<label class="radio-label sw-radio-variation sw-radio-variation-'. esc_attr( $key .' '. $active ) .'" title="'. esc_attr( $term->slug )  .'" for="'. esc_attr( $term->slug . '_' . $key ) . '">';

                    $html .= '<input type="radio" id="'.  esc_attr( $term->slug . '_' . $key ) .'" name="' . esc_attr( $name ) . '" data-attribute_name="attribute_' . esc_attr( sanitize_title( $attribute ) ) . '" value="' . esc_attr( $term->slug ) . '" '. checked( sanitize_title( $args['selected'] ), $term->slug, false ) .'/>';

                    $html .= '<span '. $attributes .'>'. $term->name .'</span>';

                    $html .= '</label>';

                }

            }           

        }else {

            foreach ( $options as $key => $option ) {

                // This handles < 2.4.0 bw compatibility where text attributes were not sanitized.

                $checked = sanitize_title( $args['selected'] ) === $args['selected'] ? checked( $args['selected'], sanitize_title( $option ), false ) : checked( $args['selected'], $option, false );

                $active = ( $checked ) ? 'selected' : '';


                $html .= '<label class="radio-label sw-radio-variation sw-radio-variation-'. esc_attr( $key .' '. $active ) .'"  title="'. esc_attr( $option )  .'"  for="'. esc_attr( $option . '_' . $key ) . '">';

                $html .= '<input type="radio" id="'.  esc_attr( $option . '_' . $key ) .'" name="' . esc_attr( $name ) . '" data-attribute_name="attribute_' . esc_attr( sanitize_title( $attribute ) ) . '" value="' . esc_attr( $option ) . '" '. $checked .'/>';

                $html .= '<span>'. $option .'</span>';

                $html .= '</label>';

            }

        }

        $html .= '</div>';

    }

    return $html;

}


/*

** Action hook to color

*/


$sw_attribute_taxonomies = wc_get_attribute_taxonomies();

if ( ! empty( $sw_attribute_taxonomies ) ) {

    foreach( $sw_attribute_taxonomies as $attr ){

        if( preg_match( '/color|colors/', $attr->attribute_name, $match ) ){

            add_action( 'pa_'. $attr->attribute_name .'_add_form_fields', 'sw_woocommerce_variation_fields', 200 );

            add_action( 'pa_'. $attr->attribute_name .'_edit_form_fields', 'sw_woocommerce_edit_variation_fields', 200 );

            add_action( 'created_term', 'sw_woocommerce_save_variation_fields', 10, 3 );

            add_action( 'edit_terms', 'sw_woocommerce_save_variation_fields', 10, 3 );


            /* Enqueue Admin js */

            add_action( 'admin_enqueue_scripts', 'sw_woocommerce_variation_color_script' ); 

        }

    }

}


/*

** Create color

*/

function sw_woocommerce_variation_fields() {

    ?>

    <div class="form-field custom-picker">

        <label for="sw_variation_color"><?php _e( 'Color', 'sw_woocommerce' ); ?></label>

        <input name="sw_variation_color" id="sw_variation_color" type="text" value="" size="40" class="category-colorpicker"/>

    </div>

    <?php

}


function sw_woocommerce_edit_variation_fields( $term ) {


    $sw_variation_color = get_term_meta( $term->term_id, 'sw_variation_color', true );


    ?>

    <tr class="form-field custom-picker custom-picker-edit">

        <th scope="row" valign="top"><label for="sw_variation_color"><?php _e( 'Color', 'sw_woocommerce' ); ?></label></th>

        <td>

            <input name="sw_variation_color" id="sw_variation_color" type="text" value="<?php echo esc_attr( $sw_variation_color ) ?>" size="40" class="category-colorpicker"/>

        </td>

    </tr>

    <?php

}


/** Save Custom Field Of Category Form */

function sw_woocommerce_save_variation_fields( $term_id, $tt_id = '', $taxonomy = '', $prev_value = '' ) {

    if ( isset( $_POST['sw_variation_color'] ) ) {          

        $term_value = esc_attr( $_POST['sw_variation_color'] );

        update_term_meta( $term_id, 'sw_variation_color', $term_value, $prev_value );

    }

}


function sw_woocommerce_variation_color_script(){

    wp_enqueue_style( 'wp-color-picker' ); 

    wp_enqueue_script('category_color_picker_js', WCURL . '/js/admin/category_color_picker.js', array( 'wp-color-picker' ), false, true);

}

正如你在第60行看到的,你会发现标签是否有任何代码来添加一个自定义的CSS到这些标签,创建变量?

*****解决方案

<?php
    // Adiccional Class For Label Variations
    function get_variation_stock_text( $product, $name, $term_slug ){
        foreach ( $product->get_available_variations() as $variation ){
            if($variation['attributes'][$name] == $term_slug ){
                $is_in_stock = $variation['is_in_stock'];
                $stock_qty   = get_post_meta($variation['variation_id'], '_stock', true);
            }
        }
        $in_stock     = ' '.__("katayainstock", "woocommerce").'';
        $out_of_stock = ' '.__("katayaoutstock", "woocommerce").'';

        return $is_in_stock == 1 ? $in_stock : $out_of_stock;
    }
        /**
         * Product variation hook
         */
        add_filter( 'woocommerce_dropdown_variation_attribute_options_html', 'sw_woocommerce_custom_variation', 10, 2 );
        function sw_woocommerce_custom_variation( $html, $args ) {
            $options   = $args['options'];
            $product   = $args['product'];
            $attribute = $args['attribute'];
            $name      = $args['name'] ? $args['name'] : 'attribute_' . sanitize_title( $attribute );
            $id        = $args['id'] ? $args['id'] : sanitize_title( $attribute );
            $class     = $args['class'];

            if ( empty( $options ) && ! empty( $product ) && ! empty( $attribute ) ) {
                $attributes = $product->get_variation_attributes();
                $options    = $attributes[ $attribute ];
            }

            $html = '';

            if ( ! empty( $options ) ) {

                $html .= '<div class="sw-custom-variation">';

                if ( $product && taxonomy_exists( $attribute ) ) {
                    // Get terms if this is a taxonomy - ordered. We need the names too.
                    $terms = wc_get_product_terms( $product->get_id(), $attribute, array( 'fields' => 'all' ) );

                    foreach ( $terms as $key => $term ) {
                        $color      = get_term_meta( $term->term_id, 'sw_variation_color', true );
                        $active     = checked( sanitize_title( $args['selected'] ), $term->slug, false ) ? ' selected' : '';
                        $attributes = ( preg_match( '/color|colors/', $attribute, $match ) && $color !== '' ) ? 'class="variation-color" style="background: ' . esc_attr( $color ) . '"' : '';

                        if ( in_array( $term->slug, $options, true ) ) {
                            $stock_text = get_variation_stock_text( $product, $name, $term->slug );
                            $html .= '<label class="' . $stock_text . ' radio-label sw-radio-variation sw-radio-variation-' . esc_attr( $key . ' ' . $active ) . '" title="' . esc_attr( $term->slug ) . '" for="' . esc_attr( $term->slug . '_' . $key ) . '">';
                            $html .= '<input type="radio" id="' . esc_attr( $term->slug . '_' . $key ) . '" name="' . esc_attr( $name ) . '" data-attribute_name="attribute_' . esc_attr( sanitize_title( $attribute ) ) . '" value="' . esc_attr( $term->slug ) . '" ' . checked( sanitize_title( $args['selected'] ), $term->slug, false ) . '/>';
                            $html .= '<span ' . $attributes . '>' . $term->name . '</span>';
                            $html .= '</label>';
                        }
                    }
                } else {
                    foreach ( $options as $key => $option ) {
                        // This handles < 2.4.0 bw compatibility where text attributes were not sanitized.
                        $checked = sanitize_title( $args['selected'] ) === $args['selected'] ? checked( $args['selected'], sanitize_title( $option ), false ) : checked( $args['selected'], $option, false );
                        $active  = ( $checked ) ? 'selected' : '';
                        $html    .= '<label class="radio-label sw-radio-variation sw-radio-variation-' . esc_attr( $key . ' ' . $active ) . '"  title="' . esc_attr( $option ) . '"  for="' . esc_attr( $option . '_' . $key ) . '">';
                        $html    .= '<input type="radio" id="' . esc_attr( $option . '_' . $key ) . '" name="' . esc_attr( $name ) . '" data-attribute_name="attribute_' . esc_attr( sanitize_title( $attribute ) ) . '" value="' . esc_attr( $option ) . '" ' . $checked . '/>';
                        $html    .= '<span>' . $option . '</span>';
                        $html    .= '</label>';
                    }
                }

                $html .= '</div>';
            }

            return $html;
        }

        /**
         * Action hook color
         */
        $sw_attribute_taxonomies = wc_get_attribute_taxonomies();
        if ( ! empty( $sw_attribute_taxonomies ) ) {
            foreach ( $sw_attribute_taxonomies as $attr ) {
                if ( preg_match( '/color|colors/', $attr->attribute_name, $match ) ) {
                    add_action( 'pa_' . $attr->attribute_name . '_add_form_fields', 'sw_woocommerce_variation_fields', 200 );
                    add_action( 'pa_' . $attr->attribute_name . '_edit_form_fields', 'sw_woocommerce_edit_variation_fields', 200 );
                    add_action( 'created_term', 'sw_woocommerce_save_variation_fields', 10, 3 );
                    add_action( 'edit_terms', 'sw_woocommerce_save_variation_fields', 10, 3 );
                    add_action( 'admin_enqueue_scripts', 'sw_woocommerce_variation_color_script' );
                }
            }
        }

        /**
         * Create color
         */
        function sw_woocommerce_variation_fields() { ?>

            <div class="form-field custom-picker">
                <label for="sw_variation_color"><?php _e( 'Color', 'sw_woocommerce' ); ?></label>
                <input name="sw_variation_color" id="sw_variation_color" type="text" value="" size="40"
                       class="category-colorpicker"/>
            </div>

            <?php
        }

        /**
         * @param $term
         */
        function sw_woocommerce_edit_variation_fields( $term ) {
            $sw_variation_color = get_term_meta( $term->term_id, 'sw_variation_color', true ); ?>

            <tr class="form-field custom-picker custom-picker-edit">
                <th scope="row" valign="top"><label
                            for="sw_variation_color"><?php _e( 'Color', 'sw_woocommerce' ); ?></label></th>
                <td>
                    <input name="sw_variation_color" id="sw_variation_color" type="text"
                           value="<?php echo esc_attr( $sw_variation_color ) ?>" size="40" class="category-colorpicker"/>
                </td>
            </tr>

            <?php
        }

        /**
         * Save custom field of category form
         *
         * @param $term_id
         * @param string $tt_id
         * @param string $taxonomy
         * @param string $prev_value
         */
        function sw_woocommerce_save_variation_fields( $term_id, $tt_id = '', $taxonomy = '', $prev_value = '' ) {
            if ( isset( $_POST['sw_variation_color'] ) ) {
                $term_value = esc_attr( $_POST['sw_variation_color'] );
                update_term_meta( $term_id, 'sw_variation_color', $term_value, $prev_value );
            }
        }

        function sw_woocommerce_variation_color_script() {
            wp_enqueue_style( 'wp-color-picker' );
            wp_enqueue_script( 'category_color_picker_js', WCURL . '/js/admin/category_color_picker.js', array( 'wp-color-picker' ), false, true );
        }
rta7y2nd

rta7y2nd1#

这里试试这个。还有一个提示,改进你的代码格式,因为有一天你需要再次编辑它。

<?php

    /**
     * Product variation hook
     */
    add_filter( 'woocommerce_dropdown_variation_attribute_options_html', 'sw_woocommerce_custom_variation', 10, 2 );
    function sw_woocommerce_custom_variation( $html, $args ) {
        $options   = $args['options'];
        $product   = $args['product'];
        $attribute = $args['attribute'];
        $name      = $args['name'] ? $args['name'] : 'attribute_' . sanitize_title( $attribute );
        $id        = $args['id'] ? $args['id'] : sanitize_title( $attribute );
        $class     = $args['class'];

        if ( empty( $options ) && ! empty( $product ) && ! empty( $attribute ) ) {
            $attributes = $product->get_variation_attributes();
            $options    = $attributes[ $attribute ];
        }

        $html = '';

        if ( ! empty( $options ) ) {

            $html .= '<div class="sw-custom-variation">';

            if ( $product && taxonomy_exists( $attribute ) ) {

                //Get product stock
                $stock = $product->get_stock_quantity();

                // Get terms if this is a taxonomy - ordered. We need the names too.
                $terms = wc_get_product_terms( $product->get_id(), $attribute, array( 'fields' => 'all' ) );

                foreach ( $terms as $key => $term ) {
                    $color      = get_term_meta( $term->term_id, 'sw_variation_color', true );
                    $active     = checked( sanitize_title( $args['selected'] ), $term->slug, false ) ? ' selected' : '';
                    $attributes = ( preg_match( '/color|colors/', $attribute, $match ) && $color !== '' ) ? 'class="variation-color" style="background: ' . esc_attr( $color ) . '"' : '';

                    if ( in_array( $term->slug, $options, true ) ) {
                        $additional_class = '';
                        if ( ! empty( $stock ) && $stock < 1 ) {
                            $additional_class = 'out-stock';
                        }

                        $html .= '<label class="' . $additional_class . ' radio-label sw-radio-variation sw-radio-variation-' . esc_attr( $key . ' ' . $active ) . '" title="' . esc_attr( $term->slug ) . '" for="' . esc_attr( $term->slug . '_' . $key ) . '">';
                        $html .= '<input type="radio" id="' . esc_attr( $term->slug . '_' . $key ) . '" name="' . esc_attr( $name ) . '" data-attribute_name="attribute_' . esc_attr( sanitize_title( $attribute ) ) . '" value="' . esc_attr( $term->slug ) . '" ' . checked( sanitize_title( $args['selected'] ), $term->slug, false ) . '/>';
                        $html .= '<span ' . $attributes . '>' . $term->name . '</span>';
                        $html .= '</label>';
                    }
                }
            } else {
                foreach ( $options as $key => $option ) {
                    // This handles < 2.4.0 bw compatibility where text attributes were not sanitized.
                    $checked = sanitize_title( $args['selected'] ) === $args['selected'] ? checked( $args['selected'], sanitize_title( $option ), false ) : checked( $args['selected'], $option, false );
                    $active  = ( $checked ) ? 'selected' : '';
                    $html    .= '<label class="radio-label sw-radio-variation sw-radio-variation-' . esc_attr( $key . ' ' . $active ) . '"  title="' . esc_attr( $option ) . '"  for="' . esc_attr( $option . '_' . $key ) . '">';
                    $html    .= '<input type="radio" id="' . esc_attr( $option . '_' . $key ) . '" name="' . esc_attr( $name ) . '" data-attribute_name="attribute_' . esc_attr( sanitize_title( $attribute ) ) . '" value="' . esc_attr( $option ) . '" ' . $checked . '/>';
                    $html    .= '<span>' . $option . '</span>';
                    $html    .= '</label>';
                }
            }

            $html .= '</div>';
        }

        return $html;
    }

    /**
     * Action hook color
     */
    $sw_attribute_taxonomies = wc_get_attribute_taxonomies();
    if ( ! empty( $sw_attribute_taxonomies ) ) {
        foreach ( $sw_attribute_taxonomies as $attr ) {
            if ( preg_match( '/color|colors/', $attr->attribute_name, $match ) ) {
                add_action( 'pa_' . $attr->attribute_name . '_add_form_fields', 'sw_woocommerce_variation_fields', 200 );
                add_action( 'pa_' . $attr->attribute_name . '_edit_form_fields', 'sw_woocommerce_edit_variation_fields', 200 );
                add_action( 'created_term', 'sw_woocommerce_save_variation_fields', 10, 3 );
                add_action( 'edit_terms', 'sw_woocommerce_save_variation_fields', 10, 3 );
                add_action( 'admin_enqueue_scripts', 'sw_woocommerce_variation_color_script' );
            }
        }
    }

    /**
     * Create color
     */
    function sw_woocommerce_variation_fields() { ?>

        <div class="form-field custom-picker">
            <label for="sw_variation_color"><?php _e( 'Color', 'sw_woocommerce' ); ?></label>
            <input name="sw_variation_color" id="sw_variation_color" type="text" value="" size="40"
                   class="category-colorpicker"/>
        </div>

        <?php
    }

    /**
     * @param $term
     */
    function sw_woocommerce_edit_variation_fields( $term ) {
        $sw_variation_color = get_term_meta( $term->term_id, 'sw_variation_color', true ); ?>

        <tr class="form-field custom-picker custom-picker-edit">
            <th scope="row" valign="top"><label
                        for="sw_variation_color"><?php _e( 'Color', 'sw_woocommerce' ); ?></label></th>
            <td>
                <input name="sw_variation_color" id="sw_variation_color" type="text"
                       value="<?php echo esc_attr( $sw_variation_color ) ?>" size="40" class="category-colorpicker"/>
            </td>
        </tr>

        <?php
    }

    /**
     * Save custom field of category form
     *
     * @param $term_id
     * @param string $tt_id
     * @param string $taxonomy
     * @param string $prev_value
     */
    function sw_woocommerce_save_variation_fields( $term_id, $tt_id = '', $taxonomy = '', $prev_value = '' ) {
        if ( isset( $_POST['sw_variation_color'] ) ) {
            $term_value = esc_attr( $_POST['sw_variation_color'] );
            update_term_meta( $term_id, 'sw_variation_color', $term_value, $prev_value );
        }
    }

    function sw_woocommerce_variation_color_script() {
        wp_enqueue_style( 'wp-color-picker' );
        wp_enqueue_script( 'category_color_picker_js', WCURL . '/js/admin/category_color_picker.js', array( 'wp-color-picker' ), false, true );
    }

相关问题