• Resolved lucytech

    (@lucytech)


    I need to write code to check the product_id.

    I’m using the functions:

    if ( wc_current_user_has_role( ‘whole_sale’ ) ) {

    if(get_post_meta( $product->get_id(), ‘_regular_price’, true )){

    $gift_id = $product->get_id();
    if($gift_id == ‘49372’) {

    $price_unchanged = ‘$’ . wc_get_price_to_display( $product );
    return $price_unchanged;
    I works for all the products except for the one I created with your plugin.

    How do I find the product_id so I can code just for that product_id?

Viewing 11 replies - 1 through 11 (of 11 total)
  • Plugin Author pimwick

    (@pimwick)

    The PW Gift Card product is an extension of the Variable Product type that is built into WooCommerce.

    The code you provided is out of context, however I assume that $product is the parent (Variable) product and not the individual Variation. As a result, there is no ‘_regular_price’ field on the Variable product since it is on each individual Variation.

    Are you able to access the specific Variation where this code is being used? That will fix the issue.

    Thread Starter lucytech

    (@lucytech)

    add_filter( 'woocommerce_variable_price_html', 'custom_min_max_variable_price_html', 10, 2 );
    
    function custom_min_max_variable_price_html( $price, $product ) {
    
                    if ( is_admin() ) return $price;
    
                                                 $min_var_reg_price = $product->get_variation_regular_price( 'min', true );
    
                                                 $min_var_sale_price = $product->get_variation_sale_price( 'min', true );
    
                                                 $max_var_reg_price = $product->get_variation_regular_price( 'max', true );
    
                                                 $max_var_sale_price = $product->get_variation_sale_price( 'max', true );
    
                                                 if ( ! ( $min_var_reg_price == $max_var_reg_price && $min_var_sale_price == $max_var_sale_price ) ) {  
    
                                                    if ( $min_var_sale_price < $min_var_reg_price ) {
    
                                                                   if ( wc_current_user_has_role( 'whole_sale' ) ) {
    
                                                                  $price = sprintf( __( 'From: <del>%1$s</del><ins>%2$s</ins>', 'woocommerce' ), wc_price( round($min_var_reg_price/2,2) ), wc_price( round($min_var_sale_price/2,2) ) );
    
                                                                   }else{
    
                                                                               $price = sprintf( __( 'From: <del>%1$s</del><ins>%2$s</ins>', 'woocommerce' ), wc_price( $min_var_reg_price ), wc_price($min_var_sale_price)  );

    I have this code for the variable products. I don’t want to show the price as half price for the gift card.

    How can I check if the product_id = gift card and then run the correct card?

    Plugin Author pimwick

    (@pimwick)

    You should be able to see if the product is a PW Gift Card and ignore the display for the wholesale customers.

    For example:

    if ( wc_current_user_has_role( 'whole_sale' ) && !is_a( $product, 'WC_Product_PW_Gift_Card' ) ) {
    Thread Starter lucytech

    (@lucytech)

    that was really helpful for the product page, thank you.

    Now I need to adjust the code for the cart page – all prices are half price except for the gift card. This is my current code:

    foreach ( $cart->get_cart() as $cart_item_key => $cart_item ) {
    $product = $cart_item[‘data’];
    $price = $product->get_price();
    $cart_item[‘data’]->set_price(round($price/2,2));
    }

    Is there $cart_item[‘data’] I can use that is unique for pw gift card to make an exception for the gift card?

    Plugin Author pimwick

    (@pimwick)

    Same principle applies here, you will need to check the cart item’s parent to see if it is a gift card. Something like this may work:

    foreach ( $cart->get_cart() as $cart_item_key => $cart_item ) {
        $product = $cart_item['data'];
        $price = $product->get_price();
    
        $half_price = true;
    
        if ( is_a( $product, 'WC_Product_Variation' ) ) {
            $parent_product = wc_get_product( $product->get_parent_id() );
            if ( is_a( $parent_product, 'WC_Product_PW_Gift_Card' ) ) {
                $half_price = false;
            }
        }
    
        if ( $half_price ) {
            $cart_item['data']->set_price(round($price/2,2));
        }
    }
    Thread Starter lucytech

    (@lucytech)

    perfect!!

    Thank you so much, that code was a huge help.

    Thanks for a great plugin.

    Plugin Author pimwick

    (@pimwick)

    You are welcome, happy to help. If you enjoy the plugin we would love to have your review on WordPress.org when you have a minute.

    I am marking this thread as resolved, best of luck with your store!

    Thread Starter lucytech

    (@lucytech)

    I wrote a review – thanks. 

    One more question – I’ve created a product and the page exists but when I search for the product from the dashboard it is not listed? 

    Why is that??

    • This reply was modified 1 year, 1 month ago by lucytech.
    Plugin Author pimwick

    (@pimwick)

    Thanks for the review!

    The issue you describe sounds like the Catalog Visibility for the product is marked as Shop Only. To change this, log into your WordPress admin area and click the Products menu on the left. Edit the product and look for Catalog Visibility in the top right box. Click Edit and set the visibility to “Shop and Search Results”.

    Thread Starter lucytech

    (@lucytech)

    The settings are fine there – see image.

    It simply does not show in the product list…

    Thread Starter lucytech

    (@lucytech)

    I added 1234 for the sku and now it is showing. before it was blank and it didn’t show on the product list.

Viewing 11 replies - 1 through 11 (of 11 total)
  • The topic ‘product id’ is closed to new replies.