Below, you’ll find a detailed guide on how to add the Product Addons & Fields for WooCommerce Shortcode to your WordPress website, including its parameters, examples, and PHP function code. Additionally, we’ll assist you with common issues that might cause the Product Addons & Fields for WooCommerce Plugin shortcode not to show or not to work correctly.
Before starting, here is an overview of the Product Addons & Fields for WooCommerce Plugin and the shortcodes it provides:
"Product Addons & Fields for WooCommerce is a dynamic plugin that enhances your WooCommerce store by allowing you to add custom product addons and fields, thereby boosting user experience."
- [ppom]
Product Addons & Fields for WooCommerce [ppom] Shortcode
The WooCommerce Product Add-On plugin shortcode ‘ppom’ dynamically generates a customized product form. It uses a global post variable to fetch the product id and render it on the page. It checks the validity of the product id, loads frontend scripts, and displays a form for adding the product to the cart. It also includes a hidden field for the product id and page id. Shortcode: [ppom]
Shortcode: [ppom]
Parameters
Here is a list of all possible ppom shortcode parameters and attributes:
product_id
– identifier of the specific WooCommerce product
Examples and Usage
Basic example – Displaying a specific product by using the product_id attribute.
[ppom product_id=25 /]
Advanced examples
Displaying a specific product by using the product_id attribute when the product id is a variable within your PHP code.
<?php
$product_id = 25; // This could be any variable within your code
echo do_shortcode("[ppom product_id='$product_id' /]");
?>
Using the shortcode within a template file to display a specific product by its ID.
<?php echo do_shortcode("[ppom product_id='25' /]"); ?>
Please note that in these examples, we are using the product_id attribute to specify the product we want to display. Make sure to replace the number 25 with the actual ID of the product you want to display.
PHP Function Code
In case you have difficulties debugging what causing issues with [ppom]
shortcode, check below the related PHP functions code.
Shortcode line:
add_shortcode( 'ppom', 'ppom_hooks_render_shortcode' );
Shortcode PHP function:
function ppom_hooks_render_shortcode( $attr ) {
$params = shortcode_atts(
array(
'product_id' => '',
),
$attr
);
// return '';
global $post;
$page_id = ! is_null( $post ) ? $post->ID : '';
ob_start();
if ( $params['product_id'] ) {
$product = wc_get_product( $params['product_id'] );
if ( ! $product ) {
echo __( 'Product ID is not valid', 'woocommerce-product-addon' );
return ob_get_clean();
}
PPOM_FRONTEND_SCRIPTS::load_scripts_by_product_id( $params['product_id'], '', 'shortcode' );
?>
<form class="cart"
action="<?php echo esc_url( apply_filters( 'woocommerce_add_to_cart_form_action', $product->get_permalink() ) ); ?>"
method="post" enctype='multipart/form-data'>
<?php
if ( ppom_is_legacy_mode() ) {
ppom_woocommerce_show_fields_on_product( $params['product_id'] );
} else {
ppom_woocommerce_template_base_inputs_rendering( $params['product_id'] );
}
if ( apply_filters( 'ppom_shortcode_show_quantity', true, $product ) ) {
woocommerce_quantity_input(
array(
'min_value' => apply_filters( 'woocommerce_quantity_input_min', $product->get_min_purchase_quantity(), $product ),
'max_value' => apply_filters( 'woocommerce_quantity_input_max', $product->get_max_purchase_quantity(), $product ),
'input_value' => isset( $_POST['quantity'] ) ? wc_stock_amount( wp_unslash( $_POST['quantity'] ) ) : $product->get_min_purchase_quantity(),
// WPCS: CSRF ok, input var ok.
),
$product
);
}
?>
<input type="hidden" name="ppom[ppom_shorcode_page_id]" value="<?php echo esc_attr( $page_id ); ?>">
<input type="hidden" name="ppom[ppom_shorcode_product_id]"
value="<?php echo esc_attr( $params['product_id'] ); ?>">
<button type="submit" name="add-to-cart" value="<?php echo esc_attr( $product->get_id() ); ?>"
class="single_add_to_cart_button button alt"><?php echo esc_html( $product->single_add_to_cart_text() ); ?></button>
</form>
<?php
return ob_get_clean();
}
}
Code file location:
woocommerce-product-addon/woocommerce-product-addon/classes/plugin.class.php
Conclusion
Now that you’ve learned how to embed the Product Addons & Fields for WooCommerce Plugin shortcode, understood the parameters, and seen code examples, it’s easy to use and debug any issue that might cause it to ‘not work’. If you still have difficulties with it, don’t hesitate to leave a comment below.
Leave a Reply