Woo Snippet: Add Whatsapp Button to Product Page

updated: 21.06.2021

The snippet add a Whatsapp checkout button for product page to Woocommerce. The user follows the link, after which the product data is added to the message text.
Example here.

<?php 
add_action( 'woocommerce_after_add_to_cart_button', 'add_custom_button', 0, 0 );
function add_custom_button() { 
	global $product;
	global $WOOCS;
	$wh_product_title = $product->get_title();
	$wh_product_sku = $product->get_sku();
	$wh_product_link = get_permalink();
        $wh_product_status = $product->get_stock_status();
        $wh_send_status = $wh_product_status == 'instock' ? 'В наличии' : 'Нет в наличии';
	$wh_btn_icon = '<svg xmlns="http://www.w3.org/2000/svg" width="22px" height="22px" fill="none" viewBox="0 0 24 24" class=""><path fill="currentColor" d="M8.886 7.17c.183.005.386.015.579.443.128.285.343.81.519 1.238.137.333.249.607.277.663.064.128.104.275.02.448l-.028.058a1.43 1.43 0 01-.23.37 9.386 9.386 0 00-.143.17c-.085.104-.17.206-.242.278-.129.128-.262.266-.114.522.149.256.668 1.098 1.435 1.777a6.634 6.634 0 001.903 1.2c.07.03.127.055.17.076.257.128.41.108.558-.064.149-.173.643-.749.817-1.005.168-.256.34-.216.578-.128.238.089 1.504.71 1.761.837l.143.07c.179.085.3.144.352.23.064.109.064.62-.148 1.222-.218.6-1.267 1.176-1.742 1.22l-.135.016c-.436.052-.988.12-2.956-.655-2.426-.954-4.027-3.32-4.35-3.799a2.768 2.768 0 00-.053-.076l-.006-.008c-.147-.197-1.048-1.402-1.048-2.646 0-1.19.587-1.81.854-2.092l.047-.05a.95.95 0 01.687-.32c.173 0 .347 0 .495.005z"></path><path fill="currentColor" fill-rule="evenodd" d="M2.184 21.331a.4.4 0 00.487.494l4.607-1.204a10 10 0 004.76 1.207h.004c5.486 0 9.958-4.447 9.958-9.912a9.828 9.828 0 00-2.914-7.011A9.917 9.917 0 0012.042 2c-5.486 0-9.958 4.446-9.958 9.911 0 1.739.458 3.447 1.33 4.954l-1.23 4.466zm2.677-4.068a1.5 1.5 0 00-.148-1.15 8.377 8.377 0 01-1.129-4.202c0-4.63 3.793-8.411 8.458-8.411 2.27 0 4.388.877 5.986 2.468a8.328 8.328 0 012.472 5.948c0 4.63-3.793 8.412-8.458 8.412h-.005a8.5 8.5 0 01-4.044-1.026 1.5 1.5 0 00-1.094-.132l-2.762.721.724-2.628z" clip-rule="evenodd"></path></svg>';
	
	## FOR 1 CURRENCY ##
	#$wh_product_price = $product->get_price();
	#$wh_product_currency = $get_woocommerce_currency_symbol();
	
	## For WOOCS ##
	$wh_product_price = $res=$WOOCS->woocs_exchange_value($product->get_price());
	$wh_product_currency = $WOOCS->current_currency;
	
	$whatsapp_text = 'Здравствуйте! Хочу приобрести товар: '. $wh_product_title .' (' . $wh_product_sku . ') , за ' . $wh_product_price . ' ' . $wh_product_currency . '. Статус товара: ' . $wh_send_status . '. Ссылка '. Ссылка на товар: ' . $wh_product_link;
	$whatsapp_number= '375291111111';
        $whatsapp_link = 'https://api.whatsapp.com/send?phone=' . $whatsapp_number . '&text=' . urlencode($whatsapp_text) .'"';
    echo '<a class="btn-whatsapp button alt" href="' . esc_url( $whatsapp_link ) . '">' . __( "Оформить в Whatsapp", "my_theme_slug" )  . $wh_btn_icon . '</a>';
}; 

add_action( 'wp_head', function () { ?>
<style>
	.btn-whatsapp {
		background-color: #025a07 !important;
		display: inline-flex;
		align-items: center;
		justify-content: center;
		margin-top: 0;
		line-height: 1;
		position: relative;
		width: 100%;
		margin-top: 10px;
	}
	.btn-whatsapp > svg {
		margin-left: 12px;
	}
	@media only screen and (min-width: 576px) { .btn-whatsapp { margin-left: 114px; } }
</style>
<?php } );