Open cdesp opened 7 months ago
To anyone interesteed i manage to do what i wanted using this snippetQ ` /**
@param {int} $post_id Post ID */ function client_prefix_modify_meta( $post_meta, $existing_meta, $post_id ) { // Set post meta if not set.
//if ( $post_meta->post_type != 'product') return; // Only products
$disc = 0.03; //3% discount
$regprice=round($post_meta[ '_regular_price'][0] * (1-$disc),2) ; $price = $regprice; $saleprice=$post_meta[ '_sale_price'][0] ;
update_post_meta( $post_id, '_regular_price', $regprice );
if ($saleprice!='') { $saleprice=round($saleprice * (1-$disc),2) ; $price = $saleprice; update_post_meta( $post_id, '_sale_price', $saleprice ); } update_post_meta( $post_id, '_price', $price ); } add_action( 'dt_after_set_meta', 'client_prefix_modify_meta', 10, 3 ); `
Describe your question
Hello,
i want to change the price of a product whenever it changes on the original, so the copied product will have a percentage of the original.
I tried like this and failed: `/**
add_filter( 'dt_push_post_meta', function( $a, $new_post_id, $post_meta, $post_id, $args, $connection ) {
// $fieldprefix =( $connection instanceof \Distributor\ExternalConnections\WordPressExternalConnection ) ? '' : 'post';
},10,6);
/**
This filter is used to filter the arguments sent to the remote server during a push. The below code snippet passes the original published date to the new pushed post and sets the same published date instead of setting it as per the current time. */ add_filter( 'dt_push_post_args', function( $post_body, $post, $args, $connection ) {
// When pushing to an external connection, we use the REST API, so the name of the field is
date
. // But when pushing to an internal connection, the attributes are sent to wp_insert_post, which expectspost_date
. $fieldprefix =( $connection instanceof \Distributor\ExternalConnections\WordPressExternalConnection ) ? '' : 'post';$post_body[ $field_prefix . 'date'] = $post->post_date; $post_body[ $field_prefix . 'date_gmt'] = $post->post_date_gmt; $post_body[ $field_prefix . 'regular_price'] = $post->regular_price-10; $post_body[ $field_prefix . 'sale_price'] = '3'; $post_body[ $field_prefix . 'description'] = 'chris4'; $post_body[ $field_prefix . 'content'] = "chris4w";
return $post_body; }, 10, 4 ); `
any help will be appreciated.
Code of Conduct