如何使用自定义基本slug输出产品slug

发布于 2025-02-12 20:36:56 字数 4649 浏览 0 评论 0原文

首先,向每个在这里和所有stackoverflow的工作人员提供答案的人。 我为WC_DUPLICATE_PRODUCT OFF wooCommerce的wc_duplate_product of wooCommerce编写了一个(复制糊和mod),以便在重复项上放一个元,并以“ https://website.com/slug-base/product-name”为“ slug wit base”,但是我canthish slug base的一部分。如果有人可以帮助我,我会感激。 这是我的代码,随时使用和mod:

/**
     * Function to create the duplicate of the product.
     *
     * @param WC_Product $product The product to duplicate.
     * @return WC_Product The duplicate.
     */
    public function product_duplicate( $product ) {
        /**
         * Filter to allow us to exclude meta keys from product duplication..
         *
         * @param array $exclude_meta The keys to exclude from the duplicate.
         * @param array $existing_meta_keys The meta keys that the product already has.
         * @since 2.6
         */
        $meta_to_exclude = array_filter(
            apply_filters(
                'woocommerce_duplicate_product_exclude_meta',
                array(),
                array_map(
                    function ( $datum ) {
                        return $datum->key;
                    },
                    $product->get_meta_data()
                )
            )
        );

        $duplicate = clone $product;
        $duplicate->set_id( 0 );
        $char = 47;
        /* translators: %s contains the name of the original product. */
        $duplicate->set_name( sprintf( '%s', $duplicate->get_name()) );
        $duplicate->set_total_sales( 0 );
        if ( '' !== $product->get_sku( 'edit' ) ) {
            $duplicate->set_sku( wc_product_generate_unique_sku( 0, $product->get_sku( 'edit' ) ) );
        }
        $duplicate->set_status( 'draft' );
        $duplicate->set_date_created( null );
        $duplicate->set_slug( 'vendor'.sprintf( '%c', $char).'' );
        $duplicate->set_rating_counts( 0 );
        $duplicate->set_average_rating( 0 );
        $duplicate->set_review_count( 0 );
        $duplicate->add_meta_data( '_is_clone', 'true' );

        foreach ( $meta_to_exclude as $meta_key ) {
            $duplicate->delete_meta_data( $meta_key );
        }

        /**
         * This action can be used to modify the object further before it is created - it will be passed by reference.
         *
         * @since 3.0
         */
        do_action( 'woocommerce_product_duplicate_before_save', $duplicate, $product );

        // Save parent product.
        $duplicate->save();

        // Duplicate children of a variable product.
        if ( ! apply_filters( 'woocommerce_duplicate_product_exclude_children', false, $product ) && $product->is_type( 'variable' ) ) {
            foreach ( $product->get_children() as $child_id ) {
                $child           = wc_get_product( $child_id );
                $child_duplicate = clone $child;
                $child_duplicate->set_parent_id( $duplicate->get_id() );
                $child_duplicate->set_id( 0 );
                $child_duplicate->set_date_created( null );

                // If we wait and let the insertion generate the slug, we will see extreme performance degradation
                // in the case where a product is used as a template. Every time the template is duplicated, each
                // variation will query every consecutive slug until it finds an empty one. To avoid this, we can
                // optimize the generation ourselves, avoiding the issue altogether.
                $this->generate_unique_slug( $child_duplicate );

                if ( '' !== $child->get_sku( 'edit' ) ) {
                    $child_duplicate->set_sku( wc_product_generate_unique_sku( 0, $child->get_sku( 'edit' ) ) );
                }

                foreach ( $meta_to_exclude as $meta_key ) {
                    $child_duplicate->delete_meta_data( $meta_key );
                }

                /**
                 * This action can be used to modify the object further before it is created - it will be passed by reference.
                 *
                 * @since 3.0
                 */
                do_action( 'woocommerce_product_duplicate_before_save', $child_duplicate, $child );

                $child_duplicate->save();
            }

            // Get new object to reflect new children.
            $duplicate = wc_get_product( $duplicate->get_id() );
        }

        return $duplicate;
    }


这是我需要抛光的代码的一部分才能获得所需的sl,因此当我运行此CLAS时,请始终获得 https://website.com/slug-baseproduct-name

first of all thx to everyone who put answers here and to all the staff of stackoverflow.
I write a class(copy paste and mod) of WC_duplicate_product off woocommerce in order to put a meta on duplicates and to generate a slug wit base as "https://website.com/slug-base/product-name", but I cant acomplish the part of slug-base. If anybody can help me i'll appreciate.
This is my code, feel free to use and mod:

/**
     * Function to create the duplicate of the product.
     *
     * @param WC_Product $product The product to duplicate.
     * @return WC_Product The duplicate.
     */
    public function product_duplicate( $product ) {
        /**
         * Filter to allow us to exclude meta keys from product duplication..
         *
         * @param array $exclude_meta The keys to exclude from the duplicate.
         * @param array $existing_meta_keys The meta keys that the product already has.
         * @since 2.6
         */
        $meta_to_exclude = array_filter(
            apply_filters(
                'woocommerce_duplicate_product_exclude_meta',
                array(),
                array_map(
                    function ( $datum ) {
                        return $datum->key;
                    },
                    $product->get_meta_data()
                )
            )
        );

        $duplicate = clone $product;
        $duplicate->set_id( 0 );
        $char = 47;
        /* translators: %s contains the name of the original product. */
        $duplicate->set_name( sprintf( '%s', $duplicate->get_name()) );
        $duplicate->set_total_sales( 0 );
        if ( '' !== $product->get_sku( 'edit' ) ) {
            $duplicate->set_sku( wc_product_generate_unique_sku( 0, $product->get_sku( 'edit' ) ) );
        }
        $duplicate->set_status( 'draft' );
        $duplicate->set_date_created( null );
        $duplicate->set_slug( 'vendor'.sprintf( '%c', $char).'' );
        $duplicate->set_rating_counts( 0 );
        $duplicate->set_average_rating( 0 );
        $duplicate->set_review_count( 0 );
        $duplicate->add_meta_data( '_is_clone', 'true' );

        foreach ( $meta_to_exclude as $meta_key ) {
            $duplicate->delete_meta_data( $meta_key );
        }

        /**
         * This action can be used to modify the object further before it is created - it will be passed by reference.
         *
         * @since 3.0
         */
        do_action( 'woocommerce_product_duplicate_before_save', $duplicate, $product );

        // Save parent product.
        $duplicate->save();

        // Duplicate children of a variable product.
        if ( ! apply_filters( 'woocommerce_duplicate_product_exclude_children', false, $product ) && $product->is_type( 'variable' ) ) {
            foreach ( $product->get_children() as $child_id ) {
                $child           = wc_get_product( $child_id );
                $child_duplicate = clone $child;
                $child_duplicate->set_parent_id( $duplicate->get_id() );
                $child_duplicate->set_id( 0 );
                $child_duplicate->set_date_created( null );

                // If we wait and let the insertion generate the slug, we will see extreme performance degradation
                // in the case where a product is used as a template. Every time the template is duplicated, each
                // variation will query every consecutive slug until it finds an empty one. To avoid this, we can
                // optimize the generation ourselves, avoiding the issue altogether.
                $this->generate_unique_slug( $child_duplicate );

                if ( '' !== $child->get_sku( 'edit' ) ) {
                    $child_duplicate->set_sku( wc_product_generate_unique_sku( 0, $child->get_sku( 'edit' ) ) );
                }

                foreach ( $meta_to_exclude as $meta_key ) {
                    $child_duplicate->delete_meta_data( $meta_key );
                }

                /**
                 * This action can be used to modify the object further before it is created - it will be passed by reference.
                 *
                 * @since 3.0
                 */
                do_action( 'woocommerce_product_duplicate_before_save', $child_duplicate, $child );

                $child_duplicate->save();
            }

            // Get new object to reflect new children.
            $duplicate = wc_get_product( $duplicate->get_id() );
        }

        return $duplicate;
    }


this is teh part of code that I need to polish to get the desired slug, so when i run this clas, always obtain https://website.com/slug-baseproduct-name

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文