Created
July 23, 2022 15:26
-
-
Save Yorlinq/0cc134ce009faf10df702320fbc6d946 to your computer and use it in GitHub Desktop.
Shortcode for 'Add to cart url' by 'Product id' - WooCommerce
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Shortcode for 'Add to cart url' by 'Product id': [yl_product_add_to_cart id=''] | |
function yl_product_add_to_cart_shortcode($atts) { | |
$atts = shortcode_atts( array( | |
'id' => null, | |
), $atts ); | |
if( intval( $atts['id'] ) > 0 && function_exists( 'wc_get_product' ) ){ | |
$_product = wc_get_product($atts['id']); | |
$output = $_product->add_to_cart_url(); | |
} | |
return $output; | |
} | |
add_shortcode( 'yl_product_add_to_cart', 'yl_product_add_to_cart_shortcode' ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment