• Resolved gabi_cavaller

    (@gabi_cavaller)


    Hi there,

    Wondering if anyone can assist.

    I have created the query below, to update the price (throughout the site)

    Unfortunately it is updating the product price, but the cart is saying £0, as well as emails and everything else.

    I am simply grabbing the price from another field (bespoke field) stored in the attribute > _wclsi_ls_obj

    Now if I change web price to;

    $webPrice = “100”;

    It will show everything correctly.

    However the code below must be working or it would not show the price on the product page.

    > $webPrice = $postMeta->Prices->ItemPrice[2]->amount;

    Any help/guidance really appreciated.

    Will send you some beers if you want ??

    function return_custom_price($price, $product) {
      global $post;
      $post_id = $post->ID;
      $postMeta = get_post_meta($post_id, '_wclsi_ls_obj', true);
      $webPrice = $postMeta->Prices->ItemPrice[2]->amount;
      return $webPrice;
    } 
    
    add_filter('woocommerce_get_price', 'return_custom_price', 10, 2);

    https://www.ads-software.com/plugins/woocommerce/

Viewing 6 replies - 1 through 6 (of 6 total)
  • Caleb Burks

    (@icaleb)

    Automattic Happiness Engineer

    It is working correctly on the product pages because global $post is pulling in the post object for the current page, which you then get the correct ID from.

    But when this code runs on the cart/checkout pages, the $post_id = $post->ID line is not doing what you need it to do. Instead of using the $post object to get the id, use the $product object that is passed into the filter.

    Thread Starter gabi_cavaller

    (@gabi_cavaller)

    Caleb,

    Thanks very much, I thought that might have been the case and had tested it out, but did not work.

    Do you happen to have sample coding I can can a look at? or even better, what do you think the code should be?

    Really appreciate it.

    Many thanks,

    Thread Starter gabi_cavaller

    (@gabi_cavaller)

    function return_custom_price($price, $product) {
      global $post, $product;
      $post_id = $product->id;
      $postMeta = get_post_meta($post_id, '_wclsi_ls_obj', true);
      $webPrice = $postMeta->Prices->ItemPrice[2]->amount;
    
      return $webPrice;
    
    }
    add_filter('woocommerce_get_price', 'return_custom_price', 10, 2);

    This is what I am trying to use.

    Plugin Contributor Mike Jolley (a11n)

    (@mikejolley)

    Remove the ‘global $post, $product;’ part.You’re given the product via the function args.

    Thread Starter gabi_cavaller

    (@gabi_cavaller)

    Mike, I love you. That was it. 100% !! Where do you want me to send some beers to?? !!!

    Plugin Contributor Mike Jolley (a11n)

    (@mikejolley)

    Help someone else on this forum and we’ll call it quits ??

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘woocommerce_get_price’ is closed to new replies.