hesabixWpTheme/inc/woocommerce-cart.php

223 lines
7.5 KiB
PHP

<?php
/**
* سبد خرید — بازطراحی سبد خالی، ترجمهٔ بلوک‌ها و استایل یکپارچه.
*
* @package Hesabix2
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/**
* جایگزینی بلوک سبد خالی با قالب اختصاصی Hesabix2.
*
* @param string $block_content .
* @param array $block .
* @return string
*/
function hesabix2_wc_render_empty_cart_block( $block_content, $block ) {
if ( empty( $block['blockName'] ) || 'woocommerce/empty-cart-block' !== $block['blockName'] ) {
return $block_content;
}
if ( ! function_exists( 'is_cart' ) || ! is_cart() ) {
return $block_content;
}
ob_start();
hesabix2_wc_render_empty_cart_panel();
return (string) ob_get_clean();
}
add_filter( 'render_block', 'hesabix2_wc_render_empty_cart_block', 9, 2 );
/**
* رندر پنل سبد خالی.
*/
function hesabix2_wc_render_empty_cart_panel() {
$shop_url = function_exists( 'hesabix2_wc_shop_url' ) ? hesabix2_wc_shop_url() : home_url( '/shop/' );
$contact_url = function_exists( 'hesabix2_contact_page_url' ) ? hesabix2_contact_page_url() : home_url( '/contact/' );
?>
<div class="hesabix-wc-empty-cart wp-block-woocommerce-empty-cart-block">
<div class="hesabix-wc-empty-cart__hero">
<div class="hesabix-wc-empty-cart__icon" aria-hidden="true">
<svg width="72" height="72" viewBox="0 0 72 72" fill="none" xmlns="http://www.w3.org/2000/svg" focusable="false">
<circle cx="36" cy="36" r="34" stroke="currentColor" stroke-width="2" opacity="0.18"/>
<path d="M22 26h5.2l2.4 18.4h17.8l3.2-14H27" stroke="currentColor" stroke-width="2.4" stroke-linecap="round" stroke-linejoin="round"/>
<circle cx="33" cy="50" r="2.4" fill="currentColor"/>
<circle cx="47" cy="50" r="2.4" fill="currentColor"/>
<path d="M30 26l1.8-6h8.4" stroke="currentColor" stroke-width="2.4" stroke-linecap="round"/>
</svg>
</div>
<h2 class="hesabix-wc-empty-cart__title wc-block-cart__empty-cart__title">
<?php esc_html_e( 'سبد خرید شما خالی است', 'hesabix2' ); ?>
</h2>
<p class="hesabix-wc-empty-cart__lead">
<?php esc_html_e( 'هنوز محصولی به سبد اضافه نکرده‌اید. از فروشگاه حسابیکس دیدن کنید یا با پشتیبانی برای انتخاب محصول مناسب تماس بگیرید.', 'hesabix2' ); ?>
</p>
<div class="hesabix-wc-empty-cart__actions">
<a href="<?php echo esc_url( $shop_url ); ?>" class="hesabix-wc-empty-cart__cta hesabix-wc-empty-cart__cta--primary">
<?php esc_html_e( 'رفتن به فروشگاه', 'hesabix2' ); ?>
</a>
<a href="<?php echo esc_url( $contact_url ); ?>" class="hesabix-wc-empty-cart__cta hesabix-wc-empty-cart__cta--secondary">
<?php esc_html_e( 'تماس با پشتیبانی', 'hesabix2' ); ?>
</a>
</div>
</div>
<?php hesabix2_wc_render_empty_cart_suggestions(); ?>
</div>
<?php
}
/**
* پیشنهاد محصول در سبد خالی.
*/
function hesabix2_wc_render_empty_cart_suggestions() {
if ( ! function_exists( 'wc_get_products' ) ) {
return;
}
$products = wc_get_products(
array(
'status' => 'publish',
'limit' => 4,
'orderby' => 'date',
'order' => 'DESC',
)
);
if ( empty( $products ) ) {
return;
}
$shop_url = function_exists( 'hesabix2_wc_shop_url' ) ? hesabix2_wc_shop_url() : home_url( '/shop/' );
?>
<section class="hesabix-wc-cart-suggestions" aria-labelledby="hesabix-wc-cart-suggestions-title">
<div class="hesabix-wc-cart-suggestions__head">
<h3 id="hesabix-wc-cart-suggestions-title" class="hesabix-wc-cart-suggestions__title">
<?php esc_html_e( 'پیشنهادهای فروشگاه', 'hesabix2' ); ?>
</h3>
<a href="<?php echo esc_url( $shop_url ); ?>" class="hesabix-wc-cart-suggestions__all">
<?php esc_html_e( 'مشاهدهٔ همه', 'hesabix2' ); ?> <span aria-hidden="true">←</span>
</a>
</div>
<ul class="hesabix-wc-cart-suggestions__grid products columns-4">
<?php
global $post;
foreach ( $products as $product ) {
if ( ! $product instanceof WC_Product || ! $product->is_visible() ) {
continue;
}
$post_object = get_post( $product->get_id() );
if ( ! $post_object instanceof WP_Post ) {
continue;
}
$post = $post_object; // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited
setup_postdata( $post );
$GLOBALS['product'] = $product;
wc_get_template_part( 'content', 'product-cart-suggest' );
}
wp_reset_postdata();
?>
</ul>
</section>
<?php
}
/**
* نقشهٔ جایگزینی رشته‌های انگلیسی سبد.
*
* @return array<string, string>
*/
function hesabix2_wc_cart_string_replacements() {
return array(
'Your cart is currently empty!' => 'سبد خرید شما خالی است!',
'Your cart is currently empty.' => 'سبد خرید شما خالی است.',
'New in store' => 'پیشنهادهای فروشگاه',
'You may be interested in&hellip;' => 'شاید این‌ها را بپسندید…',
'You may be interested in...' => 'شاید این‌ها را بپسندید…',
'You may be interested in&hellip' => 'شاید این‌ها را بپسندید…',
'Cart totals' => 'جمع کل سبد',
'Proceed to Checkout' => 'ادامه به تسویه‌حساب',
'Proceed to checkout' => 'ادامه به تسویه‌حساب',
);
}
/**
* جایگزینی رشته‌های انگلیسی در HTML.
*
* @param string $content .
* @return string
*/
function hesabix2_wc_apply_cart_string_replacements( $content ) {
if ( ! is_string( $content ) || '' === $content ) {
return $content;
}
return str_replace(
array_keys( hesabix2_wc_cart_string_replacements() ),
array_values( hesabix2_wc_cart_string_replacements() ),
$content
);
}
/**
* ترجمهٔ رشته‌های انگلیسی باقی‌مانده در HTML بلوک سبد (محتوای ذخیره‌شده در برگه).
*
* @param string $content .
* @return string
*/
function hesabix2_wc_cart_content_fa( $content ) {
if ( ! function_exists( 'is_cart' ) || ! is_cart() || ! is_string( $content ) || '' === $content ) {
return $content;
}
return hesabix2_wc_apply_cart_string_replacements( $content );
}
add_filter( 'the_content', 'hesabix2_wc_cart_content_fa', 25 );
/**
* کلاس بدنه برای بازطراحی سبد.
*
* @param string[] $classes .
* @return string[]
*/
function hesabix2_wc_cart_body_class( $classes ) {
if ( function_exists( 'is_cart' ) && is_cart() ) {
$classes[] = 'hesabix-wc-cart-redesign';
}
return $classes;
}
add_filter( 'body_class', 'hesabix2_wc_cart_body_class', 26 );
/**
* یک‌بار: فارسی‌سازی محتوای ذخیره‌شدهٔ برگهٔ سبد (بلوک گوتنبرگ).
*/
function hesabix2_wc_maybe_localize_cart_page_content() {
if ( get_option( 'hesabix2_wc_cart_page_localized_v2' ) ) {
return;
}
if ( ! function_exists( 'wc_get_page_id' ) ) {
return;
}
$cart_id = wc_get_page_id( 'cart' );
if ( $cart_id <= 0 ) {
return;
}
$post = get_post( $cart_id );
if ( ! $post instanceof WP_Post ) {
return;
}
$content = hesabix2_wc_apply_cart_string_replacements( $post->post_content );
if ( $content !== $post->post_content ) {
wp_update_post(
array(
'ID' => $cart_id,
'post_content' => $content,
)
);
}
update_option( 'hesabix2_wc_cart_page_localized_v2', '1', false );
}
add_action( 'init', 'hesabix2_wc_maybe_localize_cart_page_content', 20 );