WooCommerce добавить в корзину и переопределить цену - PullRequest
1 голос
/ 28 апреля 2020

Используя последнюю версию woocommerce V4.01 в WordPress v5.4, я целую вечность тралялся через inte rnet и, похоже, не смог найти рабочий ответ.

При добавлении товар в корзину по URL-ссылке, мне нужно переопределить цену корзины и ввести новую цену.

Вот что у меня есть на странице функций

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

Вышеуказанные работы но не правильно и имеет следующую проблему:

Я проверил оператор strpos, и он работает нормально. поэтому для custom_price должно быть установлено значение 12, если утверждение strpos истинно (что происходит, если я добавляю домен .com), но оно продолжает вводить ложное значение 10 Я потянул мои волосы за этот совет. Любой совет с благодарностью. Большое спасибо

1 Ответ

0 голосов
/ 28 апреля 2020
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' );

Попробуйте этот код

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...