• Resolved panda

    (@alejorostata)


    Is add to cart function in product archive, single product page and cart is different?

    I’m about to leave my keyboard now with a smile on my face because finally, I’ve finished what I wanted to do on my website. But no, something’s not right.

    I want to limit my cart into 10 products only and show a popup dialog created in Elementor with a message that they cannot add more than 10 products.

    So I add an action ofwoocommerce_add_to_cartwith the following codes in my functions.php.

    
    function woocommerce_ajax_add_to_cart(){
        ob_start();
        if ( ! isset( $_POST['product_id'] ) ) {
            return;
        }
        $found = false;
            
        if ( sizeof( WC()->cart->get_cart() ) > 0 ) { //check if product already in cart
            foreach ( WC()->cart->get_cart() as $cart_item_key => $values ) {
                $_product = $values['data'];
                if ( $_product->id == $product_id )
                    $found = true;
            }
            if ( ! $found ){ // if product not found, add it
                $cart_items_count = WC()->cart->get_cart_contents_count();
                $total_count = $cart_items_count + $quantity;
                if ( $cart_items_count > 10 || $total_count > 10 ) {
                    $data = array( 'error' => true, 'is_full_cart' => true, 'datas' => $cart_items_count );
                    wp_send_json( $data );
                }else{
                    WC()->cart->add_to_cart( $product_id, $quantity, $variation_id, $variation );
                }
            }
        } else {
            WC()->cart->add_to_cart(  $product_id, $quantity, $variation_id, $variation ); // if no products in cart, add it
        }
    }
    add_action('woocommerce_add_to_cart', 'woocommerce_ajax_add_to_cart');
    
    //Create a new copy of add-to-cart.js and add the elementor popup
    function child_enqueue_parent_style() {
        wp_dequeue_script('woocommerce-ajax-add-to-cart');
        wp_enqueue_script('woocommerce-ajax-add-to-cart', get_stylesheet_directory_uri().'/js/add-to-cart.js', array('jquery'));
    
    }
    add_action( 'wp_enqueue_scripts', 'child_enqueue_parent_style' );

    then on add-to-cart.js

    ....
    e.data.addToCartHandler.addRequest({
        type: 'POST',
        url: wc_add_to_cart_params.wc_ajax_url.toString().replace( '%%endpoint%%', 'add_to_cart' ),
        data: data,
        success: function( response ) {
            if ( ! response ) {
                return;
            }
    
           //show elementor popup full cart
           if ( response.error && response.is_full_cart ) {
                elementorProFrontend.modules.popup.showPopup( { id: 2409 } );
                return;
            }
                        .....
        },
        dataType: 'json'
    });

    Well, this things works only on product archive not on single product page nor when you are in the cart page trying to update the quantity of the products.

    Does it means, those three has a different functions?

Viewing 2 replies - 1 through 2 (of 2 total)
Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Add to cart function in product archive, single product, cart page’ is closed to new replies.