Я постараюсь быть более полным, насколько это возможно, потому что это довольно сложно, по крайней мере, чтобы объяснить, лол.
Как вы можете видеть на https://www.cascinacanova.it/en/acquista-online/ здесь у меня есть стандартный магазин Woocommerce. Цены видны и при регистрации они динамически меняются: старые цены зачеркиваются, а появляется новая цена со скидкой. Это возможно благодаря оптовым ценам на WooCommerce от Wholesale Suite и этому коду, который позволяет новым пользователям регистрироваться с привилегиями оптовых клиентов:
add_role( 'wholesale_customer', __( 'Wholesale Customer' ), array(
'read' => true,
));
add_filter( 'woocommerce_new_customer_data', 'bbloomer_assign_custom_role' );
function bbloomer_assign_custom_role( $args ) {
$args['role'] = 'wholesale_customer';
return $args;
}
Вот где начинается сложная часть: в https://www.cascinacanova.it/en/i-nostri-vini/ У меня есть несколько кнопок, которые должны показывать цену, и это самая простая часть. После этого сообщения о переполнении стека я сделал следующее:
Этот шорткод на кнопке:
[product_price id="37"]
Этот код в functions.php
function custom_price_shortcode_callback( $atts ) {
$atts = shortcode_atts( array(
'id' => null,
), $atts, 'product_price' );
$html = '';
if( intval( $atts['id'] ) > 0 && function_exists( 'wc_get_product' ) ){
// Get an instance of the WC_Product object
$product = wc_get_product( intval( $atts['id'] ) );
// Get the product prices
$price = wc_get_price_to_display( $product, array( 'price' => $product->get_price() ) ); // Get the active price
$regular_price = wc_get_price_to_display( $product, array( 'price' => $product->get_regular_price() ) ); // Get the regular price
$sale_price = wc_get_price_to_display( $product, array( 'price' => $product->get_sale_price() ) ); // Get the sale price
// Your price CSS styles
$style1 = 'style="font-size:40px;color:#e79a99;font-weight:bold;"';
$style2 = 'style="font-size:25px;color:#e79a99"';
// Formatting price settings (for the wc_price() function)
$args = array(
'ex_tax_label' => false,
'currency' => 'EUR',
'decimal_separator' => '.',
'thousand_separator' => ' ',
'decimals' => 2,
'price_format' => '%2$s %1$s',
);
// Formatting html output
if( ! empty( $sale_price ) && $sale_price != 0 && $sale_price < $regular_price )
$html = "<del $style2>" . wc_price( $regular_price, $args ) . "</del> <ins $style1>" . wc_price( $sale_price, $args ) . "</ins>"; // Sale price is set
else
$html = "<ins $style1>" . wc_price( $price, $args ) . "</ins>"; // No sale price set
}
return $html;
}
add_shortcode( 'product_price', 'custom_price_shortcode_callback' );
Вот в чем проблема, потому что этот код идет и принимает обычную цену со скидкой, установленную Woocommerce, а не то, что мы называем оптовой ценой.
Основная проблема заключается в том, что цена должна быть динамичной даже здесь, вне магазина. Итак, мне понадобится способ отредактировать этот код, чтобы показать цену продукта и оптовую цену при регистрации.
Дайте мне знать, если вам нужна дополнительная информация, и заранее спасибо за вашу помощь!