programing

Woocommerce에서 제품 이름 가져오기

codeshow 2023. 3. 13. 20:42
반응형

Woocommerce에서 제품 이름 가져오기

PHP를 사용하여 제품명을 제품 ID로 에코함으로써 제품 제목을 표시할 수 있도록 하고 싶습니다(제품 페이지 자체가 아닌 페이지 내에 표시되도록 합니다).워드프레스를 사용하고 있으며 PHP용 플러그인이 있기 때문에 PHP 코드를 포함할 수 있습니다.[php]echo 'example';[/php]

제품의 예로는 http://ukcctvinstallations.co.uk/product/1-camera-residential-system-hi-res/가 있습니다.

상품을 편집하면 URL에 'Post' = 129라고 표시되어 있는데, 이것이 상품 ID라고 하는 것이 맞습니까?

누군가 이에 대한 해결책을 가지고 있다면 매우 감사하겠습니다.저는 WooCommerce를 사용하고 있습니다.

제품을 개체로 받는다고 가정합니다.

$product = wc_get_product( id );

echo $product->get_title();

(WC 버전 2.5.2)

솔루션을 찾았습니다.-

echo get_the_title( 'ID' );

여기를 봐주세요.http://docs.woothemes.com/wc-apidocs/package-WooCommerce.Functions.Product.html

2017 - 2020 - WooCommerce 3부터WC_Product방법get_name()

global $product;

// If the WC_product Object is not defined globally
if ( ! is_a( $product, 'WC_Product' ) ) {
    $product = wc_get_product( get_the_id() );
}

echo $product->get_name();

카트 항목:

foreach( WC()->cart->get_cart() as $cart_item ) {
    echo $cart_item['data']->get_name();
}

주문시 아이템:

foreach( $order->get_items() as $cart_item ) {
    echo $item->get_name();
    // Or
    $product = $item->get_product(); // Get the WC_Product Object
    echo $product->get_name();
}

다음은 제 CCTV 워드프레스 사이트의 페이지에서 가져온 PHP 코드입니다.

<header class="entry-header">
<?php
if ( is_singular() ) :
    the_title( '<h1 class="entry-title">', '</h1>' );
else :
    the_title( sprintf( '<h2 class="entry-title"><a href="%s" rel="bookmark">', esc_url( get_permalink() ) ), '</a></h2>' );
endif;
?>

언급URL : https://stackoverflow.com/questions/26167916/get-the-product-name-in-woocommerce

반응형