I need to a php file that can show a product's price by id. I have tried doing this with this code in a "price.php" file with this content:
I'm sure that something that links this with wp is missing, but what?
<?php
$price = get_post_meta( '225', '_regular_price', true);
echo $price;
?>
If you have product's ID you can use that to create product object:
$product = new WC_Product($product_id);
After Creating Object you can run any of WooCommerce's product methods.
$product->get_regular_price();
$product->get_sale_price();
$product->get_price();
Related
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
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>Add to Cart</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
I created a custom product type for WooCommerce. With this product type it is possible to connect an other product that is exist by ID.
WooCommerce reduce the stock quantity automatically if an order is placed and the payment is successful.
For example I added a product with ID 4082 to the cart with a quantity from 3.
After place this order WooCommerce updated the stock from product 4082 with -3.
Ok back to my custom product type. As I said it is possible to connect another product by ID.
For example I connect product 4082 with product ID 10988.
If a customer add product 4082 to the cart and placed the order I want reduce the stock quantity from product ID 10988 and not from 4082.
<?php
add_action('woocommerce_checkout_order_processed', 'stocktest');
function stocktest($order_id){
$order = wc_get_order( $order_id );
$order_item = $order->get_items();
foreach( $order_item as $product ) {
//for the topic I programmed the IDs hardcoded
if($product->ID == '4082'){
wc_update_product_stock( 10998, $product['qty'], 'decrease', '' );
}
}
}
?>
I tried the code above and the stock from ID 10998 is correctly decreased but also the stock from ID 4082 is decreased.
Do I use the wrong hook? And how can I make the function correctly?
Hope somebody can help me with this.
Thanks a lot
Does wc_update_product_stock( 4082, $product['qty'], 'increase', '' ); not work?
That way, the stock should be adjusted for the 4082 product.
Also, there might be a potential issue with your snippet. The $product variable refers to the product object of 4082 product, not the 10998 product.
Maybe try something like this?
<?php
add_action('woocommerce_checkout_order_processed', 'stocktest');
function stocktest($order_id){
$order = wc_get_order( $order_id );
$order_item = $order->get_items();
foreach( $order_item as $product ) {
//for the topic I programmed the IDs hardcoded
if($product->ID == '4082'){
$connected_qty = get_post_meta( 10988, '_stock', true );
wc_update_product_stock( 10998, $connected_qty, 'decrease', '' );
wc_update_product_stock( 4082, $product['qty'], 'increase', '' );
}
}
}
?>
Instead of using the custom field query, you could use the WC_Product class:
$connected_product = wc_get_product(10998);
$connected_qty = $connected_product->get_stock_quantity();
I did some research on your question. WooCommerce called a lot of functions to update the stock af order / payment. If an order is placed the items become an status reduce_stock yes or no.
If is yes go further and update the stock of this item. After this WooCommerce created the e-mails and ordernotes.
All this functions worked together please be careful with changing the code in the file wc-stock-functions.php
If you want change and try something do this for example on a local testserver.
This are some functions in the file
<?php
wc_maybe_reduce_stock_levels();
wc_maybe_increase_stock_levels();
wc_reduce_stock_levels();
wc_trigger_stock_change_notifications();
?>
I am trying to show the price of a product in a post by code using HTML.
I am trying to utilize $product->get_price(). however, I need to be able to call the product by identifying it, using its code or something like it.
So Basically, all I want to do is to show the price of a particular product in the post by using a product ID as reference for example.
Any idea?
If you have the product's ID you can use that to create a product object:
$_product = wc_get_product( $product_id );
Then from the object you can run any of WooCommerce's product methods.
$_product->get_regular_price();
$_product->get_sale_price();
$_product->get_price();
Hope this one helps.
you use wc_get_product function :
$_product = wc_get_product( $product_id );
$_product->get_regular_price();
$_product->get_sale_price();
$_product->get_price();
I'm working with a custom product where customers can type their custom text that will be added to cart.
Depending on the size of the text, a different price will be set, the logic is done and I can see the new price in the product object.
It seems that I can change the product object with the new price but I get status 500 back from admin-ajax.php when I do $woocommerce->setup_product_data($product_id).
I have found several topics but none of them seems to work in my case.
I'm not able to update the cart with the new price.
Here is my ajax function in functions.php:
// Adjust new price
function applyNewPrice() {
global $woocommerce;
// From JS
$product_id = (int) $_POST['id'];
// From JS
$price = (float) $_POST['price'];
$product_data = get_post($product_id);
// Code returning status 500 here...
$product = $woocommerce->setup_product_data($product_data);
$product->set_price($price);
update_post_meta($product_id,'_price',$price);
update_post_meta($product_id,'_regular_price',$price);
$woocommerce->clear_product_transients( $product_id );
}
add_action('wp_ajax_applyNewPrice', 'applyNewPrice');
add_action('wp_ajax_nopriv_applyNewPrice', 'applyNewPrice');
The function you are trying to call does not exist. This is the function you need.
$product = wc_setup_product_data( $product_id );
No sure where you are coming up with those functions. I could not find this function either?
$woocommerce->clear_product_transients( $product_id );
Trying to get Product Data on custom Template By product ID, right now i have this code to get Product Title.
$productId = 164;
echo $p_title = get_the_title( $productId );
looking for Short Description, Price, Product Image, Product Url, Product Brand. Or might be loop will be better but that loop should work with product static ID.
Thanks in advance.
You would probably be better served by creating a new product object.
$productId = 164;
$product = wc_get_product( $productId );
echo $product->get_title();
echo $product->get_price_html();
Note, that the short description is merely the post's post_excerpt. If using outside of the loop (where $post is automatically defined) then you would need to get the post directly.
$post = get_post( $productId );
echo apply_filters( 'woocommerce_short_description', $post->post_excerpt );
or alternatively, if you've already defined the product object you could do
echo apply_filters( 'woocommerce_short_description', $product->post->post_excerpt );
since the WooCommerce product class will automatically define the $product->post property with get_post().
apply_filters() means that functions can be attached at this point to modify the content of $product->post->post_content. At a minimum, I know that wpautop() is attached to the woocommerce_short_description filter to create paragraph breaks.