0

I’m trying to create a bit of shortcode that would allow me to display what my product price would be after multiplying it by .2 but I haven’t much understanding of php

The Shortcode script seems to basically be

function wpc_elementor_shortcode( $atts ) {
}
add_shortcode( 'my_elementor_php_output', 'wpc_elementor_shortcode');

Where the actual content I want to add would be inbetween the brackets

The content seems to be in the wheel house of this code

$product_id = 99;
$product = wc_get_product( $product_id );
echo $product->get_price_html();

Which I’m told would simply display the price of the product. What I would like to know is how I can take whatever number “get_price_htnml(); produces and multiply it by .2 and have that result displayed by my shortcode script.

It’s not proper code, but I would think something to the effect of

$sum = get_price_html() * .2
echo $sum

Is what I would do if php was a lot simpler.

I also am uncertain about having the product id set to 99, the post I got the code from said that I needed to create a product object just to use the get_price_html code, but the shortcode would need to be responsive to the single product it was added to, it can’t keep calling to product id 99’s price one very page it’s on

Sorry that this is a mess, I mostly code css but need to add this one bit of php to make my life a whole lot easier

1 Answers1

0

You have to use 2 functions 1st for updating product price and 2nd create product shortcode.

1) Update Product price like this:

   /**
    * @description    Alter Product Pricing For WooCommerce Product
    * @compatible    WooCommerce 4.1
    */

    add_filter( 'woocommerce_get_price_html', 'woo_alter_price_display', 9999, 2 );

   function woo_alter_price_display( $price_html, $product ) {
    
     // ONLY ON FRONTEND
     if ( is_admin() ) return $price_html;

     // ONLY IF PRICE NOT NULL
     if ( '' === $product->get_price() ) return $price_html;

     // IF CUSTOMER LOGGED IN
     // if ( wc_current_user_has_role( 'customer' ) ) {
         $price = $product->get_price();
         $updated_price = $price * .02;
         $orig_price = wc_get_price_to_display( $product );
         $price_html = wc_price($updated_price);
     // }
    
      return $price_html;

   }

Reference URL: https://stackoverflow.com/a/69521331/16560548

2. Create Short Product Shortcode.

    function woo_product_details($atts = array()){
    if(isset($atts['product_id']) && !empty($atts['product_id'])){
        $html = '';
        $product_id                = $atts['product_id'];
        $product                   = wc_get_product($product_id);

        $product_name              = $product->get_name();
        $product_slug              = $product->get_slug();
        //$product_sku               = $product->get_sku();
        //$product_description       = $product->get_description();
        $product_short_description = $product->get_short_description(); 

        $price = $product->get_price();
        $extra_price = .2;
        $updated_price = $price * $extra_price;
        $price_html = wc_price($updated_price);
        //$product_price             = $product->get_price();
        $product_formated_price    = $product->get_price_html();
        //$product_regular_price     = $product->get_regular_price();
        //$product_sale_price        = $product->get_sale_price();  
        $product_image= wp_get_attachment_image_src( get_post_thumbnail_id($product_id), 'single-post-thumbnail' );
        
        $html .= '<div class="card">
            <img src="'.$product_image[0].'" alt="'.$product_name.'" data-id="'.$product_id.'" style="width:100%">
            <h1>'.$product_name.'</h1>
            <p class="price">'.$product_formated_price.'</p>
            <p>'.$product_short_description.'</p>
            <p><a href="'.site_url().'?add-to-cart='.$product_id.'&quantity=1">Add to Cart</a></p>
        </div>';
        return $html;
    }
}
add_shortcode('product_detail', 'woo_product_details');

How to User shortcode

a) You can call directly on your PHP page like this:

<?php echo do_shortcode('[product_detail product_id = '1002']'); ?>

b) You can call on your post and page from admin panel like this:

[product_detail product_id = '1002']

Where 1002 is product id

Refrence URL: https://stackoverflow.com/a/69008400/16560548

Rajeev Singh
  • 1,724
  • 1
  • 6
  • 23