I have a woocommerce store and I have created a function in functions.php to include a button to the simple-product page if you are signed in as an administrator. This function works great and the button shows nicely with whatever i specify as the href working.
add_action( 'woocommerce_before_single_product_summary', 'product_visibility_button', 5 );
function product_visibility_button() {
if ( is_user_logged_in() ) {
global $product;
$id = $product->get_id();
$user = wp_get_current_user();
if ( in_array( 'administrator', (array) $user->roles ) ) {
echo '<div>';
echo '<a href="https://activepieces.myurl.co.uk/api/v1/webhooks/Bf8utHihs67830sOhkq41v?id=" class="button" style="margin:10px">Publish To Facebook</a>';
echo '</div>';
}
}
}
As you can see, the aim of this button will be to call a webhook for my ActivePieces instance and I need to append the product ID of whatever product I am on, to the end of the href url after the '=' so that I can parse the product ID into the flow I have created.
I am relatively new to php and woocommerce, could anyone please assist and break it down for me so I can learn while I am going?
I have tried calling various global and wc_get hooks but cannot seem to get this to work.
Each product in WooCommerce is represented by a WC_Product
object, and you can use its methods to get the product ID.
add_action( 'woocommerce_before_single_product_summary', 'product_visibility_button', 5 ); function product_visibility_button() {
if ( is_user_logged_in() ) {
global $product;
$id = $product->get_id();
$user = wp_get_current_user();
if ( in_array( 'administrator', (array) $user->roles ) ) {
$href = "https://activepieces.myurl.co.uk/api/v1/webhooks/Bf8utHihs67830sOhkq41v?id=" . $id;
echo '<div>';
echo '<a href="' . esc_url( $href ) . '" class="button" style="margin:10px">Publish To Facebook</a>';
echo '</div>';
}
}
}