• I have setup a function to check the $stock_status so I can change the button text to be Sold Out, in basket etc.. Is there any hook I can use to check if the product is in someones cart? That way I can change the button text. As it is, it has the $stock_status to ‘out-of-stock’ and the button displays Sold Out.

    Thanks,
    Dean.

Viewing 8 replies - 1 through 8 (of 8 total)
  • Plugin Author James Golovich

    (@jamesgol)

    There are a number of filters that are called to change the different texts displayed regarding the stock status, but I don’t have anything setup to handle the button text. I’d be happy to add some custom functionality to make that easier if you’ve got some more input on how you would want it setup.

    Otherwise to do it yourself you would want to use the WooCommerce integrations to grab the class instance and you should be able to call the functions to check the status. Not something I would recommend doing since the interface might change in the future.

    Thread Starter eastanglianevents

    (@eastanglianevents)

    The below is what I’m using at the minute, if I can not display soldout and have something else when stock is in a cart.

    Thanks,
    Dean.

    add_filter( ‘woocommerce_product_add_to_cart_text’, ‘customizing_add_to_cart_button_text’, 10, 2 );
    add_filter( ‘woocommerce_product_single_add_to_cart_text’, ‘customizing_add_to_cart_button_text’, 10, 2 );
    function customizing_add_to_cart_button_text( $button_text, $product ) {
    $sold_out = __( “Sold Out”, “woocommerce”);
    $incart = __( “Already in Cart”, “woocommerce”);
    $addcart = __( “Add to Cart”, “woocommerce”);
    $availability = $product->get_availability();
    $stock_status = $availability[‘class’];
    // Only for variable products on single product pages
    if ( $product->is_type(‘variable’) && is_product() )
    {
    ?>
    <script>
    jQuery(document).ready(function($) {
    $(‘select’).blur( function(){
    if( ” != $(‘input.variation_id’).val() && $(‘p.stock’).hasClass(‘out-of-stock’) )
    $(‘button.single_add_to_cart_button’).html(‘<?php echo $sold_out; ?>’);
    else
    $(‘button.single_add_to_cart_button’).html(‘<?php echo $button_text; ?>’);
    $(‘button.single_add_to_cart_button’).html(‘<?php echo $button_text; ?>’);

    console.log($(‘input.variation_id’).val());
    });
    });
    </script>
    <?php
    }
    // For all other cases (not a variable product on single product pages)
    elseif ( ! $product->is_type(‘variable’) && ! is_product() )
    {
    if($stock_status == ‘out-of-stock’){
    $button_text = $sold_out;
    return $button_text;}
    else
    $cartidnum = 0;
    foreach( WC()->cart->get_cart() as $values ) {
    $products_ids_array[] = $values[‘product_id’];
    $_product = $values[‘product_id’];
    if( $product->get_id() == $products_ids_array[$cartidnum] ) {
    $button_text = $incart;
    return $button_text;
    }
    else
    $cartidnum++;
    }
    }
    $button_text = $addcart;
    return $button_text;
    }

    Plugin Author James Golovich

    (@jamesgol)

    A quick glance at your code it looks like that should work fine for changing the button text. I think I’m missing something here, can you explain what isn’t working?

    Thread Starter eastanglianevents

    (@eastanglianevents)

    Sorry, yeah… If someone places the last item in their cart everyone else gets the soldout message even thou they have not been through the checkout yet. If the cart times out it then places back into stock for others to purchase. I would like to be able to put a different message other than sold out while its in the cart and not purchased so people can wait to see if they can purchase it.

    Thanks,
    DEan.

    Plugin Author James Golovich

    (@jamesgol)

    Ok I get what you are saying now. Though without some major changes that won’t automatically update (though sounds like a good overall improvement to WooCommerce).

    You can call WC()->integrations->get_integrations() to get an array of all of the active integrations. The ‘WC_Cart_Stock_Reducer’ element will have the instance of the plugin, with that you can call sessions->quantity_in_carts() or sessions->find_items_in_carts() to see how many are in active carts.

    I suspect others might find something like this useful, so I’ll think about an easy way to automatically have it happen. I’d happily accept a pull request with the feature ??

    Thread Starter eastanglianevents

    (@eastanglianevents)

    Hi,
    I’m a bit new to all this.. I can see in your class php file you use ‘get_virtual_stock_available’.. I could then find the difference between the real stock. How would I be able to get the virtual stock available in the functions.php file?d

    Dean.

    Plugin Author James Golovich

    (@jamesgol)

    Did you try doing what I said before?

    You can call WC()->integrations->get_integrations() to get an array of all of the active integrations. The ‘WC_Cart_Stock_Reducer’ element will have the instance of the plugin, with that you can call sessions->quantity_in_carts() or sessions->find_items_in_carts() to see how many are in active carts.

    You should be able to get the instance of the plugin and call the function that way

    Thread Starter eastanglianevents

    (@eastanglianevents)

    I had a go, but not 100% on php and couldn’t work out how to write it into the functions.php file.

Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘Hooks’ is closed to new replies.