1

Using the latest version of woocommerce V4.01 in WordPress v5.4 I have been trawling for ages through the internet and do not seem to be able to find a working answer.

When adding an item to the cart via URL link, I need to override the cart price and have the new price entered.

Here is what I have on my functions page

function add_custom_price( $cart_object ) {
    $target_product_id   = 6048;
    if ( !isset( $_GET[ 'add-to-cart' ] ) ) //** this is the product id sent through
        $add_to_cart         = esc_attr( $_GET[ 'add-to-cart' ] );
    if ( $add_to_cart        = $target_product_id ) {
        $domain_name_meta    = esc_attr( $_GET[ 'domain_name_meta' ] ); //**the domain with extension sent through
        $reg                 = strtolower( substr( $domain_name_meta, -4 ) );
        $ext                 = ".com";
        if ( strcmp( $reg, $ext ) !== 0 ) {
            $custom_price = 10;
        } else {
            $custom_price = 12;
        }
        foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
            if ( $cart_item[ 'product_id' ] == $target_product_id ) {
                $cart_item[ 'data' ]->price  = $custom_price;
                $found                       = true;
                $cart_item[ 'data' ]->set_price( $custom_price );
            }
        }
    }
}

add_action( 'woocommerce_before_calculate_totals', 'add_custom_price' );

The above works but not correctly and has the following issue:

I have checked the strpos statement and it works fine. so the custom_price should set to 12 if the strpos statement is true (Which it is if I add a .com domain) but it keeps entering the false value of 10 Been pulling my hair out on this one Any advice greatly appreciated. Many thanks

mujuonly
  • 11,370
  • 5
  • 45
  • 75
Patdundee
  • 161
  • 2
  • 10
  • https://stackoverflow.com/a/6987496/1117368 - check this – mujuonly Apr 27 '20 at 23:42
  • Hi Still not working. I have tried both strcmp anmd strpos which are both case sensitive and i have made everything lower case but still does not like it – Patdundee Apr 28 '20 at 00:17
  • Whats the output of esc_attr( $_GET['domain_name_meta'] ) ? – mujuonly Apr 28 '20 at 04:44
  • Hi the output is a domain name eg: anydomain.com or other TLDc. At the moment i am testing it with anydomain.com so the last 4 selected from the substr are .com. I have corrected an error in the code above (I typed it in wrong here) the correct code is below. It works everywhere else. For example display the custom_price on the screen. It is only setting the custom price in the database where it goes wrong – Patdundee Apr 28 '20 at 09:26
  • I have just adjusted the code above to the current section in use with the errors corrected but still the same – Patdundee Apr 28 '20 at 09:32

1 Answers1

0
function add_custom_price( $cart_object ) {
    $target_product_id   = 6048;
    if ( !empty( $_GET[ 'add-to-cart' ] ) ) //** this is the product id sent through
        $add_to_cart         = esc_attr( $_GET[ 'add-to-cart' ] );
    if ( $add_to_cart        = $target_product_id ) {
        $domain_name_meta = esc_attr( $_GET[ 'domain_name_meta' ] ); //**the domain with extension sent through

        $custom_price = 10;
        if ( strtolower( substr( $domain_name_meta, -4 ) ) === ".com" ) {
            $custom_price = 12;
        }
        foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
            if ( $cart_item[ 'product_id' ] == $target_product_id ) {
                $cart_item[ 'data' ]->price  = $custom_price;
                $found                       = true;
                $cart_item[ 'data' ]->set_price( $custom_price );
            }
        }
    }
}

add_action( 'woocommerce_before_calculate_totals', 'add_custom_price' );

Try this code

mujuonly
  • 11,370
  • 5
  • 45
  • 75
  • Hi Still the same result :( even if it is a .com still adds the 10 and not the 12 But many thnaks for looking at it – Patdundee Apr 28 '20 at 11:42