簡體   English   中英

在 WooCommerce product_page 短代碼上添加指向產品標題的鏈接

[英]Add link to the product title on WooCommerce product_page shortcode

在我們網站的主頁上,我們添加了一個短代碼來顯示特色產品的產品頁面。

我已向特色產品添加了 SKU 'featured',因此我可以使用以下短代碼來顯示特色產品[product_page sku="featured"]

現在我想修改短代碼的代碼,以便產品名稱鏈接到全功能產品頁面。

下面是設置product_page的WooCommerce。

/**
 * Show a single product page.
 *
 * @param array $atts Attributes.
 * @return string
 */
public static function product_page( $atts ) {
    if ( empty( $atts ) ) {
        return '';
    }

    if ( ! isset( $atts['id'] ) && ! isset( $atts['sku'] ) ) {
        return '';
    }

    $args = array(
        'posts_per_page'      => 1,
        'post_type'           => 'product',
        'post_status'         => 'publish',
        'ignore_sticky_posts' => 1,
        'no_found_rows'       => 1,
    );

    if ( isset( $atts['sku'] ) ) {
        $args['meta_query'][] = array(
            'key'     => '_sku',
            'value'   => sanitize_text_field( $atts['sku'] ),
            'compare' => '=',
        );

        $args['post_type'] = array( 'product', 'product_variation' );
    }

    if ( isset( $atts['id'] ) ) {
        $args['p'] = absint( $atts['id'] );
    }

    // Don't render titles if desired.
    if ( isset( $atts['show_title'] ) && ! $atts['show_title'] ) {
        remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_title', 5 );
    }

    // Change form action to avoid redirect.
    add_filter( 'woocommerce_add_to_cart_form_action', '__return_empty_string' );

    $single_product = new WP_Query( $args );

    $preselected_id = '0';

    // Check if sku is a variation.
    if ( isset( $atts['sku'] ) && $single_product->have_posts() && 'product_variation' === $single_product->post->post_type ) {

        $variation  = new WC_Product_Variation( $single_product->post->ID );
        $attributes = $variation->get_attributes();

        // Set preselected id to be used by JS to provide context.
        $preselected_id = $single_product->post->ID;

        // Get the parent product object.
        $args = array(
            'posts_per_page'      => 1,
            'post_type'           => 'product',
            'post_status'         => 'publish',
            'ignore_sticky_posts' => 1,
            'no_found_rows'       => 1,
            'p'                   => $single_product->post->post_parent,
        );

        $single_product = new WP_Query( $args );
    ?>
        <script type="text/javascript">
            jQuery( document ).ready( function( $ ) {
                var $variations_form = $( '[data-product-page-preselected-id="<?php echo esc_attr( $preselected_id ); ?>"]' ).find( 'form.variations_form' );

                <?php foreach ( $attributes as $attr => $value ) { ?>
                    $variations_form.find( 'select[name="<?php echo esc_attr( $attr ); ?>"]' ).val( '<?php echo esc_js( $value ); ?>' );
                <?php } ?>
            });
        </script>
    <?php
    }

    // For "is_single" to always make load comments_template() for reviews.
    $single_product->is_single = true;

    ob_start();

    global $wp_query;

    // Backup query object so following loops think this is a product page.
    $previous_wp_query = $wp_query;
    // @codingStandardsIgnoreStart
    $wp_query          = $single_product;
    // @codingStandardsIgnoreEnd

    wp_enqueue_script( 'wc-single-product' );

    while ( $single_product->have_posts() ) {
        $single_product->the_post()
        ?>
        <div class="single-product" data-product-page-preselected-id="<?php echo esc_attr( $preselected_id ); ?>">
            <?php wc_get_template_part( 'content', 'single-product' ); ?>
        </div>
        <?php
    }

    // Restore $previous_wp_query and reset post data.
    // @codingStandardsIgnoreStart
    $wp_query = $previous_wp_query;
    // @codingStandardsIgnoreEnd
    wp_reset_postdata();

    // Re-enable titles if they were removed.
    if ( isset( $atts['show_title'] ) && ! $atts['show_title'] ) {
        add_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_title', 5 );
    }

    remove_filter( 'woocommerce_add_to_cart_form_action', '__return_empty_string' );

    return '<div class="woocommerce">' . ob_get_clean() . '</div>';
}

您可以看到他們使用$atts['show_title']來處理 product_title 。 所以我想我們應該用產品鏈接的href鏈接來包裝它,但我擔心我在這方面的知識缺乏一點。

有人可以幫我解決這個問題嗎? 非常感激!

在您的函數中使用以下代碼片段。php 並查看。 它將刪除現有標題並使用錨標記添加新標題。

remove_action('woocommerce_single_product_summary','woocommerce_template_single_title',5);
add_action('woocommerce_single_product_summary', 'woocommerce_my_single_title',5);

if ( ! function_exists( 'woocommerce_my_single_title' ) ) {
   function woocommerce_my_single_title() {
?>
            <a href="<?php echo get_the_permalink(); ?>"><span>dfgdfg<?php the_title(); ?></span></a>
<?php
    }
}

注意:如果它沒有刪除,那么你可以給一些 css 來隱藏基於父 class 的現有標題。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM