通过php函数更改woocommerce产品的状态

时间:2019-05-22 05:54:57

标签: php wordpress

如果我的会员xml中不再存在该产品,我有一个功能可以帮助我对该产品进行重定向。 我现在想要的是将产品放到草稿上,但将其链接重定向到“此产品不再存在”页面-出于seo的目的

我没有尝试什么,因为我不知道什么。 我在这里看到了一些东西,我想我的屁股在这里,但我不知道如何使用它:

Change product status if prices are updated in Woocommerce 3

function my_is_post_to_delete( $is_post_to_delete, $post_id, $import ) {
    $redirect_url = "https://stackoverflow.com";
    if ( $import->id == 72 ) {
        $redirects = get_option( '301_redirects', array() );
        $redirects = maybe_unserialize( $redirects );

        $url_for_post = get_the_permalink( $post_id );
        $url = parse_url( $url_for_post );
        if ( $url ) {
            if ( ! array_key_exists( $url['path'], $redirects ) ) {
                $redirects[ $url['path'] ] = $redirect_url;
                update_option( '301_redirects', $redirects );
            }
        }
        return false;       
    }
}
add_filter( 'wp_all_import_is_post_to_delete', 'my_is_post_to_delete', 10, 3 );

我希望我的代码可以从我的网站供稿中删除该产品,并将其链接重定向到我的网站页面。 出于搜索引擎优化的目的,我无法删除所有产品,只需要将其隐藏在我的商店中即可,但保留Google的链接(还有图片)。

1 个答案:

答案 0 :(得分:0)

您可以使用WordPress函数来更新product doesn't exist anymore in my affiliate xml.的逻辑所在的产品状态。

function my_is_post_to_delete( $is_post_to_delete, $post_id, $import ) {
    $redirect_url = "https://stackoverflow.com";
    if ( $import->id == 72 ) {

     /* Start Here*/
     $my_product = array(
          'ID'           => $import->id,
          'post_status' => 'draft',
      );
      wp_update_post( $my_product );
      /* End Here*/

        $redirects = get_option( '301_redirects', array() );
        $redirects = maybe_unserialize( $redirects );

        $url_for_post = get_the_permalink( $post_id );
        $url = parse_url( $url_for_post );
        if ( $url ) {
            if ( ! array_key_exists( $url['path'], $redirects ) ) {
                $redirects[ $url['path'] ] = $redirect_url;
                update_option( '301_redirects', $redirects );
            }
        }
        return false;       
    }
}
add_filter( 'wp_all_import_is_post_to_delete', 'my_is_post_to_delete', 10, 3 );