distributor icon indicating copy to clipboard operation
distributor copied to clipboard

Change the price of a product

Open cdesp opened this issue 2 years ago • 1 comments

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: `/**

  • Set default post meta if not set in the original.
  • @param {array} $meta All received meta for the post
  • @param {array} $existing_meta Existing meta for the post
  • @param {int} $post_id Post ID */ function client_prefix_modify_meta( $post_meta, $existing_meta, $post_id ) { // Set post meta if not set. $existing_meta[ '_regular_price'] = '45.55'; $existing_meta[ '_sale_price'] = '34.55'; $existing_meta[ 'post_regular_price'] = '645.55'; $existing_meta[ 'post_sale_price'] = '634.55'; } add_action( 'dt_after_set_meta', 'client_prefix_modify_meta', 10, 3 );

add_filter( 'dt_push_post_meta', function( $a, $new_post_id, $post_meta, $post_id, $args, $connection ) {

// $field_prefix =( $connection instanceof \Distributor\ExternalConnections\WordPressExternalConnection ) ? '' : 'post_';

$post_meta[ '_regular_price'] = '45.55';
$post_meta[ '_sale_price'] = '34.55';
$post_meta[ 'post_regular_price'] = '645.55';
$post_meta[ 'post_sale_price'] = '634.55';

return true;

},10,6);

/**

  • Keep the publication date on the new pushed post.

  • 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 expects post_date. $field_prefix =( $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

  • [X] I agree to follow this project's Code of Conduct

cdesp avatar Jan 17 '24 20:01 cdesp

To anyone interesteed i manage to do what i wanted using this snippetQ ` /**

  • Set default post meta if not set in the original.

  • @param {array} $meta All received meta for the post

  • @param {array} $existing_meta Existing meta for the post

  • @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 ); `

cdesp avatar Jan 21 '24 14:01 cdesp