Общие сниппеты для вариативного товара

PHOTO EMBED

Wed Nov 16 2022 08:42:38 GMT+0000 (Coordinated Universal Time)

Saved by @mastaklance

/**
 * Remove Sale Badge
 */
add_filter('woocommerce_sale_flash', 'ml_hide_sale_flash');
function ml_hide_sale_flash()
{
return false;
}


/**
 * Change sale text to percentage. Always Display the selected variation price for variable products (already working)
 */
add_filter( 'woocommerce_show_variation_price', 'filter_show_variation_price', 10, 3 );
function filter_show_variation_price( $condition, $product, $variation ){
    if( $variation->get_price() === "" ) return false;
    else return true;
}
// Remove the displayed price from variable products in single product pages only
add_action( 'woocommerce_single_product_summary', 'remove_the_displayed_price_from_variable_products', 9 );
function remove_the_displayed_price_from_variable_products() {
    global $product;

    // Just for variable products
    if( ! $product->is_type('variable') ) return;

    // Remove the displayed price from variable products
    remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_price', 10 );
}
// Display the selected variation discounted price with the discounted percentage for variable products
add_filter( 'woocommerce_format_sale_price', 'woocommerce_custom_sales_price', 10, 3 );
function woocommerce_custom_sales_price( $price, $regular_price, $sale_price ) {
    global $product;

    // Just for variable products on single product pages
    if( $product->is_type('variable') && is_product() ) {
    
        // Getting the clean numeric prices (without html and currency)
        $regular_price = floatval( strip_tags($regular_price) );
        $sale_price = floatval( strip_tags($sale_price) );
    
        // Percentage calculation and text
        $percentage = round( ( $regular_price - $sale_price ) / $regular_price * 100 ).'%';
        $percentage_txt = __(' save up to ', 'woocommerce' ).$percentage;
		$percentage_txt = __('<p class="ml-sale"> save ', 'woocommerce' ).$percentage; '</p>';
    
        return '<del>' . wc_price( $regular_price ) . '</del> <ins>' . wc_price( $sale_price ) . $percentage_txt . '</ins>';
    }
    return $price;
}


/**
 * display variations sale badge only when a variation on sale is selected
 */
add_action( 'woocommerce_before_single_variation', 'action_wc_before_single_variation' );
function action_wc_before_single_variation() {
    ?>
    <script type="text/javascript">
    (function($){
        // Check for the show_variation event that triggers when a variation is selected.
        $('form.variations_form').on('show_variation', function(event, data){
            //If variation is on sale, display the sale badge
            if(data.display_price < data.display_regular_price){
                $(".product-type-variable .onsale").css("display", "block");
            }
            //Otherwise hide the sale badge
            else {
                $(".product-type-variable .onsale").css("display", "none");
            }
        });
    })(jQuery);
    </script>
    <?php
}


/**
 * Add some attribute values to WooCommerce variable product title from chosen variation
 */
 // Defining product Attributes term names to be displayed on variable product title
add_filter( 'woocommerce_available_variation', 'filter_available_variation_attributes', 10, 3 );
function filter_available_variation_attributes( $data, $product, $variation ){
    // Here define the product attribute(s) slug(s) which values will be added to the product title
    // Or replace the array with 'all' string to display all attribute values
    $attribute_names = array('Custom', 'Color');

    foreach( $data['attributes'] as $attribute => $value ) {
        $attribute      = str_replace('attribute_', '', $attribute);
        $attribute_name = wc_attribute_label($attribute, $variation);

        if ( ( is_array($attribute_names) && in_array($attribute_name, $attribute_names) ) || $attribute_names === 'all' ) {
            $value = taxonomy_exists($attribute) ? get_term_by( 'slug', $value, $attribute )->name : $value;

            $data['for_title'][$attribute_name] = $value;
        }
    }
    return $data;
}

// Display to variable product title, defined product Attributes term names
add_action( 'woocommerce_after_variations_form', 'add_variation_attribute_on_product_title' );
function add_variation_attribute_on_product_title(){
    // Here define the separator string
    $separator = ' - ';
    ?>
    <script type="text/javascript">
    (function($){
        var name = '<?php global $product; echo $product->get_name(); ?>';

        $('form.cart').on('show_variation', function(event, data) {
            var text = '';

            $.each( data.for_title, function( key, value ) {
                text += '<?php echo $separator; ?>' + value;
            });

            $('.product_title').text( name + text );

        }).on('hide_variation', function(event, data) {
            $('.product_title').text( name );
        });
    })(jQuery);
    </script>
    <?php
}

/**
 * Replace the Variable Price range by the chosen variation price
 */
add_action('woocommerce_before_add_to_cart_form', 'selected_variation_price_replace_variable_price_range');
function selected_variation_price_replace_variable_price_range(){
    global $product;

    if( $product->is_type('variable') ):
    ?><style> .woocommerce-variation-price {display:none;} </style>
    <script>
    jQuery(function($) {
        var p = 'p.price'
            q = $(p).html();

        $('form.cart').on('show_variation', function( event, data ) {
            if ( data.price_html ) {
                $(p).html(data.price_html);
            }
        }).on('hide_variation', function( event ) {
            $(p).html(q);
        });
    });
    </script>
    <?php
    endif;
}


/**
 * replace variable product short description by the selected variation description if it's not empty
 */
add_action( 'woocommerce_before_variations_form', 'variable_product_jquery_script' );
function variable_product_jquery_script() {
    ?>
    <style>.woocommerce-variation-description {display:none !important}</style>
    <script>
    (function($) {
        var selector  = '.woocommerce-product-details__short-description',
            form      = $('form.cart'),
            shortDesc = $(selector).html();

        form.on('show_variation', function(event, data){
            var varDesc = data.variation_description;       
            $(selector).html( varDesc ? varDesc : shortDesc );
        });

        form.on('hide_variation', function(){
            $(selector).html(shortDesc);
        });
    })(jQuery);
    </script>
    <?php
}
content_copyCOPY