Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change the price of a product #1183

Open
1 task done
cdesp opened this issue Jan 17, 2024 · 1 comment
Open
1 task done

Change the price of a product #1183

cdesp opened this issue Jan 17, 2024 · 1 comment
Labels
type:question Further information is requested.

Comments

@cdesp
Copy link

cdesp commented Jan 17, 2024

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

  • I agree to follow this project's Code of Conduct
@cdesp cdesp added the type:question Further information is requested. label Jan 17, 2024
@cdesp
Copy link
Author

cdesp commented Jan 21, 2024

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type:question Further information is requested.
Projects
Status: Incoming
Development

No branches or pull requests

1 participant