【问题标题】:Use Different Product Title For Shop/Category Pages为商店/类别页面使用不同的产品标题
【发布时间】:2019-06-26 00:34:21
【问题描述】:

我正在寻找一种在不影响单个产品页面标题的情况下为商店页面使用不同产品标题的解决方案。 这是我想要实现的示例 https://www.slickwraps.com/devices/phones/ios/iphone-xs-max.html 您可以在上面的链接中看到商店页面上的产品标题不同,我知道这个网站是在不同的 CMS 中构建的,但是我们可以在 Woocommerce 中获得类似的结果吗?

【问题讨论】:

    标签: wordpress woocommerce variations


    【解决方案1】:

    这是你可以做的。将以下代码添加到主题 functions.php 文件中。该代码将在产品数据 -> 常规选项下添加自定义标题选项。如果在管理中没有添加自定义标题,它将显示默认标题。

    在后端添加自定义字段

        // Display the custom text field
        function mos_create_custom_field() {
            $args = array(
            'id' => 'shop_page_field_title',
            'label' => __( 'Shop Page product title' ),
            'class' => 'mos-custom-field',
            'desc_tip' => true,
            'description' => __( 'Shop Page product title.', 'ctwc' ),
            );
            woocommerce_wp_text_input( $args );
        }
        add_action( 'woocommerce_product_options_general_product_data', 'mos_create_custom_field' );
        add_action( 'woocommerce_product_options_inventory_product_data', 'mos_create_custom_field' );
    

    保存自定义字段值

    // Save the custom field
    function mos_save_custom_field( $post_id ) {
        $product = wc_get_product( $post_id );
        $title = isset( $_POST['shop_page_field_title'] ) ? $_POST['shop_page_field_title'] : '';
        $product->update_meta_data( 'shop_page_field_title', sanitize_text_field( $title ) );
        $product->save();
    }
    add_action( 'woocommerce_process_product_meta', 'mos_save_custom_field' );
    

    在商店页面上显示

    // remove the default action for product title on shop page
    remove_action( 'woocommerce_shop_loop_item_title', 'woocommerce_template_loop_product_title' );
    
    // add the custom action to show the custom product title
    add_action( 'woocommerce_shop_loop_item_title', 'custom_woocommerce_template_loop_product_title', 99 );
    
    function custom_woocommerce_template_loop_product_title() {
        global $post;
    
        $product = wc_get_product( $post->ID );
        $title = $product->get_meta( 'shop_page_field_title' );
        if( $title ) {
            // Only display our field if we've got a value for the field title
            echo '<h2 class="woocommerce-loop-product__title">' . esc_html( $title ) . '</h2>';
        } else {
            // else display the defaul title
            echo '<h2 class="woocommerce-loop-product__title">' . esc_html( $product->get_title() ) . '</h2>';
        }
    }
    

    代码已经过测试并且工作正常。希望对你有帮助

    【讨论】:

    • 非常感谢您帮助我。该代码在简单产品上效果很好,但对于可变产品似乎需要在代码中稍作修改。我们几乎所有的产品都是可变的。在这种情况下需要您的帮助,谢谢!
    • 代码也适用于可变产品。如果您看到任何错误,您需要解释一下,因为我无法弄清楚为什么它不能与您合作。
    • 此代码在产品数据中添加了一个自定义字段 -> 常规选项,但“Geeral”仅在简单产品中可用,如果我切换到可变产品,则此选项卡消失。也许我们需要在不同的选项卡中为库存或额外的可变产品产品添加该自定义字段
    • 好的,知道了。更新了答案。添加了钩子 add_action( 'woocommerce_product_options_inventory_product_data', 'mos_create_custom_field' );
    • 你太棒了,这段代码运行完美:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-12-14
    • 1970-01-01
    相关资源
    最近更新 更多