• Resolved paradisiac

    (@paradisiac)


    Hey!

    I have tried to create a custom plugin so the URL of variable product changes when variation is selected.

    Currently
    Base URL: /product/iphone-11-nieuw/
    URL after variations are selected: /product/iphone-11-nieuw/

    What I’m trying to achieve:
    Base URL: /product/iphone-11-nieuw/
    URL after variation is selected: /product/iphone-11-nieuw-blue-64gb/

    I have tried using the following code (plugin) but it does not work, as the product URL gets appended with /variationURL/.

    Example with code below – not working

    Base URL: /product/iphone-11-nieuw-blue-64gb/
    URL after variation is selected: /product/iphone-11-nieuw-blue-64gb/color-blue-size-64gb -> this solution does not work as product is not found on this URL.

    <?php
    /*
    Plugin Name: WooCommerce Variations URL
    Description: Adds support for variation-specific URL's for WooCommerce product variations
    Author: Author name
    Version: 1.0
    Author URI: #
    */
    
    namespace WCVariationsUrl;
    
    final class Init
    {
        /**
         * Call this method to get singleton
         *
         * @return Init
         */
        public static function Instance()
        {
            static $inst = null;
            if ($inst === null) {
                $inst = new Init();
            }
            return $inst;
        }
    
        /**
         * Private ctor so nobody else can instance it
         *
         */
        private function __construct()
        {
            $this->add_actions();
        }
    
        /**
         * Add actions
         */
        function add_actions() {
            add_filter('woocommerce_dropdown_variation_attribute_options_args',array($this,'variation_dropdown_args'));
            add_action('init', array($this,'add_rewrite_rules'));
            add_action('wp_head', array($this,'add_js_to_head'));
    
        }
    
        function variation_dropdown_args($args) {
            // Get the WooCommerce atts
            $attributes =  wc_get_attribute_taxonomies();
            $atts = [];
            foreach ($attributes as $attribute) {
                $atts[] = $attribute->attribute_name;
            }
    
            // Get the variations part of URL
            $url_string = get_query_var('variation');
    
            if ($url_string) {
                $array = [];
                preg_replace_callback(
                    "/(\w++)(?>-(\w+-?(?(?!" . implode("|", $atts) . ")(?-1))*))/",
                    function($matches) use (&$array) {
                        $array[$matches[1]] = rtrim($matches[2], '-');
                    },
                    $url_string
                );
    
                if (!empty($array)) {
                    $attribute_key = str_replace('pa_','',$args['attribute']);
    
                    if (array_key_exists($attribute_key,$array)) {
                        $args['selected'] = $array[$attribute_key];
                    }
                }
            }
    
            return $args;
    
        }
    
        function add_rewrite_rules() {
            add_rewrite_rule('^product-([^/]*)/([^/]*)/?','index.php?product=$matches[1]&variation=$matches[2]','top');
            add_rewrite_tag('%variation%', '([^&]+)');
        }
    
        function add_js_to_head() {
            if (!function_exists('is_product') || !is_product())
                return;
    
            global $post;
            ?>
    
            <script type="text/javascript">
                var url = '<?php echo get_permalink($post->ID);?>';
                jQuery( document ).ready(function($) {
                    setTimeout(
                        function() {
                            $('table.variations select').on('change',function() {
                                var attributes = [];
                                var allAttributesSet = true;
                                $('table.variations select').each(function() {
                                    var value = $(this).val();
                                    if (value) {
                                        attributes.push({
                                            id: $(this).attr('name'),
                                            value: value
                                        });
                                    } else {
                                        allAttributesSet = false;
                                    }
                                });
    
                                if (allAttributesSet) {
                                    $.each(attributes,function(key, val) {
                                        var attributeSlug = val.id.replace('attribute_pa_','');
                                        url = url + attributeSlug + '-' + val.value
                                        if($(this)[0] !== $(attributes).last()[0]) {
                                            url = url + '-';
                                        }
                                    });
                                    window.location.replace(url);
                                }
                            });
    
                        }, 1000
                    )
                });
            </script>
        <?php }
    }
    
    Init::Instance();

    Thank you so much in advance.

    • This topic was modified 3 years, 2 months ago by paradisiac.
    • This topic was modified 3 years, 2 months ago by paradisiac.
    • This topic was modified 3 years, 2 months ago by paradisiac.

    The page I need help with: [log in to see the link]

Viewing 2 replies - 1 through 2 (of 2 total)
Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Custom URL for variations’ is closed to new replies.