Skip to content
View in the app

A better way to browse. Learn more.

Web Designer Forum

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Having Trouble With Function Command Wordpress Retrieving New ID Property

Featured Replies

Hi All,

 

Having a bit a trouble with this code added to themes functions.php file. Trying to add a new Custom Field Wordpress/Woocommerce, in Admin Panel Product>General>Sku and also show it on Single Product Page front end just under the SKU. The Field being added is "Brand". Everything I have so far is working as expected. Field "Brand" has been created and it will save the inputed text. Also The Heading Brand is showing on Frontend. The Problem is retrieving the inputed text. Can not figure out correct call function and keep getting a Fatal error at that line. I am including the code below I am using and Highlighting the line I am having trouble with.

 

// Display Fields
add_action( 'woocommerce_product_options_sku', 'wc_brand_product_field' );
function wc_brand_product_field() {
woocommerce_wp_text_input(
array(
'id' => 'brand_field',
'class' => 'wc_input_sku short',
'label' => __( 'Brand', 'woocommerce' )
)
);
}
// Save Fields using WooCommerce Action Hook
add_action( 'save_post', 'wc_brand_save_product' );
function wc_brand_save_product( $product_id ) {
if ( isset( $_POST['brand_field'] ) ) {
if ($_POST['brand_field'] )
if ( $_POST['brand_field'] )
update_post_meta( $product_id, 'brand_field', $_POST['brand_field'] );
}
}
add_action( 'woocommerce_single_product_summary', 'wc_brand_show', 5 );
function wc_brand_show() {
global $product;
Do not show this on variable products
if ( $product->product_type <> 'variable' ) {
$brand = get_post_meta( $product->id, 'brand_field', true );
echo '<div class="woocommerce_brand">';
_e( 'Brand: ', 'woocommerce' );
echo '<span class="woocommerce_brand_field_sku-wrapper">' . woocommerce_product_meta_sku_wrapper( $brand ) . '</span>';
echo '</div>';
}
}
I have highlighted the line above I am having trouble with. I know I am not pointing to correct ID class. I just can't figure out. If I point it to _price class it works without error but forces numeric layout. Any help on this would be much appreciated. Thanks in advance.
Joe
  • Author

Consider this request closed. Finally figured it out when I simplified it. Here is the code that fixed it.

add_action( 'woocommerce_single_product_summary', 'wc_brand_show', 5 );
function wc_brand_show() {
		$brand = get_post_meta( $product->id, 'brand_field', true );
		_e( 'Brand: ', 'woocommerce' );
		echo get_post_meta( get_the_ID(), 'brand_field', true );
	
}

Thanks to anyone who was taking a crack at it

Create an account or sign in to comment

Account

Navigation

Search

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.