function add_price_calculate_tour() {
global $post;
$product = wc_get_product( $post->ID );
if( $product->is_type( 'tour_booking' ) ):
$priceAdult = (float) get_post_meta( $product->get_id(), '_regular_price', true );
$priceChild = (float) get_post_meta( $product->get_id(), 't_children_price', true );
$tour = json_encode(["priceAdult" => $priceAdult,"priceChild" => $priceChild]);
wp_add_inline_script( 'twentyseventeen-global', 'var tour = '.$tour.'', 'before' );
endif;
}
I would like to simplify or improve this code, what it does basically is to create an array with a value, and then create a json and print it in the footer.
Any suggestions?
Related
In the Woocommerce Shipment Tracking plugin they use:
<?php echo esc_html( $tracking_item['tracking_number'] ); ?>
to get the shipping tracking number. How can use something similar directly in a Woocommerce email template?
Here is what I came up with and it works!
<?php
$order_id = $order->get_order_number();
$tracking_items = get_post_meta( $order_id, '_wc_shipment_tracking_items', true );
foreach ( $tracking_items as $tracking_item ){
echo esc_html( $tracking_item['tracking_number'] );
}?>
Updated version for WooCommerce 3.0+:
$order = new WC_Order( $queue_item->order_id );
$tracking_items = $order->get_meta( '_wc_shipment_tracking_items', true );
if ( count( $tracking_items ) > 0 ) {
foreach ( $tracking_items as $tracking_item ) {
$tracker = esc_html( $tracking_item['tracking_number'] );
}
}
I need to get all variation id and update price in loop.
Simple query and loop looks like:
$params = array(
‘posts_per_page’ => -1,
‘post_type’ => ‘product_variation’,
‘post_parent’ => $product->get_id() // tried $post-ID
);
$variations = get_posts( $params );
foreach ( $variations as $variation ) {
$variation_ID = $variation->ID; // tried $post-ID, $product->get_id()
$regular_price=34;
update_post_meta( $variation_ID, ‘_regular_price’, (float)$regular_price );
update_post_meta( $variation_ID, ‘_price’, (float)$regular_price );
}
This doesn't seem to work:
(‘post_parent’ => $product->get_id())
Neither does this:
($variation_ID = $variation->ID;).
First in your code ‘ or ’ should be replaced by '.
Also if used $post-ID should be replaced by $post->ID
Depending on where and how you are using this code, you should try to include global $post; first to be able to use the WP_Post object $post.
Then you could try to use this customized version of your code instead:
global $post;
$regular_price = 13;
// Only for product post type
if( $post->post_type == 'product' )
$product = wc_get_product( $post->ID ); // An instance of the WC_Product object
// Only for variable products
if( $product->is_type('variable') ){
foreach( $product->get_available_variations() as $variation_values ){
$variation_id = $variation_values['variation_id']; // variation id
// Updating active price and regular price
update_post_meta( $variation_id, '_regular_price', $regular_price );
update_post_meta( $variation_id, '_price', $regular_price );
wc_delete_product_transients( $variation_id ); // Clear/refresh the variation cache
}
// Clear/refresh the variable product cache
wc_delete_product_transients( $post->ID );
}
This code is tested on WooCommerce version 3+ and works
I like to get the value from the Aelia Currency Switcher plugin which is the '_order_total_base_currency' and make a simple USD conversion then display it on my custom field metabox. How do I fetch that value so I can use it for calculation, and then display?
Here is my code:
// Adding the metabox (on the right side)
add_action( 'add_meta_boxes', 'cdmb_add_meta_box');
function cdmb_add_meta_box() {
add_meta_box(
'woocommerce-order-my-custom',
__('USD Currency display'),
'cdmb_display_meta_box',
'shop_order',
'side',
'core'
);
}
// The metabox content
function cdmb_display_meta_box() {
// Get
$total_usd = (get_post_meta( $post->ID, '_order_total_base_currency', true )) / 0.75;
$total_usd .= get_post_meta( $post->ID, '_order_total_base_currency', true );
echo '<p>' . $total_usd . '</p>';
}
// Save/Update the meta data
add_action( 'save_post', 'cdmb_save_meta_box_data' );
function cdmb_save_meta_box_data( $post_id ) {
// Only for shop order
if ( 'shop_order' != $_POST[ 'post_type' ] )
return $post_id;
// Checking that is not an autosave
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE )
return $post_id;
## SETTING AND UPDATING DATA ##
update_post_meta( $post_id, 'total-usd', sanitize_text_field( $_POST[ 'total-usd' ] ) );
}
?>
After spending time with it, I found the answer. I am now able to fetch the value for '_order_total_base_currency' from the Aelia Currency Switcher plugin.
It needs the global $post; before the variable $total_usd.
The code should be this:
function cdmb_display_meta_box() {
// Get
global $post;
$total_usd = get_post_meta( $post->ID, '_order_total_base_currency', true );
echo '<p>' . $total_usd . '</p>';
I am displaying some custom fields on the woocommerce single product page with this
add_action( 'woocommerce_single_product_summary','add_custom_field', 20 );
function add_custom_field() {
global $post;
echo get_post_meta( $post->ID, 'Brand', true );
echo get_post_meta( $post->ID, 'Content', true );
return true;
}
This dispays only the values of the custom fields, but I would like the names before so it would look like this:
Brand: ...
Content: ...
The custom fields don't aplly to every product though, so for the products where the custom fields are not set, nothing should be displayed.
Use this:
add_action( 'woocommerce_single_product_summary', 'add_custom_field', 20 );
function add_custom_field() {
global $post;
$brand = get_post_meta( $post->ID, 'Brand', true );
$content = get_post_meta( $post->ID, 'Content', true );
if (!empty($brand)) {
echo 'Brand: '. $brand;
}
if (!empty($content)) {
echo 'Content: '. $content;
}
}
Try below code
add_action( 'woocommerce_single_product_summary', 'add_custom_field', 20 );
function add_custom_field() {
global $post;
$brand = get_post_meta( $post->ID, 'Brand', true );
$content = get_post_meta( $post->ID, 'Content', true );
if (!empty($brand)) {
echo 'Brand: '. $brand .'<br>';
}
if (!empty($content)) {
echo 'Content: '. $content .'<br>';
}
}
I am trying to update the price from a custom page template. In my custom template I am writing these :
$args = array( 'post_type' => 'product');
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
$post_id = get_the_ID();
switch ($post_id) {
case '70':
echo the_title().'<br>';
$get_the_product_price = get_post_meta( get_the_ID(), '_regular_price', true);
$updated_product_price = $get_the_product_price+30;
update_my_price($post_id , $updated_product_price);
default:
break;
}
endwhile;
In functions.php I wrote this :
function update_my_price( $post_id , $updated_product_price) {
update_post_meta( $post_id, '_regular_price', $updated_product_price );
}
add_action( 'woocommerce_process_product_meta', 'update_my_price' );
The problem is Price is not being updated.
Any thought ?
Thanks in Advance.