Display date range from 2 custom date-picker fields - wordpress

I've created two custom datepicker fields with ACF (beginning_date and completion_date) and need to output/display these as a date range.
For example, if beginning_date Year and completion_date Year are equal, I need to output beginning_date day/month - completion day/month/year.
The display and return format these fields use is: l, F j, Y
I've searched everywhere! And tried to implement this: Create display date range from two dates in ACF in Wordpress
I also attempted to convert the fields to datetime objects but comparing these as dates seems to be the wrong way! I do not know where else I'm failing at...
I'm beginning again as nothing has worked so far, so I have no code to publish here because I ended up with a Frankenstein code snippet which I deleted and then came here for desperate help!

in your functions.php
function date_compare($id){
$start_date = get_field('beginning_date', $id);
$end_date = get_field('completion_date', $id);
$start_year = wp_date("Y", strtotime( $start_date ) );
$end_year = wp_date("Y", strtotime( $start_date ) );
$start_month = wp_date("n", strtotime( $start_date ) );
$end_month = wp_date("n", strtotime( $start_date ) );
$date_output = '';
$start_date_format = "l, F j, Y";
if( $start_date && $end_date ){
if( $start_year == $end_year && $start_month == $end_month ){
$start_date_format = "l j";
}
elseif( $start_year == $end_year){
$start_date_format = "l, F j";
}
}
if( $start_date ){
$date_output .= wp_date($start_date_format, strtotime( $start_date ) );
}
if( $end_date ){
$date_output .= ' - ' . wp_date("l, F j, Y", strtotime( $end_date ) );
}
return $date_output;
}
and in your theme:
echo date_compare($post->ID);

Related

Update Parent Product Quantity When Variation Quantity Changes

I am actually trying to update the stock quantity of parent products whenever the quantity of any of its variation increases or decreases.
What could be the error in following code.
function wc_get_variable_product_stock_quantity( $output = 'raw', $product_id = 0 ){
global $wpdb, $product;
// Get the product ID (can be defined)
$product_id = $product_id > 0 ? $product_id : get_the_id();
// Check and get the instance of the WC_Product Object
$product = is_a( $product, 'WC_Product' ) ? $product : wc_get_product($product_id);
// Only for variable product type
if( $product->is_type('variable') ){
// Get the stock quantity sum of all product variations (children)
$stock_quantity = $wpdb->get_var("
SELECT SUM(pm.meta_value)
FROM {$wpdb->prefix}posts as p
JOIN {$wpdb->prefix}postmeta as pm ON p.ID = pm.post_id
WHERE p.post_type = 'product_variation'
AND p.post_status = 'publish'
AND p.post_parent = '$product_id'
AND pm.meta_key = '_stock'
AND pm.meta_value IS NOT NULL
");
// Preparing formatted output
if ( $stock_quantity > 0 ) {
$html = '<p class="stock in-stock">'. sprintf( __("%s in stock", "woocommerce"), $stock_quantity ).'</p>';
} else {
if ( is_numeric($stock_quantity) )
$html = '<p class="stock out-of-stock">' . __("Out of stock", "woocommerce") . '</p>';
else
$html = '';
}
// Different output options
if( $output == 'echo_html' )
echo $html;
elseif( $output == 'return_html' )
return $html;
else
return $stock_quantity;
}
}

Update Product Stock When Variation Stock Changes in WooCommerce

I am trying the update total stock of the product when stock for any variation changes. I want to keep the total stock of all the variations updated.
Here is the code I am using but it's not working. What could be the reason.
add_action( 'woocommerce_product_set_stock', 'wc_get_variable_product_stock_quantity' );
function wc_get_variable_product_stock_quantity( $output = 'raw', $product_id = 0 ){
global $wpdb, $product;
// Get the product ID (can be defined)
$product_id = $product_id > 0 ? $product_id : get_the_id();
// Check and get the instance of the WC_Product Object
$product = is_a( $product, 'WC_Product' ) ? $product : wc_get_product($product_id);
// Only for variable product type
if( $product->is_type('variable') ){
// Get the stock quantity sum of all product variations (children)
$stock_quantity = $wpdb->get_var("
SELECT SUM(pm.meta_value)
FROM {$wpdb->prefix}posts as p
JOIN {$wpdb->prefix}postmeta as pm ON p.ID = pm.post_id
WHERE p.post_type = 'product_variation'
AND p.post_status = 'publish'
AND p.post_parent = '$product_id'
AND pm.meta_key = '_stock'
AND pm.meta_value IS NOT NULL
");
// Preparing formatted output
if ( $stock_quantity > 0 ) {
$html = '<p class="stock in-stock">'. sprintf( __("%s in stock", "woocommerce"), $stock_quantity ).'</p>';
} else {
if ( is_numeric($stock_quantity) )
$html = '<p class="stock out-of-stock">' . __("Out of stock", "woocommerce") . '</p>';
else
$html = '';
}
// Different output options
if( $output == 'echo_html' )
echo $html;
elseif( $output == 'return_html' )
return $html;
else
return $stock_quantity;
set_stock_quantity( $stock_quantity );
}
}

Edit Attribute Name in Additional Information tab on WooCommerce Product Page [duplicate]

Using "Reorder and customize product dimensions formatted output in WooCommerce" answer code il would like to make the output display as:
Size: D40 x W45 x H60 (cm)
Any help is appreciated.
Just add the follows code snippet in your active theme's functions.php to achieve the above -
function woocommerce_display_product_attributes( $product_attributes, $product ){
if( !isset( $product_attributes['dimensions'] ) ) return $product_attributes;
$modified_dimensions = array();
foreach ( $product->get_dimensions( false ) as $key => $value ) {
if( $key == 'length' )
$modified_dimensions[$key] = 'D'.$value;
if( $key == 'width' )
$modified_dimensions[$key] = 'W'.$value;
if( $key == 'height' )
$modified_dimensions[$key] = 'H'.$value;
}
$dimension_string = implode( ' × ', array_filter( array_map( 'wc_format_localized_decimal', $modified_dimensions ) ) );
if ( ! empty( $dimension_string ) ) {
$dimension_string .= ' (' . get_option( 'woocommerce_dimension_unit' ) . ')';
} else {
$dimension_string = __( 'N/A', 'woocommerce' );
}
// change dimensions label & value.
$product_attributes['dimensions']['label'] = __( 'Size', 'text-domain' );
$product_attributes['dimensions']['value'] = $dimension_string;
return $product_attributes;
}
add_filter( 'woocommerce_display_product_attributes', 'woocommerce_display_product_attributes', 99, 2 );
Welcome to StackOverflow!
Try a simpler code (tested):
add_filter( 'woocommerce_format_dimensions', 'custom_formated_product_dimentions', 10, 2 );
function custom_formated_product_dimentions( $dimension_string, $dimensions ){
if ( empty( $dimension_string ) )
return __( 'N/A', 'woocommerce' );
$dimensions = array_filter( array_map( 'wc_format_localized_decimal', $dimensions ) );
return 'D'.$dimensions['length'].' x W'.$dimensions['width'].' x H'.$dimensions['height'].' ('.get_option( 'woocommerce_dimension_unit' ).')';
}

Woocommerce add labels to custom dimension unit on front end

Need to implement custom dimension labels for custom dimensions output on front end.
I have found codes below which work well on their own but not together, so trying to implement the two into 1 compatible code.
code 1. Outputs woocommerce product dimensions on frontend in both
units "CM" & "IN"
x2 custom dimension units without custom labels
Code 1.
add_filter( 'woocommerce_format_dimensions', 'custom_format_dimensions', 10, 2 );
function custom_format_dimensions( $dimension_string, $dimensions ){
// Initializing variable
$dimentions2 = array();
// Loop though dimensions array (and set custom converted formatted decimals values in a new array)
foreach( $dimensions as $key => $value ){
if( ! empty($value) && $value != 0 )
$dimentions2[$key] = wc_format_localized_decimal( round($value * 2.54, 2) );
}
// Format default dimentions in a string
$dimension_string = implode( ' x ', array_filter( array_map( 'wc_format_localized_decimal', $dimensions ) ) );
if ( ! empty( $dimension_string ) ) {
// Format custom converted array in a string and append it to default formatted dimensions string
$dimension_string2 = ' ( ' . implode( ' x ', $dimentions2 ) . ' ' . __( 'in', 'woocommerce' ) . ' )';
$dimension_string .= ' ' . get_option( 'woocommerce_dimension_unit' ) . $dimension_string2;
} else {
$dimension_string = __( 'N/A', 'woocommerce' );
}
return $dimension_string;
}
Code 2. Add custom labels to output dimensions. For example "D X W X
H (cm)" & "D X W X H (in)"
Woocommerce dimension units with custom labels
add_filter( 'woocommerce_format_dimensions', 'custom_formated_product_dimensions', 10, 2 );
function custom_formated_product_dimensions( $dimension_string, $dimensions ){
if ( empty( $dimension_string ) )
return __( 'N/A', 'woocommerce' );
$dimensions = array_filter( array_map( 'wc_format_localized_decimal', $dimensions ) );
return 'D'.$dimensions['length'].' x W'.$dimensions['width'].' x H'.$dimensions['height'].' (cm)';
}
How to make code 1 output with code 2 labels? I have tried for many hours replacing dimensions array etc from one snippet to the other but no luck. Any help would be very much appreciated.
Admin feel free to edit!

Woo custom badges/bubbles conditions

I create a custom badge/bubble "New!" based on time published:
add_action( 'woocommerce_before_shop_loop_item', function() {
#add_action( 'woocommerce_single_product_image_html', function() {
$postdate = get_the_time( 'Y-m-d' ); // Post date
$postdatestamp = strtotime( $postdate ); // Timestamped post date
$newness = 10; // Newness in days
if ( ( time() - ( 60 * 60 * 24 * $newness ) ) < $postdatestamp ) {
echo '<div class="woo-entry-new-badge"><div class="woo-cust-new-badge">' . esc_html__( 'New!', 'woocommerce' ) . '</div></div>';
}
}, 20 );
But i want to add conditional to show if "on sale" is not set.
How should look like?
thanks!
WC_Product::is_on_sale() – Returns whether or not the product is on sale.
function my_callback_function() {
global $product;
$postdate = get_the_date( 'Y-m-d', $product->get_id() ); // Post date
$postdatestamp = strtotime( $postdate ); // Timestamped post date
$newness = 10; // Newness in days
$onsale = $product->is_on_sale();
if ( ( time() - ( 60 * 60 * 24 * $newness ) ) < $postdatestamp && !$onsale ) {
echo '<div class="woo-entry-new-badge"><div class="woo-cust-new-badge">' . esc_html__( 'New!', 'woocommerce' ) . '</div></div>';
}
}
add_action( 'woocommerce_before_shop_loop_item', 'my_callback_function', 10, 0 );

Resources