【问题标题】:Add link to the product title on WooCommerce product_page shortcode在 WooCommerce product_page 短代码上添加指向产品标题的链接
【发布时间】:2021-05-21 04:27:17
【问题描述】:

在我们网站的主页上,我们添加了一个短代码来显示特色产品的产品页面。

我已在特色产品中添加了一个 SKU“特色”,因此我可以使用以下短代码来显示特色产品 [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链接来包装它,但我担心我在这方面的知识有点欠缺。

有人可以帮我解决这个问题吗?非常感谢!

【问题讨论】:

  • “您可以看到他们使用 $atts['show_title'] 来处理 product_title。” - 这不是实际的产品标题,这只是确定是否是否显示标题。根据此选项,woocommerce_template_single_title 函数将从操作列表中删除/添加到操作列表中。
  • 您需要基于 wooCommerce one 构建自己的简码...
  • @LoicTheAztec 好的!这是您可以帮助我的事情吗?

标签: php wordpress woocommerce product shortcode


【解决方案1】:

在您的functions.php 中使用下面的代码sn-p 并查看一下。 它将删除现有标题并使用锚标记添加新标题。

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 来隐藏基于父类的现有标题。

【讨论】:

  • 你是一个绝对的英雄,@momin-iqbal,工作就像一种享受!
猜你喜欢
  • 1970-01-01
  • 2018-08-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-07-12
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多