• Resolved eypclk

    (@eypclk)


    Hello, I want to use the URL in the format “/?add-to-cart=4735&attribute_pa_color=black”, but I’m getting an error message saying ‘Please choose product options by visiting.’ Everything seems to be correct, but it doesn’t work when I enter the URL with ‘pa_color’. I’ve written a JavaScript code, and I need to use the URL in this way. I’ve seen other websites using it. Can you please assist me?

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

Viewing 5 replies - 1 through 5 (of 5 total)
  • The best guide I’ve seen:
    https://www.businessbloomer.com/woocommerce-custom-add-cart-urls-ultimate-guide/

    It looks like you need the format:
    ?add-to-cart=88&quantity=1
    where 88 is the variation ID, not the product ID.

    Thread Starter eypclk

    (@eypclk)

    Yes, it is making additions in that way, but I need to make additions in the format I specified. It works on some websites, but the format I mentioned doesn’t work on my website

    Thread Starter eypclk

    (@eypclk)

    function add_product_to_cart_by_slug() {
        if (isset($_GET['add-to-cart'])) {
            $product_slug = sanitize_text_field($_GET['add-to-cart']);
    
            // Varyasyonlar? yakalamak i?in URL'de "?attribute_pa_" ile ba?layan parametreleri kontrol ediyoruz
            $variation_attributes = array();
            foreach ($_GET as $param => $value) {
                if (strpos($param, 'attribute_pa_') === 0) {
                    $attribute = str_replace('attribute_pa_', '', $param);
                    $variation_attributes[$attribute] = sanitize_text_field($value);
                }
            }
    
            $product_args = array(
                'name'             => $product_slug,
                'post_type'        => 'product',
                'posts_per_page'   => 1,
                'post_status'      => 'publish'
            );
    
            $product_query = new WP_Query($product_args);
    
            if ($product_query->have_posts()) {
                while ($product_query->have_posts()) {
                    $product_query->the_post();
                    $product = wc_get_product(get_the_ID());
                    $product_id = $product->get_id();
    
                    if ($product->is_type('variable')) {
                        $product_variation_id = find_matching_product_variation($product_id, $variation_attributes);
    
                        if ($product_variation_id) {
                            WC()->cart->add_to_cart($product_id, 1, $product_variation_id, $variation_attributes);
                            wp_redirect(wc_get_cart_url());
                            exit;
                        } else {
                            // Varyasyon bulunamad?
                            wp_redirect(home_url()); // ?ste?e ba?l? olarak y?nlendirme yapabilirsiniz
                            exit;
                        }
                    } else {
                        WC()->cart->add_to_cart($product_id);
                        wp_redirect(wc_get_cart_url());
                        exit;
                    }
                }
            }
    
            wp_reset_postdata();
        }
    }
    add_action('template_redirect', 'add_product_to_cart_by_slug');
    
    function find_matching_product_variation($product_id, $variation_attributes) {
        $product = wc_get_product($product_id);
        $variations = $product->get_available_variations();
    
        foreach ($variations as $variation) {
            $variation_data = $variation['attributes'];
    
            if (array_diff($variation_attributes, $variation_data) === array_diff($variation_data, $variation_attributes)) {
                return $variation['variation_id'];
            }
        }
    
        return false;
    }

    I solved the problem by writing a function, guys. If anyone wants to use it, they can add the above code to the functions.php file. We can add items to the cart using the following format: “/?add-to-cart=product-slug&attribute_pa_variation=attribute-slug

    Hey there @eypclk ,

    Thank you so much for adding your input! Some of our customers might indeed find this guide helpful!

    Also, thank you, @lorro, for chiming-in.

    We appreciate you all being an active part of the community ??

    Have a wonderful day!

    filipeffenberg

    (@filipeffenberg)

    @eypclk i add to end on function your code but my link doesn’t work but idk why

    my variables are: wersja, strony, ilosc-firm-klientow

    domain.local/cart/?add-to-cart=6588&attribute_wersja=Asystent+ksi%C4%99gowego&attribute_strony=1000&attribute_ilosc-firm-klientow=do+100
    domain.local/cart/?add-to-cart=6588&attribute_pa_wersja=Asystent+ksi%C4%99gowego&attribute_pa_strony=1000&attribute_pa_ilosc-firm-klientow=do+100


    when i go to product page with attribute it fill up successfully
    domain.local/product/?attribute_wersja=Asystent+ksi%C4%99gowego&attribute_strony=1000&attribute_ilosc-firm-klientow=do+100

    my variant id is 6589 i have with this attributes
    it work using variant id but i can’t use it in my dynamic url
    domain.local/cart/?add-to-card=6589
    i have a custom form with dynamic creation url

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Add to cart attribute url’ is closed to new replies.