$product = wc_get_product( $post_id ); // now you can call WC_Product class functions, e.g.: if ( $product->is_type( 'variable' ) ) { // do something... }
Archive by Author: Tom
Add quantity field to product category pages in WooCommerce
// remove archives Add to Cart button remove_action( 'woocommerce_after_shop_loop_item', 'woocommerce_template_loop_add_to_cart', 10 ); // replace it with the Add to Cart button from single product page add_action( 'woocommerce_after_shop_loop_item', 'woocommerce_template_single_add_to_cart', 30);
Calculate elapsed time in PHP
// get the time in microseconds before your search $start_time = microtime(true); /** * your code here * */ // get the time in microseconds when you search is done $end_time = microtime(true); // finally subtract and print the time elapsed echo $start_time - $end_time;
Hide currency symbol in WooCommerce
add_filter( 'woocommerce_currency_symbol', 'hide_currency_symbol', PHP_INT_MAX, 2 ); function hide_currency_symbol( $currency_symbol, $currency ) { return ''; }
Change product price in WooCommerce programmatically
function change_price_by_type( $product_id, $multiply_price_by, $price_type ) { $the_price = get_post_meta( $product_id, '_' . $price_type, true ); $the_price *= $multiply_price_by; update_post_meta( $product_id, '_' . $price_type, $the_price ); } function change_price_all_types( $product_id, $multiply_price_by ) { change_price_by_type( $product_id, $multiply_price_by, 'price' ); change_price_by_type( $product_id, $multiply_price_by, 'sale_price' ); change_price_by_type( $product_id, $multiply_price_by, 'regular_price' ); } /* * `change_product_price` is main function you should call to change product's price */ function change_product_price( $product_id, $multiply_price_by ) { change_price_all_types( $product_id, $multiply_price_by ); $product = wc_get_product( $product_id ); // Handling variable products if ( $product->is_type( 'variable' ) ) { $variations = $product->get_available_variations(); foreach ( $variations as $variation ) { change_price_all_types( $variation['variation_id'], $multiply_price_by ); } } }