• Resolved shamoonalitee

    (@shamoonalitee)


    I’m creating a small marketplace on wordpress using WooCommerce, i want Vendors to be able to change the price of the products. How can i do that using PHP?

Viewing 14 replies - 1 through 14 (of 14 total)
  • Plugin Support RK a11n

    (@riaanknoetze)

    Hi there,

    Have you considered using the dedicated https://woocommerce.com/products/product-vendors/ extension? I’m asking as it sounds like it covers the exact use-case you’re looking for.

    If, however, you’re looking to add that using custom coding: I’m going to leave it open for a bit to see if anyone is able to chime in to help you out considering the code required is quite in-depth.

    I can also recommend the following places for more development-oriented questions:

    1. WooCommerce Slack Community: https://woocommerce.com/community-slack/
    2. Advanced WooCommerce group on Facebook: https://www.facebook.com/groups/advanced.woocommerce/

    The product object is described here:
    https://docs.woocommerce.com/wc-apidocs/class-WC_Product.html

    You can use the methods: set_regular_price(), set_sale_price() and set_price(). If there is a sale_price, price will be the sale price, if not then price will be the regular price.

    Thread Starter shamoonalitee

    (@shamoonalitee)

    @riaanknoetze Yes I’ve seen that Plugin, its not really what i was looking for my project is a little different. Anyways, thank you so much for the links!

    Thread Starter shamoonalitee

    (@shamoonalitee)

    @lorro Thanks! but How can I use these functions for just a single product, that it updates its price not only on display but in the database as well? Is there a way of doing that or am I asking for something that cannot be done?

    I tried adding this code

    function return_custom_price($price, $product) {
        global $post, $blog_id;
        $product = wc_get_product( '$post_id' );
        $post_id = $post->ID;
        $wc_product_id = 168;
        if($post_id == $wc_product_id) {
           $price = 77;
        }
        return $price;
    }
    add_filter('woocommerce_get_price', 'return_custom_price', 10, 2);
    
    

    It does change the displayed price of the product, but the actual price in the database and the woocommerce dashboard remains the same.

    • This reply was modified 4 years, 10 months ago by shamoonalitee.
    • This reply was modified 4 years, 10 months ago by shamoonalitee.

    Sorry, not tested:

    // ...
    $product->set_regular_price( $regular_price );
    if( $sale_price ) {
      $product->set_sale_price( $sale_price );
      $new_price = $sale_price;
    } else {
      $new_price = $regular_price;
    }
    $product->set_price( $new_price );
    $product->save()
    Thread Starter shamoonalitee

    (@shamoonalitee)

    @lorro Thanks man! i’ll give it a try!

    Thread Starter shamoonalitee

    (@shamoonalitee)

    Thanks guys i finally got the code working… going to post the code here so that someone else with the same query can view it.

    
    add_action( 'get_header', 'change_and_save_product_price', 10, 2 );
    
    function change_and_save_product_price() {
        if ( get_post_type() === "product" ){ // or use: is_product()
            // Regular Price
            $regular_price = 400;
    
    	// Sale Price (If you want to add any)
    	$sale_price = 336;
    
    	// Set the product ID of the product you want to change the price of
    	$productID = 167;
    
    	// Get product
            $product = wc_get_product($productID);
    
    	// Set regular price
    	$product->set_regular_price( $regular_price );
    
    	// If your product has a Sale price
    	if( $sale_price ) {
    	$product->set_sale_price( $sale_price );
    	$new_price = $sale_price;
    	} else {
    	$new_price = $regular_price;
    				}
    	// Set new price
    	$product->set_price( $new_price );
    
    	// Save to database (Note: You might want to clear cache of your page and then reload, if it still doesn't show up go to the product page and check there.)
    	$product->save();
        }
    }
    do_action('get_header');
    
    • This reply was modified 4 years, 10 months ago by shamoonalitee.

    You may need to delete the relevant product transient:
    wc_delete_product_transients( $post_id );

    This is the cached product. The cache will remake itself next time the product is queried.

    Thread Starter shamoonalitee

    (@shamoonalitee)

    @lorro Oh thanks! didn’t about this…

    Hi! I try your code and It’s worked perfec!

    But I need to add a issue, if the price is to saved in a php variable the code doesn’t work, any idea??

    $precio_producto = $precio_gramo * $row_peso[3] * $row_porc[3];

    add_action( ‘get_header’, ‘change_and_save_product_price’, 10, 2 );

    function change_and_save_product_price() {

    if ( get_post_type() === “product” ){ // or use: is_product()

    // Regular Price

    $regular_price = $precio_producto;

    // Sale Price (If you want to add any)

    //$sale_price = ;

    // Set the product ID of the product you want to change the price of

    $productID = 732;

    // Get product

    $product = wc_get_product($productID);

    // Set regular price

    $product->set_regular_price( $regular_price );

    // If your product has a Sale price

    if( $sale_price ) {

    $product->set_sale_price( $sale_price );

    $new_price = $sale_price;

    } else {

    $new_price = $regular_price;

    }

    // Set new price

    $product->set_price( $new_price );

    // Save to database

    $product->save();

    }

    }

    do_action(‘get_header’);

    To be able to use a value from outside a function you could use:
    global $precio_producto;
    in all functions where it occurs.

    Thank you @lorro but it continues without returning any value:

    $precio_producto = $precio_gramo * $row_peso[3] * $row_porc[3];
    global $precio_producto;
    add_action( ‘get_header’, ‘change_and_save_product_price’, 10, 2 );

    function change_and_save_product_price() {
    if ( get_post_type() === “product” ){ // or use: is_product()
    // Regular Price
    $regular_price = $precio_producto;

    // Sale Price (If you want to add any)
    //$sale_price = ;

    // Set the product ID of the product you want to change the price of
    $productID = 732;

    // Get product
    $product = wc_get_product($productID);

    // Set regular price
    $product->set_regular_price( $regular_price );

    // If your product has a Sale price
    if( $sale_price ) {
    $product->set_sale_price( $sale_price );
    $new_price = $sale_price;
    } else {
    $new_price = $regular_price;
    }
    // Set new price
    $product->set_price( $new_price );

    // Save to database (Note: You might want to clear cache of your page and then reload, if it still doesn’t show up go to the product page and check there.)
    $product->save();
    }
    }
    do_action(‘get_header’);

    You need the global inside the function as well.

    I get it ??

    add_action( 'get_header', 'change_and_save_product_price', 10, 2 );
    
    function change_and_save_product_price() {
        
        global $precio_producto;
        if ( get_post_type() === "product" ){ // or use: is_product()
            // Regular Price
            $regular_price = $precio_producto;
    
    	// Sale Price (If you want to add any)
    	//$sale_price = ;
    
    	// Set the product ID of the product you want to change the price of
    	$productID = 732;
    
    	// Get product
            $product = wc_get_product($productID);
    
    	// Set regular price
    	$product->set_regular_price( $regular_price );
    
    	// If your product has a Sale price
    	if( $sale_price ) {
    	$product->set_sale_price( $sale_price );
    	$new_price = $sale_price;
    	} else {
    	$new_price = $regular_price;
    				}
    	// Set new price
    	$product->set_price( $new_price );
    
    	// Save to database (Note: You might want to clear cache of your page and then reload, if it still doesn't show up go to the product page and check there.)
    	$product->save();
        }
    }
    do_action('get_header');
Viewing 14 replies - 1 through 14 (of 14 total)
  • The topic ‘How to change the price of a single product of woocommerce using php?’ is closed to new replies.